1

我收到以下错误

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,以便我可以在我的单元测试中模拟它。没有它,我将不得不 bin automapper

4

0 回答 0