开发者

3 layer design question, binding data to form

开发者 https://www.devze.com 2023-02-06 19:52 出处:网络
I want to bind controls on form to a data from database, the binding happens in codebehind. I want to use 3 layer pattern, DAL layer that will use Entity Framework, BLL that will use DAL to retrieve

I want to bind controls on form to a data from database, the binding happens in codebehind.

I want to use 3 layer pattern, DAL layer that will use Entity Framework, BLL that will use DAL to retrieve the data, and my form to display it.

Let's say there is an Entity object called Product with 3 properties: Name, Count, Price

I want to bind them to 3 text boxes on the from.

from code behind I call BLL's method that will call DAL method to retrieve the needed entity object...

Here is the problem, in order to bind data on the form, form must "know" about this entityObject that returned by BLL.

So I could do this:

DataAccessLayer.Product product = BusinessLogicLayer.GetProduct(someid);
textbox1.text=product.name; 
textbox2.text=product.price; 
....

But this breaks the 3-layer pattern!! How to be? Create a new obje开发者_如何学Pythonct just to hold this data?


I typically separate my business objects and interfaces that deal with these objects like IRepository into a Domain project. Then my DAL can reference this project and so can my web project.

UPDATE

Project structure:

  • Domain - contains entities, interfaces
  • DAL - contains data access logic, implements interfaces form Domain
  • BLL - contains business logic, uses DAL and Domain, potentially implements some interfaces from Domain as well
  • Web - contains pages, uses BLL and entities/interfaces from Domain


Work with interfaces. Then your form knows about some IProduct, but does not need to know about the specific implementation. You could use NInject for example, to do the dependency injection.


The most common way and also a best practice is to use a client side repository. You can abstract your entities into domain objects. You can use a mapper like AutoMapper to map the data transfer objects (DTO's) which are returned by the service layer and the domain entities which are used by the application layer. By abstracting this behind a repository your application layer need not know about the servie layern and the two can vary independently.

0

精彩评论

暂无评论...
验证码 换一张
取 消