使用带有DI容器的AutoMapper实例化类型 [英] Instantiating types using AutoMapper with a DI container

查看:185
本文介绍了使用带有DI容器的AutoMapper实例化类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见下面的代码:

public class Test : ITest
    {
        public ITest2 _iTest2;
        public int _id;
        public string _name;

        public Test(ITest2 test2)
        {
            _iTest2 = test2;
        }
    }

    public interface ITest
    {
    }

    public class Test2 : ITest2
    {
    }

    public interface ITest2
    {

    }

    public class Test3 : ITest3
    {
        public int _id;
        public string _name;
    }

    public interface ITest3
    {

    }

我的Global.asax中包含以下内容:

I have the following in my Global.asax:

Mapper.Initialize(m =>
            {  
 m.CreateMap<DataLayer.Test3, BusinessLayer.Test>().ConstructUsing(opt => new BusinessLayer.Test(new BusinessLayer.Test2()));
});

我可以在客户端应用程序中映射类型:

I can map the types in my client app doing this:

cfg.CreateMap<DataLayer.Test3, BusinessLayer.Test>().ConstructUsing(opt => new BusinessLayer.Test(new BusinessLayer.Test2()));

如何使用Castle Windsor映射类型,而不必为Test and Test2使用new关键字?

How can I map the types using Castle Windsor instead of having to use the new keyword for Test and Test2?

我阅读了另一个答案,有人建议这样做:

I read another answer and someone suggested doing this:

 public void Install(IWindsorContainer container, IConfigurationStore store)
    {

        container.Register(Types.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn(typeof(IValueResolver<,,>)));
        // container.Register(Types.FromAssembly(Assembly.GetExecutingAssembly()).BasedOn<IValueResolver>());
        container.Register(Types.FromThisAssembly().BasedOn<Profile>().WithServiceBase());
        var profiles = container.ResolveAll<Profile>();

        // Add your list of profiles to the mapper configuration here
        Mapper.Initialize(m => {
            m.ConstructServicesUsing(container.Resolve);
            profiles.ToList().ForEach(p => m.AddProfile(p));
        });

        // I'm not sure about this as I haven't used AutoMapper for a while,
        // but I assume you want to resolve the static mapper instance
        container.Register(Component.For<IMapper>().Instance(Mapper.Instance));
    }

我必须这样做吗?

cfg.CreateMap<DataLayer.Test3, BusinessLayer.Test>().ConstructUsing(opt => new BusinessLayer.Test(new BusinessLayer.Test2()));

或者AutoMapper应该能够使用以下方式映射类型:

or should AutoMapper be able to map the types using this:

cfg.CreateMap<DataLayer.Test3, BusinessLayer.Test>()

推荐答案

为了使AutoMapper使用Windsor创建目标类型,您需要配置以下两项:

In order to get AutoMapper to use Windsor to create the target type, you need to configure two things:

  1. 告诉AutoMapper使用温莎构建服务
  2. 告诉AutoMapper(每次映射)以实际使用上述配置

  1. Tell AutoMapper to construct services using Windsor
  2. Tell AutoMapper (per-mapping) to actually use the above configuration

 var container = new WindsorContainer();

    Mapper.Initialize(m =>
    {
        m.ConstructServicesUsing(container.Resolve);

        m.CreateMap<Test3, ITest>().ConstructUsingServiceLocator(); // This is important!

    });

    container.Register(Component.For<ITest>().ImplementedBy<Test>());
    container.Register(Component.For<ITest2>().ImplementedBy<Test2>());
    container.Register(Component.For<ITest3>().ImplementedBy<Test3>());

    var test3 = new Test3();
    var test1 = Mapper.Instance.Map<Test3, ITest>(test3);

这篇关于使用带有DI容器的AutoMapper实例化类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆