开发者

AutoMapper custom type converter dependencies

开发者 https://www.devze.com 2022-12-15 22:45 出处:网络
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 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消