当我一个源对象映射到现有实例似乎要创建一个新的对象,而不是复制到...
这是我的测试方案:
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());
之前: 后:
我使用automapper不正确? domainCustomers = Mapper.Map(客户,domainCustomers);
小回地面:我使用的是虚拟代理模式的延迟加载我的联系人,部门和群体,因为这些都是非常大的列表。 这就是为什么我从客户这是基类继承。