AutoMapper可以"说服"暂时暂停特定的映射吗?

Joh*_*ogo 9 c# linq automapper linq-to-sql

AutoMapper可以"说服"暂时暂停特定的映射吗?

为了说明我想要完成什么,我将使用一个插图.假设我有一个存储库StudentRepository,它使用LINQ与学生,课程,活动,俱乐部等数据库对象(表)进行交互.在应用程序方面,有匹配的域对象Student,Course,Activity,Club.Student类包含类型为Course,Activity和Club的数组成员,如:

public class Student
{
    // ... more members
    public Course[] Courses { get; set; }
    public Activity[] Activities { get; set; }
    public Club[] Clubs { get; set; }
    // ... even more members
}
Run Code Online (Sandbox Code Playgroud)

AutoMapper配置为将数据库对象映射到域对象,其中映射在StudentRepository的静态构造函数中定义,如:

public class StudentRepository : IStudentRepository
{
    static class StudentRepository
    {
        // ... other mappings

        Mapper.CreateMap<TableStudent, Student>()
            .ForMember(dest => dest.Courses, opt => opt.MapFrom(src => Mapper.Map<IEnumerable<Course>>(src.TableCourses)))
            .ForMember(dest => dest.Activities, opt => opt.MapFrom(src => Mapper.Map<IEnumerable<Activity>>(src.TableActivities)))
            .ForMember(dest => dest.Clubs, opt => opt.MapFrom(src => Mapper.Map<IEnumerable<Clubs>>(src.TableClubs)))
        // where TableStudents, TableCourses, TableActivities, TableClubs are database entities

        // ... yet more mappings

    }
}
Run Code Online (Sandbox Code Playgroud)

是否有可能"说服"AutoMapper暂停一个功能块内的映射?例如:

public Student[] GetStudents()
{
    DataContext dbContext = new StudentDBContext();

    var query = dbContext.Students;
    // => SUSPEND CONFIGURATION MAPPINGS for Subjects, Activities and Clubs WHILE STILL making use of others
    // => The idea here it to take personal charge of 'manually' setting the particular members (*for some specific reasons)

    var students = Mapper.Map<Student>(query); // => Still be able to use AutoMapper to map other members
}

public Student[] OtherStudentRepositoryMethods()
{
    // Other repository methods continue to make use of the mappings configured in the static constructor
}
Run Code Online (Sandbox Code Playgroud)

注意" 由于某些特定原因 ":人们可能想要控制远离AutoMapper的一个原因是http://codebetter.com/davidhayden/2007/08/06/linq-to-sql-query-tuning-appears- to-break-down-in-advanced-scenarios / where 在1:n关联的情况下,LINQ to SQL仅支持每个查询加入一个1:n关联.AutoMapper在这里效率很低 - 为N个学生返回N个调用加载课程,N个更多调用加载活动,为相同的N个学生返回,并且可能还有N个调用加载俱乐部为相同的N个学生返回.

And*_*rko 4

是否有可能“说服”AutoMapper 暂停一个功能块内的映射?

据我所知,最好的方法是像这样使用 Ignore()

public class StudentRepository : IStudentRepository
{
    static class StudentRepository
    {
        // ... other mappings

        Mapper.CreateMap<TableStudent, Student>()
            .ForMember(dest => dest.Courses, opt => opt.Ignore())
            .ForMember(dest => dest.Activities, opt => opt.Ignore())
            .ForMember(dest => dest.Clubs, opt => opt.Ignore())
        // where TableStudents, TableCourses, TableActivities, TableClubs are database entities

        // ... yet more mappings

    }
}
Run Code Online (Sandbox Code Playgroud)

另外,正如之前注意到的,我建议您为要实现的每个目标使用不同的配置文件。这是一个示例

public BaseService()
{
    AutoMapperRegistry.Configure();
}

public class AutoMapperRegistry
{
    public static void Configure()
    {
        Mapper.Initialize(x =>
        {
            x.AddProfile<ServiceProfile1>();
            x.AddProfile<ServiceProfileReverseProfile1>();
        });
    }
}

public class ServiceProfile1 : Profile
{
    protected override string ProfileName
    {
        get
        {
            return "ServiceProfile1";
        }
    }

    protected override void Configure()
    {
        Mapper.CreateMap<DataContract_Sub, DTO_Sub>();
        Mapper.CreateMap<DataContract, DTO>()
            .ForMember(x => x.DataContract_Sub, opt => opt.MapFrom(y => y.DTO_Sub))
    .BeforeMap((s, d) => 
            {
                // your custom logic
            })
    .AfterMap((s, d) => 
            {
                // your custom logic
            });
    }
}
Run Code Online (Sandbox Code Playgroud)