AutoMapper is a mapper between two objects. It maps two different entities by transforming an input object of one type to an output object of another type. It is very tough job to map two different entities and sometime it is even more hectic when it comes to testing. It can be anywhere in the application but in general it happens in UI/Domain or Service/Domain layers.
* Need to download from NuGet package manager, open the NuGet console and enter the following command to install the AutoMapper library:
public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public class TestAutoMapperTwo
{
public int Id { get; set; }
public string FullName { get; set; }
public string Address { get; set; }
}
* Now we have create one more class , where we can create Map for our class .
using AutoMapper;
namespace TestDemo
{
public class TestMapper : Profile // this is inherit from AutoMapper dll to create Mapper
{
public TestMapper()
{
CreateMap<TestAutoMapperOne, TestAutoMapperTwo>()
.ForMember(des => des.FullName, map => map.MapFrom(src => src.Name));
CreateMap<TestAutoMapperTwo, TestAutoMapperOne>()
.ForMember(des => des.Name, map => map.MapFrom(src => src.FullName)); // this for Reverse mapping or we can also call it in below line like that
//************************OR***************************//
CreateMap<TestAutoMapperOne, TestAutoMapperTwo>()
.ForMember(des => des.FullName, map => map.MapFrom(src => src.Name)).ReverseMap();
* ForMember=> We have different name in property class that we have to map our class property in ForMember() of mapping.
.ForMember(des => des.FullName, map => map.MapFrom(src => src.Name));
// First class will we destination and second class will be source
For ex-> public class TestAutoMapperOne
{
public string Name { get; set; }
}
public class TestAutoMapperTwo
{
public string FullName { get; set; }
}
* Now start mapping our class in Main method
public static void Main()
{
Console.WriteLine("Start AutoMapper!");
var test = new TestAutoMapperOne()
{
Name = "Test",
Id = 1,
Address = "Bnagalore"
};
var config = new MapperConfiguration(cfg => cfg.AddProfile(new TestMapper()));
// Here i have called my above class constructor "TestMapper" to configuration mapper
var mapper = config.CreateMapper();
var item = mapper.Map<TestAutoMapperTwo>(test);
* As first class inside Map its class "TestAutoMapperTwo" where we are going to map with TestAutoMapperOne object .
var item1 = mapper.Map<TestAutoMapperOne>(item);
// its for ReverseMap class TestAutoMapperOne to TestAutoMapperTwo
Console.ReadLine();
}
* Need to download from NuGet package manager, open the NuGet console and enter the following command to install the AutoMapper library:
PM> Install-Package AutoMapper
Now create two class with same data type property in it but we can have different name , for different name we have to make changes in mapper class . i have declared below =>
public class TestAutoMapperOne
{public int Id { get; set; }
public string Name { get; set; }
public string Address { get; set; }
}
public class TestAutoMapperTwo
{
public int Id { get; set; }
public string FullName { get; set; }
public string Address { get; set; }
}
* Now we have create one more class , where we can create Map for our class .
using AutoMapper;
namespace TestDemo
{
public class TestMapper : Profile // this is inherit from AutoMapper dll to create Mapper
{
public TestMapper()
{
CreateMap<TestAutoMapperOne, TestAutoMapperTwo>()
.ForMember(des => des.FullName, map => map.MapFrom(src => src.Name));
CreateMap<TestAutoMapperTwo, TestAutoMapperOne>()
.ForMember(des => des.Name, map => map.MapFrom(src => src.FullName)); // this for Reverse mapping or we can also call it in below line like that
//************************OR***************************//
CreateMap<TestAutoMapperOne, TestAutoMapperTwo>()
.ForMember(des => des.FullName, map => map.MapFrom(src => src.Name)).ReverseMap();
* ForMember=> We have different name in property class that we have to map our class property in ForMember() of mapping.
.ForMember(des => des.FullName, map => map.MapFrom(src => src.Name));
// First class will we destination and second class will be source
For ex-> public class TestAutoMapperOne
{
public string Name { get; set; }
}
public class TestAutoMapperTwo
{
public string FullName { get; set; }
}
* Now start mapping our class in Main method
public static void Main()
{
Console.WriteLine("Start AutoMapper!");
var test = new TestAutoMapperOne()
{
Name = "Test",
Id = 1,
Address = "Bnagalore"
};
var config = new MapperConfiguration(cfg => cfg.AddProfile(new TestMapper()));
// Here i have called my above class constructor "TestMapper" to configuration mapper
var mapper = config.CreateMapper();
var item = mapper.Map<TestAutoMapperTwo>(test);
* As first class inside Map its class "TestAutoMapperTwo" where we are going to map with TestAutoMapperOne object .
var item1 = mapper.Map<TestAutoMapperOne>(item);
// its for ReverseMap class TestAutoMapperOne to TestAutoMapperTwo
Console.ReadLine();
}
No comments:
Post a Comment