AutoMapper is a productivity tool designed to help you write less repetitive code mapping code. AutoMapper maps objects to objects, using both convention and configuration. AutoMapper is flexible enough that it can be overridden so that it will work with even the oldest legacy systems.
AutoMapper is a popular object-to-object mapping library that can be used to map objects belonging to dissimilar types.
As an example, you might need to map the DTOs (Data Transfer Objects) in your application to the model objects.
To start working with AutoMapper, you should create a project in Visual Studio and then install AutoMapper.
Install AutoMapper from NuGet.
Automapper is a simple reusable component which helps to copy data from object type to other.
1) Projection-working:-
var config = new MapperConfiguration(cfg => {
cfg.CreateMap
});
IMapper iMapper = config.CreateMapper();
var source = new AuthorModel();
source.Id = 1;
source.FirstName = "Sibin";
source.LastName = "Thomas";
source.Address = "Kodiyan(H)";
var sourcedto = new AuthorDTO();
sourcedto.City = "Thrissur";
sourcedto.State = "Kerala";
sourcedto.Country = "India";
var destination = iMapper.Map
Console.WriteLine("FirstName: " + destination.FirstName + "City: " + destination.City);
2) Flattening-working:-
var customer = new Customer
{
Name = "George Costanza"
};
var order = new Order
{
Customer = customer
};
var bosco = new Product
{
Name = "Bosco",
Price = 4.99m
};
order.AddOrderLineItem(bosco, 15);
var configuration = new MapperConfiguration(cfg => cfg.CreateMap
IMapper mapper = configuration.CreateMapper();
OrderDto dto = mapper.Map
dto.CustomerName.Equals("George Costanza");dto.Total.Equals(74.85m);
No comments:
Post a Comment