AutoMapperは、特定のマッピングを一時的に中断するために "説得力"を持たせることができますか?AutoMapperは、特定のマッピングを一時的に中断するために "説得"できますか?
達成しようとしていることを説明するために、私はイラストを使用します。学生、コース、アクティビティ、クラブなどのデータベースオブジェクト(テーブル)と対話するためにLINQを使用するリポジトリStudentRepositoryがあるとします。アプリケーション側では、一致するドメインオブジェクトStudent、Course、Activity、Clubがあります。 Studentクラスのようなアレイ型コース、活動のメンバー、クラブが含ま:
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が1つのファンクションブロック内でマッピングをサスペンドするように説得することは可能ですか?
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
}
NOTE「いくつかの特別な理由ため」:1が1の場合、このhttp://codebetter.com/davidhayden/2007/08/06/linq-to-sql-query-tuning-appears-to-break-down-in-more-advanced-scenarios/どこだろう離れAutoMapperからの制御をしたいことがあり一つの理由:例えば、N団体、LINQ SQLへクエリごとに1:1の関連付けを結合することのみをサポートします。 AutoMapperはここでは非効率的です - N人の生徒のコースをロードするためにN回の呼び出しを行い、同じN人の生徒のアクティビティをロードするためのN個のコールが返されます。
私はこれを試したことはありませんが、「BeforeMap」 'Mapper.CreateMap()。BeforeMap(x => {Mapper.CreateMap ()。ForMember ...}) '前のマップで地図を作成します。マップを何度も再作成しないようにするための追加のチェックがあるかもしれません。 –