Automapper - Not copying from source to destination object correctly. Errors need default constructor

别来无恙 提交于 2020-01-06 04:15:46

问题


When I map a source object into an existing instance it appears to want to create a new object and not copy into...

Here is my test scenario:

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;

I get this error: Type 'Raven.Lib.Domain.CRM.Models.CustomerProxy' does not have a default constructor

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; }
    }
}

customer base class:

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;
    }
}

Now if I create a empty default constructor

protected CustomerProxy(){ }

It does not error but the appears to wipe out my old instance here is the proof.

Mapping ignoring the properties I dont want overwritten.

    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());

Before:

After:

Am I using automapper incorrectly? domainCustomers = Mapper.Map(customers, domainCustomers);

Little back ground: I am using the virtual proxy pattern for lazy loading my contacts,departments, and groups as these are very large lists. This is why I inherit from Customer which is the base class.

来源:https://stackoverflow.com/questions/20099405/automapper-not-copying-from-source-to-destination-object-correctly-errors-nee

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!