首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >注射AutoMapper

注射AutoMapper
EN

Stack Overflow用户
提问于 2009-11-12 03:37:20
回答 3查看 3.8K关注 0票数 8

我一直致力于将AutoMapper注入控制器。我喜欢代码营服务器的实现。它为AutoMapper的IMappingEngine创建了一个包装器。依赖注入是使用StructureMap完成的。但我得用温莎城堡做我的项目。那么,如何使用Windsor实现下面的依赖注入和设置呢?我不想在温莎城堡中逐行实现。如果你想这样做,请放心。相反,什么是温莎等价的结构地图的注册表和配置文件?我需要配置文件来定义CreateMap<>,如下所示。

谢谢。

会议控制器

代码语言:javascript
复制
public MeetingController(IMeetingMapper meetingMapper, ...)

会议地图

代码语言:javascript
复制
public class MeetingMapper : IMeetingMapper
{

    private readonly IMappingEngine _mappingEngine;

    public MeetingMapper(IMappingEngine mappingEngine)
    {
      _mappingEngine = mappingEngine;
    }

    public MeetingInput Map(Meeting model)
    {
        return _mappingEngine.Map<Meeting, MeetingInput>(model);    
    }

    ......
}

自动地图登记处:

代码语言:javascript
复制
public class AutoMapperRegistry : Registry
{

    public AutoMapperRegistry()
    {
        ForRequestedType<IMappingEngine>().TheDefault.Is.ConstructedBy(() => Mapper.Engine);
    }
}

会议地图简介:

代码语言:javascript
复制
public class MeetingMapperProfile : Profile
{

    public static Func<Type, object> CreateDependencyCallback = (type) => Activator.CreateInstance(type);

    public T CreateDependency<T>()
    {
        return (T)CreateDependencyCallback(typeof(T));
    }

    protected override void Configure()
    {
        Mapper.CreateMap<MeetingInput, Meeting>().ConstructUsing(
            input => CreateDependency<IMeetingRepository>().GetById(input.Id) ?? new Meeting())

       .ForMember(x => x.UserGroup, o => o.MapFrom(x => x.UserGroupId))
       .ForMember(x => x.Address, o => o.Ignore())
       .ForMember(x => x.City, o => o.Ignore())
       .ForMember(x => x.Region, o => o.Ignore())
       .ForMember(x => x.PostalCode, o => o.Ignore())
       .ForMember(x => x.ChangeAuditInfo, o => o.Ignore());
    }
}
EN

回答 3

Stack Overflow用户

发布于 2009-11-12 08:38:04

你是说你是怎么在温莎注册的?

你可能要注册FactorySupportFacility拳头..。我现在没办法查。

代码语言:javascript
复制
container.AddFacility<FactorySupportFacility>();

然后

代码语言:javascript
复制
container.Register(Component.For<IMappingEngine>().UsingFactoryMethod(()=>
            Mapper.Engine));
票数 3
EN

Stack Overflow用户

发布于 2012-09-26 14:57:45

我不熟悉Castle,但是这里是StructureMap语法。您需要设置您的注册表有点不同。不必将IMappingEngine设置为Mapper.Engine,您必须配置更多的接口。这是一个稍微多一点的工作,但这将允许您设置配置文件作为注册的一部分。

代码语言:javascript
复制
public AutoMapperRegistry()
{
    //type mapping
    For<ConfigurationStore>()
        .Singleton()
        .Use(ctx =>
        {
            ITypeMapFactory factory = ctx.GetInstance<ITypeMapFactory>();
            ConfigurationStore store 
                = new ConfigurationStore(factory, MapperRegistry.AllMappers());
            IConfiguration cfg = store;
            //Here's where you load your profile
            cfg.AddProfile<MeetingMapperProfile>();
            store.AssertConfigurationIsValid();
            return store;
        });
    For<IConfigurationProvider>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
    For<IConfiguration>().Use(ctx => ctx.GetInstance<ConfigurationStore>());
    For<IMappingEngine>().Use<MappingEngine>();
    For<ITypeMapFactory>().Use<TypeMapFactory>();
}
票数 2
EN

Stack Overflow用户

发布于 2013-12-13 18:49:02

我意识到这有点老了,但是我使用了Castle,使用安装程序加载AutoMapper配置文件是相当容易的:

代码语言:javascript
复制
using System.Linq;
using System.Reflection;

using AutoMapper;

using Castle.MicroKernel.Registration;
using Castle.MicroKernel.SubSystems.Configuration;
using Castle.Windsor;

namespace YourNamespace
{
    public class AutoMapperInstaller : IWindsorInstaller
    {
        public void Install(IWindsorContainer container, IConfigurationStore store)
        {
            Mapper.Initialize(m => m.ConstructServicesUsing(container.Resolve));

            container.Register(Types.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IValueResolver>());
            container.Register(Types.FromThisAssembly().BasedOn<Profile>().WithServiceBase());
            var profiles = container.ResolveAll<Profile>();
            profiles.ToList().ForEach(p => Mapper.AddProfile(p));

            container.Register(Component.For<IMappingEngine>().Instance(Mapper.Engine));
        }
    }
}

此安装程序将获取问题中显示的MeetingMapperProfile,或基于AutoMapper的Profile的任何其他类。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1719725

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档