DTO
DTO stands for Data Transfer Object. DTOs are there for transfer data between the different application or different layers within a single application. You can look at them as dump bags of information. The purpose of existence is just getting that information to a recipient. So that recipient doesn't get anything other than they expect.
The major problem that I had with this DTO is mapping one object to another. Every place where I need to transfer my data using DTO, either I have to use parameterized constructor or new object initialize to inject the information (data) into it.
The major problem that I had with this DTO is mapping one object to another. Every place where I need to transfer my data using DTO, either I have to use parameterized constructor or new object initialize to inject the information (data) into it.
Recently I found this library called AutoMapper which made my code awesome. AutoMapper is a simple little library built to solve a deceptively complex problem "getting rid of code that mapped one object to another".
Like any other feature, AutoMapper has to configure before using it. Configure in the sense that you need to create your mappings. No mappings, No magic.
Initially, you are marking that source object should map with destination object with required members. After that when you ask AutoMapper to map source object to destination object. AutoMapper will do it for you. because it knows which member should map with what
Most important three points you need to know about configuration are How, When and Where.
AutoMapper
It all started with Source Object and Destination Object to work with (well, that's obvious!). This works best as long as the names of the source type's members match up to the destination type's members. If you have "FirstName" in the source type, this will automatically be mapped to a destination type member called "FirstName". Don't worry even if you don' have match up names, still can be done.Getting start with AutoMapper
To perform this magic, have to cast a spell ;)Like any other feature, AutoMapper has to configure before using it. Configure in the sense that you need to create your mappings. No mappings, No magic.
Initially, you are marking that source object should map with destination object with required members. After that when you ask AutoMapper to map source object to destination object. AutoMapper will do it for you. because it knows which member should map with what
Most important three points you need to know about configuration are How, When and Where.
How
AutoMapper provides two APIs: a static API or an instance API.When
Instance API allows you to change the configuration at the run time. That caused many problems, so the new Static API does not allow you to do this. You have to configure everything in one place.Where
Instance Mapper can be placed before you place map (Line before map will do). But Static Mapper should only happen once per AppDomain. That means the best place to put the configuration code is in application startup, such as the Global.asax file for ASP.NET applications.Demo
// Source
public class Person
{
public string FirstName { get; set; }
}
// Destination
public class PersonDTO
{
public string FirstName { get; set; }
}
The static API (Recommended):
the instance API:
// Configuration
Mapper.Initialize(cfg => {
cfg.CreateMap<Person, PersonDTO>();
});
// Map to
PersonDTO personDTO = Mapper.Map<PersonDTO>(new Person());
the instance API:
// Configuration
var config = new MapperConfiguration(cfg => {
cfg.CreateMap<Person, PersonDTO>();
});
// Create a mapper instance
var mapper = config.CreateMapper();
//// OR
var mapper = new Mapper(config);
// Map to
PersonDTO personDTO = mapper.Map<PersonDTO>(new Person());
May the force be with you!
Comments
Post a Comment