From 50aee22bc591467ea0eca270da47d268fe66ef15 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Fri, 14 Feb 2020 11:03:37 +0300 Subject: [PATCH] Update AspNet-Boilerplate-Migration-Guide.md --- docs/en/AspNet-Boilerplate-Migration-Guide.md | 79 +++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/docs/en/AspNet-Boilerplate-Migration-Guide.md b/docs/en/AspNet-Boilerplate-Migration-Guide.md index 0a2b3b17b3..b47bb521e0 100644 --- a/docs/en/AspNet-Boilerplate-Migration-Guide.md +++ b/docs/en/AspNet-Boilerplate-Migration-Guide.md @@ -488,6 +488,85 @@ public class TaskAppService : ITaskAppService You inject the `ILogger` instead of the `ILogger`. +### Object to Object Mapping + +#### IObjectMapper Service + +ASP.NET Boilerplate defines an `IObjectMapper` service ([see](https://aspnetboilerplate.com/Pages/Documents/Object-To-Object-Mapping)) and has an integration to the [AutoMapper](https://automapper.org/) library. + +Example usage: Create a `User` object with the given `CreateUserInput` object: + +````csharp +public void CreateUser(CreateUserInput input) +{ + var user = ObjectMapper.Map(input); + ... +} +```` + +Example: Update an existing `User` properties with the given `UpdateUserInput` object: + +````csharp +public async Task UpdateUserAsync(Guid id, UpdateUserInput input) +{ + var user = await _userRepository.GetAsync(id); + ObjectMapper.Map(input, user); +} +```` + +ABP Framework has the same `IObjectMapper` service ([see](Object-To-Object-Mapping.md)) and the AutoMapper integration with a slightly different mapping methods. + +Example usage: Create a `User` object with the given `CreateUserInput` object: + +````csharp +public void CreateUser(CreateUserInput input) +{ + var user = ObjectMapper.Map(input); +} +```` + +This time you need to explicitly declare the source type and target type (while ASP.NET Boilerplate was requiring only the target type). + +Example: Update an existing `User` properties with the given `UpdateUserInput` object: + +````csharp +public async Task UpdateUserAsync(Guid id, UpdateUserInput input) +{ + var user = await _userRepository.GetAsync(id); + ObjectMapper.Map(input, user); +} +```` + +Again, ABP Framework expects to explicitly set the source and target types. + +#### AutoMapper Integration + +##### Auto Mapping Attributes + +ASP.NET Boilerplate has `AutoMapTo`, `AutoMapFrom` and `AutoMap` attributes to automatically create mappings for the declared types. Example: + +````csharp +[AutoMapTo(typeof(User))] +public class CreateUserInput +{ + public string Name { get; set; } + public string Surname { get; set; } + ... +} +```` + +ABP Framework has no such attributes, because AutoMapper as a [similar attribute](https://automapper.readthedocs.io/en/latest/Attribute-mapping.html) now. You need to switch to AutoMapper's attribute. + +##### Mapping Definitions + +ABP Framework follows AutoMapper principles closely. You can define classes derived from the `Profile` class to define your mappings. + +##### Configuration Validation + +Configuration validation is a best practice for the AutoMapper to maintain your mapping configuration in a safe way. + +See [the documentation](Object-To-Object-Mapping.md) for more information related to the object mapping. + ### Setting Management #### Defining the Settings