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

杀马特。学长 韩版系。学妹 提交于 2019-12-04 04:07:45

问题


How can I share the auto-generated 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.


回答1:


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



回答2:


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



来源:https://stackoverflow.com/questions/2850371/asp-net-layered-app-share-entity-data-model-amongst-layers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!