1

当我将源对象映射到现有实例时,它似乎想要创建一个新对象而不是复制到...

这是我的测试场景:

List<DomainCustomerProxy> domainCustomers = customers.Select(customer => new DomainCustomerProxy(
                new Lazy<List<DomainContact>>(() => RetrieveContactsByCustomerId(customer.Id, lazyLoad, schemaName).ToList(), LazyThreadSafetyMode.ExecutionAndPublication),
                new Lazy<List<DomainDepartment>>(() => RetrieveDepartmentsByCustomerId(customer.Id, lazyLoad, schemaName).ToList(), LazyThreadSafetyMode.ExecutionAndPublication),
                new Lazy<List<DomainGroup>>(() => RetrieveCustomerGroupsByCustomerId(customer.Id, lazyLoad, schemaName).ToList(), LazyThreadSafetyMode.ExecutionAndPublication)
                    )
            {
                Id = customer.Id,
            }).ToList();

            domainCustomers = Mapper.Map(customers, domainCustomers);

            return domainCustomers;

我收到此错误:类型“Raven.Lib.Domain.CRM.Models.CustomerProxy”没有默认构造函数

public class CustomerProxy : Customer
{
    public CustomerProxy(Lazy<List<Contact>> contacts, Lazy<List<Department>> departments, Lazy<List<Group>> groups)
    {
        _contactLazyList = contacts;
        _departmentLazyList = departments;
        _groupLazyList = groups;
    }

    private readonly Lazy<List<Contact>> _contactLazyList;
    public override List<Contact> Contacts
    {
        get { return _contactLazyList.Value; }
    }

    private readonly Lazy<List<Department>>_departmentLazyList;
    public override List<Department> Departments 
    { 
        get { return _departmentLazyList.Value; } 
    }

    private readonly Lazy<List<Group>> _groupLazyList;
    public override List<Group> CustomerGroups
    {
        get { return _groupLazyList.Value; }
    }
}

客户群:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Number { get; set; }
    public int CreatedById { get; private set; }
    public int ModifiedById { get; private set; }
    public DateTime DateCreated { get; private set; }
    public DateTime DateModified { get; private set; }
    public int Version { get; private set; }

    public virtual List<Contact> Contacts { get; set; }
    public virtual List<Department> Departments { get; set; }
    public virtual List<Group> CustomerGroups { get; set; }

    public SerializableDictionary<string, string> ValidationErrors { get; set; }

    public bool Validate()
    {
        ValidationErrors = new SerializableDictionary<string, string>();

        if (string.IsNullOrWhiteSpace(Name))
        {
            ValidationErrors.Add(LambdaHelper<Customer>.GetPropertyName(x => x.Name), "Customer.Name is a mandatory field.");
        }

        if (Number <= 0)
        {
            ValidationErrors.Add(LambdaHelper<Customer>.GetPropertyName(x => x.Number), "Customer.Number is a mandatory field and can not be 0.");
        }

        return ValidationErrors.Count > 0;
    }
}

现在如果我创建一个空的默认构造函数

protected CustomerProxy(){ }

它没有错误,但似乎清除了我的旧实例就是证明。

映射忽略我不想覆盖的属性。

    CreateMap<DaoCustomer, DomainCustomerProxy>()
        .ForMember(dest => dest.Contacts, opt => opt.Ignore())
        .ForMember(dest => dest.CustomerGroups, opt => opt.Ignore())
        .ForMember(dest => dest.Departments, opt => opt.Ignore())
        .ForMember(dest => dest.ValidationErrors, opt => opt.Ignore());

之前: 在此处输入图像描述 之后: 在此处输入图像描述

我是否错误地使用了自动映射器?domainCustomers = Mapper.Map(customers, domainCustomers);

小背景:我正在使用虚拟代理模式来延迟加载我的联系人、部门和组,因为它们是非常大的列表。这就是我从基类 Customer 继承的原因。

4

0 回答 0