开发者

ASP.Net Layered app - Share Entity Data Model amongst layers

How can I share the auto-generat开发者_运维知识库ed entity data model (generated object classes) amongst all layers of my C# web app whilst only granting query access in the data layer? This uses the typical 3 layer approach: data, business, presentation.

My data layer returns an IEnumerable<T> to my business layer, but I cannot return type T to the presentation layer because I do not want the presentation layer to know of the existence of the data layer - which is where the entity framework auto-generated my classes.

It was recommended to have a seperate layer with just the data model, but I'm unsure how to seperate the data model from the query functionality the entity framework provides.


If you use POCO entities (.NET 4+), then this is easy (or at least easier). Is that a possibility?

You can create DTOs as Ben said, but then you're basically dumbing down and duplicating each of the entities. EF2 will create the "dumbed down" entities and dynamically add change tracking, lazy loading, etc. if you wish.

Otherwise the answer is you can't. If the entities depend on the Entity Framework, then you can't use them throughout your application without dragging that dependency along. In that case you have to use DTOs. Here's a 3rd party option for EF 1 or EF 2 without POCO entities. http://automapper.codeplex.com/

Edit: Here are some useful links to learn more about all this:

  1. General MS guidelines: http://msdn.microsoft.com/en-us/library/bb738470.aspx
  2. POCO templates: http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx
  3. POCO templates, including how to move to separate project: http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx
  4. POCO proxies: http://blogs.msdn.com/adonet/archive/2009/12/22/poco-proxies-part-1.aspx
  5. How to split up model: http://blogs.msdn.com/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx
  6. Employee Tracker sample application (layers, unit tests, mocking, repository, etc.): http://code.msdn.microsoft.com/ef4/Release/ProjectReleases.aspx?ReleaseId=4279


You could create DTOs from your data entities and pass your DTOs to the rpesentation layer.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜