尝试使用automapper和城堡windsor注入进行映射时抛出异常

时间:2013-04-09 16:52:37

标签: c# inversion-of-control castle-windsor automapper

我收到以下错误

Missing type map configuration or unsupported mapping.

Mapping types:
RegistrationViewModel -> Member

这是我的模特

public class Member
{
    public int ID { get; set; }
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Gender Gender { get; set; }
    public int AddressId { get; set; }
    public virtual Address Address { get; set; }
    public DateTime DateCreated { get; set; }
}

public class RegistrationViewModel
{
    public string EmailAddress { get; set; }
    public string Password { get; set; }
    public string ConfirmPassword { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Postcode { get; set; }
    public Title Title { get; set; }
}

这是我的映射器

public class MemberMapper : Profile
{
    protected override void Configure()
    {
        Mapper.CreateMap<RegistrationViewModel, Member>()
            .ForMember(x => x.Address, y => y.Ignore())
            .ForMember(x => x.Gender, y => y.Ignore());
    }
}

这是我的windsor安装程序(我从github复制而来)

public class AutoMapperInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(
            Component.For<ConfigurationStore>()
                .OnlyNewServices()
                .ImplementedBy<ConfigurationStore>()
                // .Properties(PropertyFilter.IgnoreAll) // Uncomment this line to fix the problem (https://groups.google.com/d/msg/castle-project-users/FotV-tBtGiQ/id31pps10EsJ)
                .DependsOn(
                    Property.ForKey<ITypeMapFactory>().Eq(new TypeMapFactory()),
                    Property.ForKey<IEnumerable<IObjectMapper>>().Eq(MapperRegistry.AllMappers())
                ),
            Component.For<IConfigurationProvider>()
                .OnlyNewServices()
                .UsingFactoryMethod(kernel => kernel.Resolve<ConfigurationStore>())
                .LifeStyle.Singleton,
            Component.For<IConfiguration>()
                .OnlyNewServices()
                .UsingFactoryMethod(kernel => kernel.Resolve<ConfigurationStore>())
                .LifeStyle.Singleton,
            Component.For<IMappingEngine>()
                .OnlyNewServices()
                .ImplementedBy<MappingEngine>()
                .LifeStyle.Singleton,
            Classes.FromAssemblyContaining<MemberMapper>()
                .BasedOn<Profile>().WithServiceBase()
            );

        var profiles = container.ResolveAll<Profile>();
        var config = container.Resolve<IConfiguration>();

        foreach (var profile in profiles)
        {
            config.AddProfile(profile);
        }
    }

我正在努力弄清楚这里出了什么问题,我称之为IMappingEngine的方法看起来不错,并且在查看调试对象资源管理器时并没有遗漏任何明显的东西。

public MemberService(IGenericRepository<Member> repository, IMappingEngine mapper) :
        base(repository)
{
    this.mapper = mapper;
}

public void SaveNewUser(RegistrationViewModel registrationViewModel)
{
    var newMember = mapper.Map<Member>(registrationViewModel);

    ...
}

我觉得我可能会大量过度复杂,但我真的希望能够注入IMapperEngine以便我可以在单元测试中模拟出来。没有它,我将不得不加入automapper

0 个答案:

没有答案