क्या ऑटोमैपर अस्थायी रूप से विशेष मैपिंग को निलंबित करने के लिए "राजी" हो सकता है?क्या ऑटोमैपर अस्थायी रूप से विशेष मैपिंग को निलंबित करने के लिए "राजी" हो सकता है?
यह समझाने के लिए कि क्या पूरा करने की कोशिश कर रहा हूं, मैं एक उदाहरण का उपयोग करूंगा। मान लीजिए कि मेरे पास एक भंडार है, छात्र रिपोजिटरी, जो कि डेटाबेस, पाठ्यक्रम, क्रियाकलाप, क्लब इत्यादि जैसे डेटाबेस ऑब्जेक्ट्स (तालिकाओं) के साथ इंटरैक्ट करने के लिए LINQ का उपयोग करता है। आवेदन पक्ष पर छात्र डोमेन, छात्र, गतिविधि, क्लब मिलान कर रहे हैं। छात्र वर्ग की तरह प्रकार कोर्स, गतिविधि की सरणी के सदस्यों, और क्लब में शामिल हैं:
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
}
}
क्या एक फ़ंक्शन ब्लॉक में मैपिंग को निलंबित करने के लिए ऑटोमैपर को "मनाने" संभव है?
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 एसक्यूएल के लिए केवल एक 1: एन एसोसिएशन प्रति क्वेरी में शामिल होने का समर्थन करता है। ऑटोमैपर यहां अक्षम होगा - एन छात्रों को एन छात्रों के लिए पाठ्यक्रम लोड करने के लिए एन कॉल करने के लिए, एन एन कॉल करने के लिए एन कॉल करने के लिए एन एन कॉल वापस लौटे, और संभवतः एन एन छात्रों के लिए क्लब लोड करने के लिए एन और कॉल वापस लौटे।
मैंने यह कोशिश नहीं की है, लेकिन 'प्रीमैप' मैपर है। क्रेटमैप <टेबलस्ट्यूडेंट, छात्र>()। मैपमैप (एक्स => {मैपर.क्रेटमैप <टेबलस्ट्यूडेंट, छात्र>()। ForMember ...}) पहले मानचित्र में नक्शा बनाएं। नक्शा कई बार फिर से बनाने के लिए अतिरिक्त चेक हो सकते हैं। –