Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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:

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 i.e. Customer to the service and i would do the mapping here - Not too sure if i like this?

  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 i.e. service > dataaccess

I was thinking of using Automapper (http://www.codeplex.com/AutoMapper) to take care of this, am i on the right lines??? 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..

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..

like image 231
mark smith Avatar asked May 20 '09 13:05

mark smith


People also ask

What is automapper in Entity Framework?

Using Automapper to improve performance of Entity Framework Entity Framework is an ORM technology widely used in the.NET world. It’s very convenient to use and lets you forget about SQL… well, at least until you hit performance issues.

Can automapper be used to create domain models?

AutoMapper could be used for the opposite situation (mapping to data entities) but not when creating domain models. Below is the core of my repository pattern. The EntityFrameworkRepository class works with two generic types: ToDataEntity (TDomainModel): To convert to data entities (for Add () and Update () methods)

How does the entityframeworkrepository work?

The EntityFrameworkRepository class works with two generic types: ToDataEntity (TDomainModel): To convert to data entities (for Add () and Update () methods) ToDomainModel (TEntityModel): To construct domain models (for the Find () method). Concrete implementations of these methods would define the mapping required for the repository in question.

Is it possible to add mapping rules to Poco objects in Entity Framework?

I've been using NHibernate and know that in Entity Framework you can also specify mapping rules from DB tables to your POCO objects. It is an extra effort to develop another abstraction layer over Entity Framework entities.


1 Answers

You've hit one of the weak points of 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.

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. Check out the EF Design Blog. The team has posted a number of very interesting, very promising posts lately, with regards to EF v4. I'm looking forward to it!

Marc

like image 163
marc_s Avatar answered Oct 13 '22 00:10

marc_s