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?
Which is indeed good.
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).