Whenever you are designing your project solution using Visual Studio, you might think of where can I declare, initialise and use AutoMapper.
I tend to create projects in below way:
- <ProjectSolution> => This is your presentation layer. This could be your MVC project or Console application.
- <ProjectSolution>.Business => This is your business layer, a class library.
- <ProjectSolution>.DataAccess => This is your data access layer, a class library.
- <ProjectSolution>.Helper => This is your helper library layer, a class library.
- <ProjectSolution>.Domain => This will contain all domain models, simply database table objects, a class library.
- <ProjectSolution>.ViewModels => This will contain all view models, simply, objects that has representation in your user interface, a class library.
- <ProjectSolution>.Infrastructure => This contains all other important bits that helps your program extending different aspects like Object Mapping, Dependency Injection, Extended attributes etc..
Please note that the above structure may not fit if you are thinking to have open and load balanced endpoints [as service]. But this is close to all concepts.
I tend to use AutoMapper library in "<ProjectSolution>.Infrastructure" class library. You can right click on the project and open Nuget Package Manager. Search for AutoMapper and install it. This will add a reference called AutoMapper.
Now in the Infrastructure project,
Create a class file called AutoMapperConfig.cs
Add the following code
public class AutoMapperConfig
{
public static void CreateMappings()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Category, CategoryViewModel>();
cfg.CreateMap<SubCategory, SubCategoryViewModel>();
});
}
}
If you are using old version than AutoMapper 5 then it might look like below.
public class AutoMapperConfig
{
public static void CreateMappings()
{
Mapper.CreateMap<Category, CategoryViewModel>();
Mapper.CreateMap<SubCategory, SubCategoryViewModel>();
}
}
Where Category and SubCategory is your domain model [in <ProjectSolution>.Domain] and CategoryViewModel and SubCategoryViewModel is your view model [<ProjectSolution>.ViewModels].
This is still not ready for use as you need to call this initialization function before your custom code is executed where this mapping is used. I generally place the following code at
Application_Start() method of Global.asax.cs for MVC application.
protected void Application_Start()
{
//AreaRegistration
//FilterConfig
//GlobalConfiguration
//RouteConfig
//BundleConfig
AutoMapperConfig.CreateMappings();
//Dependency Injector
}
In case of console application, you can call AutoMapperConfig.CreateMappings(); at the beginning of the static void Main(string[] args).
class Program
{
static void Main(string[] args)
{
AutoMapperConfig.CreateMappings();
// Start your coding
}
}
Also you can call a separate function like InitializeProgram() and you can place all initialization code into one method.
Now you are ready to use the mappings.
Wherever you need it [in business layer], you can call the mapping like below
Category category = new Category();
category = DataAccessLayerObject.GetCategoryById(123);
CategoryViewModel categoryVM = new CategoryViewModel();
categoryVM = Mapper.Map<Category, CategoryViewModel>(category);
Here it is not illustrated how to define mapping when you have complex mappings.
- 1100 reads