views:

65

answers:

2

Hello:

I am developing a medium sized ASP.NET project using ASP.NET MVC and Entity Framework. I have developed a 3-tier system by setting up 3 Visual Studio projects and referencing them accordingly:

  • Presentation -- This is my MVC project and contains all the views and controllers. I deleted the model folder completely from this project as I am moving it to the BO project (see below)
  • Business Objects (BO) -- This project contains the "meat" of the application, and is where the real heart of the application is located. Here, objects are defined that represent things I'm trying to model in code (User, Facility, Appointment, etc.).
  • Data Access (DA) - This project is all Entity Framework so far.

The "problem" that I am having is that I am doing a lot of manual one-to-one mapping in the BO. For example, when a User.load() is called, I load the user from EF, then map a number of parameters (firstname, lastname, username, active, etc.) from the EF result to parameters on the object.

I see this as good and bad. Good: it disconnects EF from the project, so if I ever need to use another data store I'm not tied only to EF. Bad: it takes a little more time, because I have to setup each parameter and carefully handle them on Add(), Update(), etc. by implementing my own change tracking.

What do you think of this approach?

A: 

it disconnects EF from the project

Which is indeed good.

I am doing a lot of manual one-to-one mapping in the BO

I suggest you take a look at AutoMapper.

I find the book ASP.NET MVC in Action from Manning quite good. The second version, recently released, also has a small chapter about AutoMapper included. It's not in the free sample chapters but you might want to check out the source code (or buy the book of course).

XIII
A: 

If you are using .Net 4.0 then you should definitely consider creating and using POCO Entities instead of EntityObject which is not only gives you Persistence Ignorance (which you have mentioned) but also you will not need any Mapper in between since you work with POCOs (Plain Old CLR Object) in all layers including your data access. If you haven't work with EF & POCOs, then this would be a good start: http://blogs.msdn.com/b/adonet/archive/2009/05/21/poco-in-the-entity-framework-part-1-the-experience.aspx

If you are using .Net 3.5 SP1 and CANNOT upgrade to 4.0 then like XIII correctly mentioned, AutoMapper will automate your mapping process or you can come up with your own AutoMapper which is nothing more than a simple reflection code.

Morteza Manavi