Automapper Not Mapping mapping properties

你说的曾经没有我的故事 提交于 2020-03-05 00:23:36

问题


I was working on Generics and I got stuck into weird issue. I am facing issue with AutoMapper-9 mapping. I have implemented ICollection but Automapper is not mapping the properties correctly. My code is

CollectionBase class which is implementing ICollection

public class CollectionBase<T> : ICollection<T>
{
    ICollection<T> _items;
    public CollectionBase()
    {
        _items = new List<T>();
    }
    protected CollectionBase(ICollection<T> collection)
    {
        // Let derived classes specify the exact type of ICollection<T> to wrap.
        _items = collection;
    }
    public string FullName { get; set; }
    public bool Enabled { get; set; }
    public int Count
    {
        get
        {
            return _items.Count;
        }
    }

    public bool IsReadOnly
    {
        get
        {
            return false;
        }
    }

    public void Add(T item)
    {
        _items.Add(item);
    }

    public void Clear()
    {
        _items.Clear();
    }

    public bool Contains(T item)
    {
        return _items.Contains(item);
    }

    public void CopyTo(T[] array, int arrayIndex)
    {
        _items.CopyTo(array, arrayIndex);
    }

    public IEnumerator<T> GetEnumerator()
    {
        return _items.GetEnumerator();
    }

    public bool Remove(T item)
    {
        return _items.Remove(item);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _items.GetEnumerator();
    }
}

ChildrenElements

public class ChildrenElements
{
    public string SchoolName { get; set; }
    public string CityName { get; set; }
}

ChildrenCollection -> inherited by CollectionBase

public class ChildrenCollection : CollectionBase<ChildrenElements>
{
    public ChildrenCollection()
    {
    }
    public int DefaultValue { get; set; }
}

Children Class

public class Children : IChildren
{
    public Children()
    {
    }
    public Children(IChildren children, bool flag = true)
    {
        Name = "Test";
        FatherName = "TestFather";
        childrenCollection = new ChildrenCollection() { DefaultValue = -1, FullName = "Children Name 1", Enabled = false };
        if (children != null)
        {
                /////////////////////////////////////////////////////////////////////
                ////            Using Generic Mapper Configuration                ///
                /////////////////////////////////////////////////////////////////////
                var mapper = ObjectMapperFactory.CreateMapper<IChildren, IChildren>();
                mapper.Map(children, this);

        }
    }
    public string Name { get; set; }
    public string FatherName { get; set; }
    public ChildrenCollection childrenCollection { get; set; }
}

Object Mapper Factory for AutoMapper

public static class ObjectMapperFactory
{
    public static IMapper CreateMapper<TSource, TDestination>()
    {
        var config = new MapperConfiguration(
            cfg =>
            {
                cfg.CreateMap<TSource, TDestination>()
                .PreserveReferences()
                cfg.Advanced.AllowAdditiveTypeMapCreation = true;
                cfg.AllowNullDestinationValues = true;
                cfg.AllowNullCollections = true;
            });

        config.AssertConfigurationIsValid();
        return config.CreateMapper();
    }
}

Mapper is not mapping properties inside "ChildrenCollection" class. It is mapping "ChildrenElements" correctly but ChildrenCollection class properties are not mapping correctly like DefaultDays, Enabled etc.

Thanks in advance.

来源:https://stackoverflow.com/questions/60212096/automapper-not-mapping-mapping-properties

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