از auto mapper ها برای مپ کردن دو شیء یا ایجاد آبجکتهای دارای دیتا از روی آبجکت دیگر استفاده میکنیم. در بررسی دو مورد از این ابزار با یکدیگر، با ما همراه باشید.
| نام پکیج | تعداد دانلود | آخرین تاریخ آپدیت تا زمان نوشته شدن این مطلب | حجم فایل | توضیحات |
|---|---|---|---|---|
| AutoMapper | 37,160,701 | 2019/04/26 | 335kb |
|
| Mapster | 346,858 | 2019/04/13 | 232kb |
|
| TinyMapper | 139,155 | 2018/6/7 | 94kb | از سال 2015 تاکنون فقط نسخه بتا منتشر کرده است. |
| OoMapper | 8,456 | 2014/6/30 | 107kb | -- |
| EmitMapper | 174,707 | 2011/7/17 | 64kb | تنها همین یک نسخه منتشرشده است. |
| ValueInjecter | 898,512 | 2018/6/1 | 44kb | -- |
Install-package mapster
Tools > NuGet package manager > Manage NuGet packages for solution
public class Person
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public int Age { get; set; }
}
public class LegalInformation : Person
{
public bool IsLegalAge
{
get { return Age > 18; }
}
}
static void SimpleMapping()
{
var person = new Person()
{
Firstname = "joe",
Lastname = "smith",
Age = 30
};
var personWithLegalInfo = person.Adapt<Person, LegalInformation>();
Console.WriteLine(string.Format("Firstname: {0}, Lastname: {1}, Age: {2}, is legal Age: {3}", personWithLegalInfo.Firstname, personWithLegalInfo.Lastname, personWithLegalInfo.Age, personWithLegalInfo.IsLegalAge));
}
static void ConfigMapping()
{
var person = new Person()
{
Firstname = "joe",
Lastname = "smith",
Age = 30
};
var personWithLegalInfo = person.Adapt<person,>(
new TypeAdapterConfig()
.Default
.IgnoreMember((member, side) => (member.Name == "Age" && side == MemberSide.Source))
.EnableNonPublicMembers(true)
.MaxDepth(1)
.IgnoreNullValues(true)
.Config
);
Console.WriteLine(string.Format("Firstname: {0}, Lastname: {1}, Age: {2}, is legal Age: {3}", personWithLegalInfo.Firstname, personWithLegalInfo.Lastname, personWithLegalInfo.Age, personWithLegalInfo.IsLegalAge));
}
</person,>
Install-package automapper
Tools > NuGet package manager > Manage NuGet packages for solution
Mapper.Initialize(cfg => cfg.CreateMap<person,>());
var person = new Person()
{
Firstname = "joe",
Lastname = "smith",
Age = 30
};
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldMapProperty = info => info.GetMethod.IsPublic;
cfg.CreateMap<person,>().MaxDepth(2)
.ForMember(destination => destination.Age, option => option.Condition(source => source.Age < 5)) //will be mapped if source age is less than 5
;
});
var myMapper = new Mapper(config);
var personWithLegalInfo = myMapper.DefaultContext.Mapper.Map<LegalInformation>(person);
Console.WriteLine(string.Format("Firstname: {0}, Lastname: {1}, Age: {2}, is legal Age: {3}", personWithLegalInfo.Firstname, personWithLegalInfo.Lastname, personWithLegalInfo.Age, personWithLegalInfo.IsLegalAge));
static void SimpleMapping()
{
var person = new Person()
{
Firstname = "joe",
Lastname = "smith",
Age = 30
};
var personWithLegalInfo = person.Adapt<Person, LegalInformation>();
Console.WriteLine(string.Format("Firstname: {0}, Lastname: {1}, Age: {2}, is legal Age: {3}", personWithLegalInfo.Firstname, personWithLegalInfo.Lastname, personWithLegalInfo.Age, personWithLegalInfo.IsLegalAge));
}
static void ConfigMappingAutoMapper()
{
var person = new Person()
{
Firstname = "joe",
Lastname = "smith",
Age = 30
};
var config = new MapperConfiguration(cfg =>
{
cfg.ShouldMapProperty = info => info.GetMethod.IsPublic;
cfg.CreateMap<person,>().MaxDepth(2)
.ForMember(destination => destination.Age, option => option.Condition(source => source.Age < 5)) //will be mapped if source age is less than 5
;
});
var myMapper = new Mapper(config);
var personWithLegalInfo = myMapper.DefaultContext.Mapper.Map<LegalInformation>(person);
Console.WriteLine(string.Format("Firstname: {0}, Lastname: {1}, Age: {2}, is legal Age: {3}", personWithLegalInfo.Firstname, personWithLegalInfo.Lastname, personWithLegalInfo.Age, personWithLegalInfo.IsLegalAge));
}