How to inject AutoMapper IMappingEngine with StructureMap

前端 未结 4 1216
生来不讨喜
生来不讨喜 2020-12-31 05:58

Most of the examples I\'ve found for Automapper use the static Mapper object for managing type mappings. For my project, I need to inject an IMapperEngine as part of object

4条回答
  •  不思量自难忘°
    2020-12-31 06:19

    Here's what I ended up with as I couldn't figure out how to set the configuration on Mapper.Engine and have it passed into For().Use.

    public MyRegistry()
    {
        For().Use();
        For().Use();
    
        //type mapping
        For()
            .Singleton()
            .Use(ctx =>
            {
                ITypeMapFactory factory = ctx.GetInstance();
                ConfigurationStore store 
                    = new ConfigurationStore(factory, MapperRegistry.AllMappers());
                IConfiguration cfg = store;
                cfg.AddProfile();
                store.AssertConfigurationIsValid();
                return store;
            });
        For().Use(ctx => ctx.GetInstance());
        For().Use(ctx => ctx.GetInstance());
        For().Singleton().Use();
        For().Use();
    }
    

提交回复
热议问题