问题描述
我怎么能分享我的C#的web应用程序的所有层之间自动生成的实体数据模型(生成的对象类),而只在数据层授权查询访问?本品采用典型的3层方法:数据,业务,presentation
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.
我的数据层返回的IEnumerable< T>
来我的业务层,但我不能类型T返回presentation层,因为我不想让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.
推荐答案
如果您使用 POCO实体(.NET 4+),那么这是很容易的(或至少更容易)。那是一种可能性?
If you use POCO entities (.NET 4+), then this is easy (or at least easier). Is that a possibility?
您可以创建的DTO作为本说,但你基本上弱智化和复制每一个实体。 EF2将创建简单化的实体和动态添加更改跟踪,延迟加载等。如果你的愿望。
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.
否则,答案是不能。如果实体依赖于实体框架,那么你可以不加上拖动依赖使用它们整个应用程序。在这种情况下,你必须使用的DTO。下面是EF 1或EF 2没有POCO实体第三方选项。
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/
编辑:下面是一些有用的链接了解更多有关这一切:
Here are some useful links to learn more about all this:
- 常规MS指南:
- POCO模板:
的 - POCO模板,包括如何
移动到单独的项目:
http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx - POCO代理:
- 如何分割模型:
http://blogs.msdn.com/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx - 员工跟踪示例应用程序
(层,单元测试,嘲讽,
库等):
的
- General MS guidelines:http://msdn.microsoft.com/en-us/library/bb738470.aspx
- POCO templates:http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx
- POCO templates, including how tomove to separate project:http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx
- POCO proxies:http://blogs.msdn.com/adonet/archive/2009/12/22/poco-proxies-part-1.aspx
- How to split up model:http://blogs.msdn.com/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx
- Employee Tracker sample application(layers, unit tests, mocking,repository, etc.):http://code.msdn.microsoft.com/ef4/Release/ProjectReleases.aspx?ReleaseId=4279
这篇关于ASP.Net应用分层 - 分享实体数据模型层之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!