views:

360

answers:

2

I am trying to implement Automapper to map a ViewModel to an Entity where one of the properties of the Entity is also an Entity.

I want my converter to use NHibernate's ISession.Load<> method to load this.

So the question is what is the best way of injecting ISession into my ITypeConverter implementation? Also one thing to keep in mind is that ISession that gets injected will be disposed off, so I would need to inject a new ISession everytime when a mapping needs to happen?

A: 

I don't know about nHibernate sorry and no one seems to want to answer this question so...

The way I would tackle this is to maybe write my own Custom Model Binder. It can then be resonsible for mapping my ViewModel to my Entity.

You will also have access to the HttpRequest object so you can get all your text fields out and map them to your entity.

I hope this helps even though it's not specific to your question.

griegs
This would work, but I am trying to use tools like AutoMapper so that I don't have to map each property by hand.
adriaanp
+1  A: 

We do this in our systems, and have things like Guid->Entity type converters. However, we scope our ISessions per HttpContext, so a new ISession will not be injected per ITypeConverter. However, AutoMapper does instantiate a new ITypeConverter instance every time it's needed.

But two entities coming together from different ISession instances will lead into trouble. Just make sure you share a single ISession instance per HttpContext, and you'll be set.

Jimmy Bogard
Jimmy I have another problem here as I would not really need the ISession directly but with addition to an IRepository, so my ITypeConverter(s) would be depended on different IRepositories. I think the only way of getting this to work is to ask my IoC for the IRepository in the ITypeConverter which I am trying to avoid.
adriaanp
Is there a way of intercepting the ITypeConverter instanciating in AutoMapper?
adriaanp
Yep, we typically do this in the Initialize call by using the ConstructServicesUsing() method to setup AutoMapper to use our container for all formatters, resolvers and type converters.
Jimmy Bogard