0

我确定我缺少一些简单的东西。首先,我将展示我为连接管道而编写的所有代码,然后我将展示异常消息。然后,我将列出我试图解决的问题。

LicenseTrackerProfile

public class LicenceTrackerProfile : Profile
{
    const string LicenceTrackerProfileName = "LicenceTrackerProfile";

    public override string ProfileName
    {
        get { return LicenceTrackerProfileName; }
    }

    protected override void Configure()
    {
        // initialize mappings here
        new ViewModelMappings(this).Initialize();
    }

}

映射器引导程序

public class MapperBootstrapper
{
    public void Configure()
    {
        var profile = new LicenceTrackerProfile();
        AutoMapper.Mapper.Initialize(p => p.AddProfile(profile));
    }

}

映射库

public abstract class MappingBase
{
    private readonly Profile _profile;

    protected MappingBase(Profile profile)
    {
        _profile = profile;
        _profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
        _profile.DestinationMemberNamingConvention = new PascalCaseNamingConvention();
    }

    public Profile Profile
    {
        get { return _profile; }
    }
}

通用映射器

public class UniversalMapper : IUniversalMapper
{
    private readonly IMappingEngine _mappingEngine;

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

    public virtual TDestination Map<TSource, TDestination>(TSource source, TDestination destination)
    {
        return _mappingEngine.Map(source, destination);
    }

}

视图模型映射

public class ViewModelMappings : MappingBase, IMappingInitializer
{
    private readonly Profile _profile;

    public ViewModelMappings(Profile profile) : base(profile)
    {
        _profile = profile;
        _profile.SourceMemberNamingConvention = new PascalCaseNamingConvention();
        _profile.DestinationMemberNamingConvention = new PascalCaseNamingConvention();

    }

    public void Initialize()
    {
        //  data to domain mappings
        Profile.CreateMap<EFDTO.Enums.FileTypes, Domain.FileTypes>();
        Profile.CreateMap<EFDTO.Licence, Domain.Licence>();
        Profile.CreateMap<EFDTO.LicenceAllocation, Domain.LicenceAllocation>();
        Profile.CreateMap<EFDTO.Person, Domain.Person>();
        Profile.CreateMap<EFDTO.Software, Domain.Software>();
        Profile.CreateMap<EFDTO.SoftwareFile, Domain.SoftwareFile>();
        Profile.CreateMap<EFDTO.SoftwareType, Domain.SoftwareType>();
    }
}

请注意,正在调用初始化方法和配置方法,因此它们不会被“遗漏”。

例外

缺少类型映射配置或不支持的映射。

映射类型:软件 -> 软件 LicenceTracker.Entities.Software -> LicenceTracker.DomainEntities.Software

目标路径:软件

源值:LicenceTracker.Entities.Software

疑难解答 忽略列。我计划忽略列,从所有列开始,然后逐个取消忽略它们,直到找到问题列。但是,令我惊讶的是,当我忽略所有列时会发生错误:

Profile.CreateMap<EFDTO.Software, Domain.Software>()
    .ForMember(software => software.Licences, e => e.Ignore())
    .ForMember(software => software.Name, e => e.Ignore())
    .ForMember(software => software.SoftwareFiles, e => e.Ignore())
    .ForMember(software => software.Type, e => e.Ignore())
    .ForMember(software => software.Description, e => e.Ignore())
    .ForMember(software => software.Id, e => e.Ignore())
    .ForMember(software => software.TypeId, e => e.Ignore()
    .ForMember(software => software.ObjectState, e => e.Ignore());

域实体具有 [DataContract](在类级别)和 [DataMember](在方法级别)属性。我也将这些属性中的每一个都添加到了 EF 实体中。

除此之外,我没有想法。这一切似乎都已正确连接。

我错过了什么?

4

1 回答 1

0

我回来英勇地回答我的问题。

问题出在创建 UniversalMapper 对象的服务中(原谅草率的代码,它还不是最终的):

public class LicenceTrackerService : ILicenceTrackerService, IDisposable
{
    LicenceTrackerContext context = new LicenceTrackerContext();
    private MapperBootstrapper mapperBootstrapper;
    private IUniversalMapper mapper = new UniversalMapper(Mapper.Engine);
    private IUnitOfWork unitOfWork;

    public LicenceTrackerService()
    {            
        unitOfWork = new UnitOfWork(context, new RepositoryProvider(new RepositoryFactories()));
        mapperBootstrapper  = new MapperBootstrapper();
        mapperBootstrapper.Configure();

        Database.SetInitializer(new LicenceTrackerInitializer());
        context.Database.Initialize(true);
    }

    public int GetNumber()
    {
        return 42;
    }

    public List<LicenceTracker.DomainEntities.Software> GetSoftwareProducts()
    {
        var productsRepo = unitOfWork.Repository<Software>();

        var list = productsRepo.Query().Select().ToList();

        var softwareList = new List<LicenceTracker.DomainEntities.Software>();

        foreach (var software in list)
        {
            var softwareProduct = new LicenceTracker.DomainEntities.Software();
            softwareList.Add(Mapper.Map(software, softwareProduct));
        }

        return softwareList;
    }

    public void Dispose()
    {
        unitOfWork.Dispose();
    }
}

我仍然不确定为什么,但是在构造函数之外初始化映射器(默认值样式)并不开心。通过将该实例化移动到服务的构造函数中,它起作用了:

    private IUniversalMapper mapper;

    public LicenceTrackerService()
    {
        mapper = new UniversalMapper(Mapper.Engine);
        ...
    }

显然,我不理解静态属性(Mapper.Engine)和默认实例化。

无论如何,没什么大不了的,因为我打算将 UniversalMapper 注入到服务中。

编辑 我实际上已经真正解决了这个问题。这是一个订购的东西。使用 Automapper,在将 Mapper.Engine 插入 UniversalMapper 之前,我必须使用 Profile 初始化映射器。

显然,Mapper.Engine 属性的Get方面不仅仅是对对象的内存引用。是的,快速浏览一下 Automapper 中的代码就证实了这一点。

因此,必须在配置该引擎之后将 Get 属性的结果分配给UniversalMapper的_mappingEngine字段。

于 2014-08-17T06:17:15.473 回答