Using the instance version of CreateMap and Map with a WCF service?

后端 未结 5 351
青春惊慌失措
青春惊慌失措 2020-12-08 22:56

Been having some real issues with automapper. I think I have found the solution but unsure of how to implement it.

basically I am using a custom mapping with Resolve

相关标签:
5条回答
  • 2020-12-08 23:24

    If you want to use an instanced version of the Mapper in Automapper, then I think you can use the MappingEngine class. I believe the static Mapper class instantiates and configures an MappingEngine object to do all of the nitty gritty mapping work.

    Here's an example of applying IoC to Automapper (which requires instantiation of the MappingEngine)

    http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/05/11/automapper-and-ioc.aspx

    0 讨论(0)
  • 2020-12-08 23:28

    In response to Luke Woodwards's comment on the newer syntax:

    ConfigurationStore store 
       = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
    store.AssertConfigurationIsValid();
    MappingEngine engine = new MappingEngine(store);
    
    //add mappings via Profiles or CreateMap
    store.AddProfile<MyAutoMapperProfile>();
    store.CreateMap<Dto.Ticket, Entities.Ticket>();
    
    0 讨论(0)
  • 2020-12-08 23:36

    Have you looked at using the Map call that takes in the destination object?

    var bar = new Bar("Custom each call");

    Mapper.Map(foo, bar);

    0 讨论(0)
  • 2020-12-08 23:37

    Yes, there is a way to use an instance version of AutoMapper.

    Instead of...

    Mapper.CreateMap<Dto.Ticket, Entities.Ticket>()
    

    you can use:

    var configurationStore =
        new ConfigurationStore(new TypeMapFactory(), MapperRegistry.Mappers);
    var mapper = new MappingEngine(configurationStore);
    configurationStore.CreateMap<Dto.Ticket, Entities.Ticket>()
    
    0 讨论(0)
  • 2020-12-08 23:39

    Well it appears that my question is abandoned but after quite a while playing around I finally found a GOOD fix..

    basically i was inside a Resolve and i had another MAP which one of the properties called another ResolveUsing ...

    It appears there seems to be an issue with this. Another weird thing is that it failed everytime the application pool was started or recycled.. Hence it failed the first time and then was ok until the recycle happened (i am using a wcf app).

    So i replaced the second Mapping with with a foreach and did my mapping like that inside my original Resolve ...

    I have put the answer here in case it can help anybody else in the future..

    I was using the Mapper static methods to do my mappings, these were not in global.asax as i need to pass different things depending on certain factors..

    I always wondered if it would be possible to do it with Instance versions of mappper, i though it existed..... but never found out..

    But anyway all is working 100% now...

    0 讨论(0)
提交回复
热议问题