Entity Framework in n-Tier
Using a simple example:
- Presentation Tier : aspx and code behind.
- Business Tier: WCF with related logics
- Data Access Tier: WCF with entity framework
When I want to populate a grid view, there will be a need to call a method form the business logic, which in turns call the data access tier's method.
The methods and related logic are as follow.
Data Access Tier:
public SampleEntity[] LoadSampleEntity()
{
//retrieve SampleEntity from database.
}
Business Logic Tier:
public SampleEntity[] GetSampleEntity()
{
//call the proxy to access Data Access Tier
//Call LoadSampleEntity()
}
Presentation Tier:
protected void btn_OnClick()
{
//call the proxy to access Business Logic Tier
//Call GetSampleEntity()
//SampleEntity[] sampleEntity=BusinessLogic.GetSampleEntity();
//gridview.datasouce = sampleEntity
//gridview.databind();
}
Given this structure, if SampleEntity was modified, all the 3 tiers will require re-compiling.
Is there any way to reduce the need of re-compiling when new property/column is added to SampleEntity .One way which I have worked on will be to convert SampleEntity[] to datatable type at Business Logic Tier, and pass the datatable over to Presentation Tier. Howev开发者_如何学Pythoner doing so removes the intellisense feature of EntityFramework in the Presentation Tier.
You could try the repository pattern. You'd first have a domain layer of objects that would be similar to your Entity objects, this domain layer would be in it's own project/dll. You would use something like AutoMapper to map the values from the Entity object over to the Domain object, via the business layer. Then the presentation layer would/could share the domain object layer, that way you abstract the entity data layer behind the
IRepository<T>
{
IEnumerable<T> GetAll();
T GetById(int id);
void Save(T saveThis);
void Delete(T deleteThis);
}
Your type T for each table would be the domain type and inside these methods you'd map the Entity type to your domain type. This IRepository can be your service contract on the Data Tier access.
As for your other question any change you make to your entity object would need to be reflected in the domain object but this can be done with something like AutoMapper. I think in most cases you don't want to share an EF or LinqToSql data type all the way out to the view or UI.
Since it's WCF you could have the Domain Layer be your serialized types.
You might be interested in using the open source N-Tier Entity Framework which uses Entity Framework on server-side and generates the entire infrastructure for building an n-tier architecture based on WCF including an EF-like API on client-side. Have a look at the framework’s user guide available for download on codeplex, it contains an entire section on architectural considerations.
精彩评论