9

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

为了说明要完成的工作,我将使用一个插图。假设我有一个存储库StudentRepository,它使用 LINQ 与数据库对象(表)进行交互,例如学生、课程、活动、俱乐部等。在应用程序端,有匹配的域对象学生、课程、活动、俱乐部。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
}

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

    }
}

是否可以“说服” 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
}

注意“出于某些特定原因”:可能希望从 AutoMapper 中控制权的一个原因是http://codebetter.com/davidhayden/2007/08/06/linq-to-sql-query-tuning-appears- to-break-down-in-more-advanced-scenarios/1:n 关联的情况下,LINQ to SQL 仅支持每个查询加入一个 1:n 关联。AutoMapper 在这里效率低下 - 为返回的 N 名学生调用 N 次加载课程,为返回的相同 N 名学生再次调用 N 次加载活动,并且可能为返回的相同 N 名学生再次调用 N 次加载俱乐部。

4

3 回答 3

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

    }
}

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

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
            });
    }
}
于 2012-05-28T09:37:27.897 回答
2

实现此目的的一种方法是为每个场景创建单独的映射引擎实例,这样您就可以配置不同的映射,正如Jimmy Bogard 关于希望以不同方式映射单一类型的答案中所建议的那样。

于 2012-05-28T08:58:26.897 回答
0

嗯...谢谢大家的反馈。我花时间考虑所有的答案和建议。尽管它们提供了很多值得深思的东西,但没有一个特别能很好地呈现出来。我想我应该注入一些我尝试过的东西。(免责声明:我认为这是一种肮脏的方法——很多事情都可能出错——墨菲定律继续有效)。您可以利用特定实例中的忽略功能来“暂停”映射。通常,在 try and catch 块中,如下所示:

public Student[] GetStudents()
{
    try
    { // Suspend/Ignore the mappings
        // => SUSPEND CONFIGURATION MAPPINGS for Subjects, Activities and Clubs
        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())

        DataContext dbContext = new StudentDBContext();

        var query = dbContext.Students;

        // other logic ...

        var students = Mapper.Map<Student>(query); // => Still be able to use AutoMapper to map other members
        // set the properties you needed to do manually
    }
    finally // Restore back the 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)))
    }
}

就像我提到的,它可能很脏。不是我很乐意编写的那种代码——特别是因为我不知道如果 CreateMap() 在 finally 块中失败会出现什么样的异常情况,但是在你无法彻底检查方法的遗留应用程序上——可能使用上面@AndriyZakharko 建议的不同配置文件,您可以使用它来暂时恢复控制权。我亲自试了一下。

于 2012-06-04T08:46:26.543 回答