简体   繁体   English

使用存储库模式与Entity Framework一起使用的自动映像?

[英]Automapper for use with Entity Framework using repository pattern?

Been busy creating a new app, basically i have my dataccess, service layer and presentation layer... All works great but i am using the entity classes that are returned by EF. 忙着创建一个新的应用程序,基本上我有我的dataccess,服务层和表示层...一切都很好,但我使用EF返回的实体类。 Problem here is i pass these onto the presentation layer so i need to add the entity framework reference /dataccess to the presentation layer - NOT GOOD: 这里的问题是我将这些传递到表示层,所以我需要将实体框架引用/ dataccess添加到表示层 - 不好:

So my idea was the following and was looking for some help and confirmation that i am going down the right lines... 所以我的想法是以下,并正在寻找一些帮助和确认,我正在走下正确的路线......

  1. Create a set of classes in the service layer like customer, order etc because the presentation layer has a reference to the service layer.. 在服务层中创建一组类,如客户,订单等,因为表示层具有对服务层的引用。

  2. When a customer entity is returned in the dataccess i would return the entity class ie Customer to the service and i would do the mapping here - Not too sure if i like this? 当在dataccess中返回一个客户实体时,我会将实体类即Customer返回给服务,我会在这里进行映射 - 不太确定我喜欢这个吗?

  3. where would be the best places for the these "standard classes" that i use for mapping, if i put them in the service layer and do the mapping the dataaccess then this would create a circular reference as Dataccess > service and service > dataaccess.. - it should be only one way ie service > dataaccess 哪里是我用于映射的这些“标准类”的最佳位置,如果我将它们放在服务层并对dataaccess进行映射,那么这将创建一个循环引用,如Dataccess> service and service> dataaccess .. - 它应该只有一种方式,即服务> dataaccess

I was thinking of using Automapper ( http://www.codeplex.com/AutoMapper ) to take care of this, am i on the right lines??? 我正在考虑使用Automapper( http://www.codeplex.com/AutoMapper )来处理这个问题,我是否在正确的路线上? Any ideas or examples really appreciated.. 任何想法或例子真的很感激..

As i say the only thing is that when i return from dataaccess to service layer (using Iqueryable) i need to map these away from the entity classes and use standard collection classes.. 正如我所说,唯一的事情是当我从dataaccess返回到服务层(使用Iqueryable)时,我需要将它们从实体类中映射出来并使用标准集合类。

I think this is where i get confused, i do feel it isn't good using the entity classes because that means i need a reference to the entity framework / dataaccess in my presentation layer to be able to access the entity classes.. 我认为这是我感到困惑的地方,我觉得使用实体类并不好,因为这意味着我需要在我的表示层中引用实体框架/ dataaccess才能访问实体类。

You've hit one of the weak points of EF v1. 你已经遇到了EF v1的一个弱点。 For now, yes, going the route with AutoMapper certainly allows you to convert your EF entities to "straight" business entities and use them in your higher-up layer. 现在,是的,使用AutoMapper进行路线肯定允许您将EF实体转换为“直接”业务实体并在更高层中使用它们。

Also, EF v4 which is due with .NET 4.0 / Visual Studio 2010 should bring a lot of relieve in many of the problem areas - support for your own, straight POCOs (Plain Old CLR Objects), and a great many more. 此外,应用于.NET 4.0 / Visual Studio 2010的EF v4应该会在许多问题领域带来很多缓解 - 支持您自己的直接POCO(普通旧CLR对象)以及更多。 Check out the EF Design Blog . 查看EF设计博客 The team has posted a number of very interesting, very promising posts lately, with regards to EF v4. 关于EF v4,该团队最近发布了一些非常有趣,非常有前途的帖子。 I'm looking forward to it! 我对此很期待!

Marc

  1. You can use EF objects in Web project if you extract their interfaces into your core/common project and return the interface types from repository or services, that the Web project uses. 如果将接口提取到核心/公共项目中并从Web项目使用的存储库或服务返回接口类型,则可以在Web项目中使用EF对象。 You can make EF objects implement your interfaces, by creating partial classes and adding right there: 您可以通过创建部分类并在那里添加来使EF对象实现您的接口:

    partial class Customer : ICustomer 部分类客户:ICustomer

  2. Even though you can do 1. trick, you probably should anyway use automapper to map those entities to your custom ViewModel objects, that suit your particular view. 即使你可以做1.技巧,你可能应该使用automapper将这些实体映射到适合你的特定视图的自定义ViewModel对象。 You can also make the query methods of repository/services return DTO/ViewModels directly - it will allow making queries more efficient sometimes (querying only needed columns etc), but that would require additional EF mappings. 您还可以使存储库/服务的查询方法直接返回DTO / ViewModels - 它有时可以使查询更有效(仅查询所需的列等),但这需要额外的EF映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM