0

I have been working on a new ASP.NET MVC application and trying my best to implement the Service Layer/Repository/UOW patter using EF4 and POCO classes.

Help me see if I am understanding this correctly.

Lets say for the sake of keeping this simple, that a client is requesting a view of a Customer.

1) Client requests a view from the CustomerController.
2) The CustomerController creates a new UOW and a new CustomerService passing in the UOW.
3) The CustomerService creates a new Repository(Of Customer) and passes in the UOW it received from the CustomerService. This is the layer where you would say maybe something like "Are you allowed to view this customer?"
4) The CustomerRepository handles getting the POCO classes from the EF4.
5) The CustomerRepository hands the POCO classes back to the CustomerService, which then hands them back to the CustomerController.
6) The CustomerController uses the POCO classes to fill the CustomerViewModel and then hands the CustomerViewModel off to the CustomerView.

I am still a little confused on why/where to use AutoMapper???

Any advice on this would be greatly appreciated.

Sam
  • 15,336
  • 25
  • 85
  • 148
  • 1
    Sounds really complicated. How big is this application? Unless it's a huge, enterprisey application, this much ceremony may not be needed. See http://nerddinnerbook.s3.amazonaws.com/Part3.htm for an example of a simple repository pattern that might work well for your application. Automapper is used when you need a simple way to hook up ViewModels to the underlying model objects. – Robert Harvey Feb 25 '11 at 19:38
  • A similar question: http://stackoverflow.com/questions/3747383/best-practices-to-partition-model-code-to-logical-parts-in-mvc-which-is-the-best#3747474 – Colin Desmond Feb 25 '11 at 20:09

2 Answers2

2

AutoMapper can be used to "automatically" fill CustomerViewModel from the POCO class instead of manually writing the left side = right side code. However, if you are comfortable with the custom code, there is no need to use AutoMapper. It is just a tool to reduce the custom, boring and error prone code.

amit_g
  • 30,880
  • 8
  • 61
  • 118
  • I guess I don't see why you would not just let the View Models use the POCO classes? And do the View Models have objects on them or just simple properties. Like the CustomerViewModel, would it have a property of type Customer, or would it have FirstName, LastName, Address, etc.? If my View Model had a Customer and a collection of CustomerAddress, etc, could AutoMapper flatten that out? – Sam Feb 25 '11 at 20:53
  • 1
    @Sam Satriano, it goes both ways. For simpler view models the view model classes can simply enclose POCO classes. However, this approach makes view model and poco classes tightly coupled. So many argue against it and recommend having view models completely separate. I have seen and done both ways and like other things both ways have pros and cons. This is a decision that you would have to take yourself i.e. how much separation you want in your application. – amit_g Feb 25 '11 at 23:33
  • So if i wanted to use AutoMapper and separate out the POCOS from the View Models, what do you do? Do you create a whole set of classes that mimic your POCOS into another assembly or something and then use AutoMapper to map the POCOS to the View Model Objects to be used in the View Model? – Sam Feb 25 '11 at 23:56
  • 1
    @Sam Satriano, The view model classes don't mimic the POCOs but represent the views. The only reason we want to write them is so that they can be bound to the views. With those we need to write custom code the copy the values between view model and POCOs. The AutoMapper tool can help reduce that custom code. – amit_g Feb 26 '11 at 01:17
  • Sorry to bother, but you are a great resource :) Do you happen to have a View Model/AutoMapper sample I could see? – Sam Feb 26 '11 at 03:17
0

The Automapper is generated automatically by the tool and can be put in the DAL. Incase we want to replace Entity Framework itself, then in my opinion we could reuse the automapper classes.

Similar is attempted and can be found in the GitHub

TechNet

Chiranjeeb
  • 211
  • 2
  • 5