在AfterMap中如何避免手动映射

时间:2018-11-01 04:15:57

标签: c# .net automapper

具有类 SourceClass和DestinationClass

的特定类型的映射问题
public class SourceClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<SourceNestedClass> SourceNestedClasses { get; set; }
}

public class SourceNestedClass
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}


public class DestNestedClass
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }
}

public class DestinationClass
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<DestNestedClass> Nesteds { get; set; }
}

public DestinationClass MapAutomatically(SourceClass source)
{

        Mapper.CreateMap<SourceClass, DestinationClass>()
            .ForMember(dest => dest.Nesteds, opt => opt.Ignore())
            .AfterMap((src, dest) =>
            {
                dest.Nesteds.Clear();

                foreach (var nested in src.SourceNestedClasses )
                {
                    DestNestedClass destNest=new DestNestedClass();
                    destNest.Id=nested.Id;
                    destNest.FirstName=nested.FirstName;
                    destNest.LastName=nested.LastName;
                    destNest.FullName=nested.FullName;

                    dest.Nesteds.Add(destNest);
                }
            });

        return Mapper.Map<DestinationClass>(source);

}

想避免在自动映射中使用手动映射选项。在此过程中,需要映射每个属性。为避免手动映射,存在使用波纹管语法的任何过程。

Mapper.CreateMap<SourceNestedClass, DestNestedClass>()
    .ForMember(dest => dest.FullName, opt => opt.FirstName+opt.LastName);

1 个答案:

答案 0 :(得分:1)

语法是描述性的,对于使用 automapper 的嵌套类映射,需要注意父类和子类,如下所示:

-使用嵌套类映射父类

-映射类

    public class SourceClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<SourceNestedClass> SourceNestedClasses { get; set; }
        public SourceClass()
        {
            SourceNestedClasses = new List<SourceNestedClass>();
        }
    }

    public class SourceNestedClass
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
    }


    public class DestNestedClass
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName { get; set; }
    }

    public class DestinationClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public ICollection<DestNestedClass> Nesteds { get; set; }

        public DestinationClass()
        {
            Nesteds = new List<DestNestedClass>();
        }
    }

    class Program
    {


        public static DestinationClass MapAutomatically(SourceClass sourceInfo)
        {


            var config = new MapperConfiguration(cfg =>
            {
                //mapping parent classes with nested classes
                cfg.CreateMap<SourceClass, DestinationClass>()
                .ForMember(dest => dest.Nesteds, opt => opt.MapFrom(source => source.SourceNestedClasses));

                //mapping classes
                cfg.CreateMap<SourceNestedClass, DestNestedClass>()
                .ForMember(dest => dest.FullName, opt => opt.MapFrom(source => source.FirstName + source.LastName));
            });

            var mapper = config.CreateMapper();
            var destResult = mapper.Map<SourceClass, DestinationClass>(sourceInfo);

            return destResult;

        }


        static void Main(string[] args)
        {

            var sourceInfo = new SourceClass
            {
                Id = 5,
                Name = "SourceClass",
                SourceNestedClasses = { new SourceNestedClass { Id = 10, FirstName = "Test", LastName = "Address" } }
            };

            var destResult = MapAutomatically(sourceInfo);
        }
    }
}