ASP.Net 分层应用程序 - 在层之间共享实体数据模型
如何在 C# Web 应用程序的所有层之间共享自动生成的实体数据模型(生成的对象类),同时仅在数据层中授予查询访问权限?这使用了典型的 3 层方法:数据、业务、表示。
我的数据层将 IEnumerable
返回到我的业务层,但我无法将类型 T 返回到表示层,因为我不希望表示层知道数据层的存在 - 这是实体框架自动生成我的类的地方。
建议有一个单独的层,仅包含数据模型,但我不确定如何将数据模型与实体框架提供的查询功能分开。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用 POCO 实体 (.NET 4+),那么这很容易(或者至少更容易)。有这种可能吗?
您可以像 Ben 所说的那样创建 DTO,但是您基本上会简化并复制每个实体。如果您愿意,EF2 将创建“简化”实体并动态添加更改跟踪、延迟加载等。
否则答案是你不能。如果实体依赖于实体框架,那么如果不拖拽该依赖关系,您就无法在整个应用程序中使用它们。在这种情况下,您必须使用 DTO。这是没有 POCO 实体的 EF 1 或 EF 2 的第 3 方选项。
http://automapper.codeplex.com/
编辑: 这里有一些有用的了解更多信息的链接:
http://msdn.microsoft.com/en-us/library/bb738470。 aspx
http://blogs.msdn。 com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx
移动到单独的项目:
http:// blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx
http://blogs.msdn。 com/adonet/archive/2009/12/22/poco-proxies-part-1.aspx
http://blogs.msdn.com/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx
(层、单元测试、模拟、
存储库等):
http://code.msdn.microsoft.com/ef4/发布/ProjectReleases.aspx?ReleaseId=4279
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:
http://msdn.microsoft.com/en-us/library/bb738470.aspx
http://blogs.msdn.com/adonet/pages/walkthrough-poco-template-for-the-entity-framework.aspx
move to separate project:
http://blogs.msdn.com/adonet/pages/feature-ctp-walkthrough-poco-templates-for-the-entity-framework.aspx
http://blogs.msdn.com/adonet/archive/2009/12/22/poco-proxies-part-1.aspx
http://blogs.msdn.com/adonet/archive/2008/11/25/working-with-large-models-in-entity-framework-part-2.aspx
(layers, unit tests, mocking,
repository, etc.):
http://code.msdn.microsoft.com/ef4/Release/ProjectReleases.aspx?ReleaseId=4279
您可以从数据实体创建 DTO 并将 DTO 传递到表示层。
You could create DTOs from your data entities and pass your DTOs to the rpesentation layer.