में ऑटोमैपर कैसे सेट करें मैं .NET पर अपेक्षाकृत नया हूं, और मैंने "पुराने तरीके" सीखने के बजाय .NET कोर से निपटने का निर्णय लिया। मुझे setting up AutoMapper for .NET Core here के बारे में एक विस्तृत लेख मिला, लेकिन क्या नौसिखिया के लिए एक और आसान चलन है?एएसपी.NET कोर
उत्तर
मैंने इसे समझ लिया! यहां विवरण दिया गया है:
- NuGet के माध्यम से अपने समाधान में मुख्य ऑटोमैपर पैकेज जोड़ें।
- NuGet के माध्यम से अपने समाधान में ऑटोमैपर निर्भरता इंजेक्शन पैकेज जोड़ें।
नीचे दिखाए गए
Startup.cs
सेAddAutoMapper()
एक्सटेंशन पर कॉल करें।public void ConfigureServices(IServiceCollection services) { // .... Ignore code before this services.AddMvc(); // Already included. Add your code below this. services.AddAutoMapper(); // <-- This is the line you add. // services.AddAutoMapper(typeof(Startup)); // <-- newer automapper version uses this signature.
}
एक मानचित्रण प्रोफ़ाइल के लिए एक नया वर्ग बनाएँ। (मैंने
MappingProfile.cs
नामक मुख्य समाधान निर्देशिका में एक वर्ग बनाया और निम्न कोड जोड़ें।) मैं एक उदाहरण के रूप मेंUser
औरUserDto
ऑब्जेक्ट का उपयोग करूंगा।public class MappingProfile : Profile { public MappingProfile() { // Add as many of these lines as you need to map your objects CreateMap<User, UserDto>(); CreateMap<UserDto, User>(); } }
कोड में मैप वस्तु आह्वान करने के लिए निम्न की तरह कुछ कार्य करें:
public class UserController : Controller { // Create a field to store the mapper object private readonly IMapper _mapper; // Assign the object in the constructor for dependency injection public UserController(IMapper mapper) { _mapper = mapper; } public async Task<IActionResult> Edit(string id) { // Instantiate source object // (Get it from the database or whatever your code calls for) var user = await _context.Users .SingleOrDefaultAsync(u => u.Id == id); // Instantiate the mapped data transfer object // using the mapper you stored in the private field. // The type of the source object is the first type argument // and the type of the destination is the second. // Pass the source object you just instantiated above // as the argument to the _mapper.Map<>() method. var model = _mapper.Map<User, UserDto>(user); // .... Do whatever you want after that! } }
मुझे आशा है कि यह किसी ASP.NET कोर के साथ नए सिरे से शुरू होने में मदद करता है! मैं किसी भी प्रतिक्रिया या आलोचना का स्वागत करता हूं क्योंकि मैं अभी भी .NET दुनिया में नया हूं!
आप निर्माता को शामिल करना चाहिए MappingProfile –
CreateMap के लिए कॉल को रोकने के लिए के लिए कैसे AutoMapper उपयोग करने के लिए पता है 'मैपिंग प्रोफाइल' वर्ग? –
मैं पूरी तरह से निश्चित नहीं हूँ। यह मेरे लिए किसी और अनुभवी व्यक्ति के लिए एक अच्छा सवाल होगा। लेकिन मुझे लगता है कि वे असेंबली को स्कैन करते हैं जहां इसे प्रोफ़ाइल वर्ग से प्राप्त किसी भी वर्ग के लिए बुलाया जाता है। – theutz
theutz 'जवाब यहाँ बहुत अच्छा है, मैं सिर्फ यह जोड़ना चाहते हैं: अपने मानचित्रण सेटअप, सत्यापित करने के लिए
आप MapperConfigurationExpression
बजाय Profile
से अपने मानचित्रण प्रोफ़ाइल इनहेरिट करते हैं, तो आप बहुत बस एक परीक्षण जोड़कर देख सकते हैं नियंत्रक निर्माता पर IMapper नक्शाकार parrameter निर्दिष्ट करने के लिए कोई जरूरत नहीं है
[Fact]
public void MappingProfile_VerifyMappings()
{
var mappingProfile = new MappingProfile();
var config = new MapperConfiguration(mappingProfile);
var mapper = new Mapper(config);
(mapper as IMapper).ConfigurationProvider.AssertConfigurationIsValid();
}
मुझे एक त्रुटि मिल रही है: "ऑटोमैपर एक्सटेंशन निर्भरता इंजेक्शन एएसपीनेट कोर 1.1 के साथ असंगत है"। कृपया सहायता कीजिए! –
, : हमेशा उपयोगी है।
आप मैपर का उपयोग कर सकते हैं क्योंकि यह कोड के किसी भी स्थान पर एक स्थिर सदस्य है।
public class UserController : Controller {
public someMethod()
{
Mapper.Map<User, UserDto>(user);
}
}
लेकिन सांख्यिकी कुछ विरोधी परीक्षण योग्य हैं, नहीं? –
हाँ। यह कई मामलों में काम करेगा, लेकिन यदि आपके पास परीक्षण में इस विधि का आविष्कार करते समय कोई कॉन्फ़िगर नहीं किया गया है, तो यह एक अपवाद फेंक देगा (और इस प्रकार गलत कारण के लिए परीक्षण में विफल रहा है)। एक इंजेक्शन 'IMapper' के साथ आप उस पर नकल कर सकते हैं और, उदाहरण के लिए, अगर यह दिए गए परीक्षण के लिए अप्रासंगिक है तो इसे वापस शून्य करें। –
मैं ऑटोमैपर 6.1.1 और एएसपीनेट कोर 1.1.2 का उपयोग कर रहा हूं। सबसे पहले ऑटोमैपर प्रोफाइल क्लास द्वारा विरासत में प्राप्त प्रोफाइल कक्षाओं को परिभाषित करें। मैं, उद्देश्य इस प्रकार
public class UserProfile : Profile, IProfile
{
public UserProfile()
{
CreateMap<User, UserModel>();
CreateMap<UserModel, User>();
}
}
Now Create a seperate class e.g Mappings
public class Mappings
{
public static void RegisterMappings()
{
var all =
Assembly
.GetEntryAssembly()
.GetReferencedAssemblies()
.Select(Assembly.Load)
.SelectMany(x => x.DefinedTypes)
.Where(type => typeof(IProfile).GetTypeInfo().IsAssignableFrom(type.AsType()));
foreach (var ti in all)
{
var t = ti.AsType();
if (t.Equals(typeof(IProfile)))
{
Mapper.Initialize(cfg =>
{
cfg.AddProfiles(t); // Initialise each Profile classe
});
}
}
}
}
अब Startup.cs फ़ाइल में MVC कोर वेब परियोजना में
की कक्षाएं लगाने के लिए ही है, IProfile इंटरफ़ेस जो खाली है निर्मित निर्माता कॉल मानचित्रण कक्षा में जो सभी मैपिंग आरंभ होगा आवेदन लोडिंग के समय में
Mappings.RegisterMappings();
आप प्रोफ़ाइल क्लास से केवल एक उपclass बना सकते हैं, और जब प्रोग्राम चल रहा है। AddAutoMapper(); कोड की रेखा ऑटोमैपर स्वचालित रूप से उन्हें जानता है। – isaeid
क्या Arve Systad परीक्षण के लिए उल्लेख किया है पर जोड़ने के लिए।जो भी कारण के लिए तुम मुझे पसंद कर रहे हैं और theutz समाधान में प्रदान की विरासत संरचना को बनाए रखना चाहते हैं, तो आप की तरह MapperConfiguration सेट कर सकते हैं:
var mappingProfile = new MappingProfile();
var config = new MapperConfiguration(cfg =>
{
cfg.AddProfile(mappingProfile);
});
var mapper = new Mapper(config);
मैं NUnit में ऐसा किया।
- 1. एएसपी.NET कोर
- 2. एएसपी.NET कोर
- 3. एएसपी.NET कोर
- 4. एएसपी.NET कोर
- 5. एएसपी.Net कोर
- 6. एएसपी.NET कोर
- 7. एएसपी.NET कोर
- 8. एएसपी.NET कोर
- 9. एएसपी.NET कोर
- 10. एएसपी.Net कोर
- 11. एएसपी.NET कोर
- 12. एएसपी.NET कोर
- 13. एएसपी.NET कोर
- 14. एएसपी.NET कोर IMEMoryCache
- 15. विजुअल स्टूडियो एएसपी.NET कोर
- 16. एएसपी.NET कोर 1.0 एप्लिकेशन
- 17. एएसपी.Net कोर SAML प्रमाणीकरण
- 18. एएसपी.NET कोर डी
- 19. इकाई एएसपी.NET कोर
- 20. एएसपी.NET कोर 2.0
- 21. एएसपी.NET कोर 2.0
- 22. एएसपी.Net कोर एमवीसी
- 23. एएसपी.NET कोर 2
- 24. कुकी प्रमाणीकरण एएसपी.NET कोर
- 25. एएसपी.NET कोर 1.0
- 26. एएसपी.NET कोर 1.1
- 27. एएसपी.Net कोर पहचान
- 28. एएसपी.Net कोर समाधान/प्रोजेक्ट
- 29. एएसपी.NET कोर 1.0, प्रतिरूपण
- 30. एएसपी.NET कोर आरसी 2
https://dotnetcoretutorials.com/2017/09/23/using-automapper-asp-net-core/ देखें –