Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top