views:

112

answers:

3

Is it possible to use automapper with Immutabile types. For example my Domain type is immutable and I want to map my view type to this. I believe it is not but just want this confirmed. Also as it is best pratice to have your domain types immutabile, what is the best pratice when mapping your view types to domain types?

+2  A: 

I typically do the mapping from view types to domain types by hand, as I'll typically be working through a more complex interface, using methods and so on. If you use AutoMapper to go from view to domain, you're now locked in to an anemic domain model, whether you've intentionally decided to or not.

Jimmy Bogard
@Jimmy: Why would you say the domain model is anemic, if you use automapper to map from view to domain? You will need your domain model populated with the data from the view somehow. Is it because you propose creating/populating the domain through ctors/methods instead?
Per Hornshøj-Schierbeck
Yep, exactly. Check out my series on strengthening your domain: http://www.lostechies.com/blogs/jimmy_bogard/archive/2010/02/03/strengthening-your-domain-a-primer.aspxIf you look at other MVC frameworks, like Rails, the concept of a Model is a persistent model, so you don't need AutoMapper.
Jimmy Bogard
+1  A: 

AutoMapper relies on property setters to do its work, so if you have read-only properties, AutoMapper won't be of much use.

You could override the mapping behaviour and, for example, configure it to invoke a specific constructor, but that basically defeats the purpose of AutoMapper because then you are doing the mapping manually, and you've only succeeded in adding a clumsy extra step in the process.

It doesn't make a lot of sense to me that your domain model is immutable. How do you update it? Is the entire application read-only? And if so, why would you ever need to map to your domain model as opposed to from? An immutable domain model sounds... pretty useless.

P.S. I'm assuming that you mean this AutoMapper and not the auto-mapping feature in Fluent NHibernate or even some other totally different thing. If that's wrong then you should be more specific and add tags for your platform/language.

Aaronaught
A: 

Jimmy, Thanks for the reply. I am a bit confused with your answer as you are the creator of automapper. To be clear I am talking about mapping from my MVC view model types to my domain types and visa-versa. Surely this is what automapper was created for?

Aaronaught I agree with your answer and I think our use of domain types been immutable is probably not correct.

Noel