|
| 1 | +# Migrating from AutoMapper to Mapperly |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +The AutoMapper library is **no longer free for commercial use**. For more details, you can refer to [this announcement post](https://www.jimmybogard.com/automapper-and-mediatr-going-commercial/). |
| 6 | + |
| 7 | +ABP Framework provides both AutoMapper and Mapperly integrations. If your project currently uses AutoMapper and you don't have a commercial license, you can switch to Mapperly by following the steps outlined below. |
| 8 | + |
| 9 | +## Migration Steps |
| 10 | + |
| 11 | +Please open your project in an IDE(`Visual Studio`, `VS Code` or `JetBrains Rider`), then perform the following global search and replace operations: |
| 12 | + |
| 13 | +1. Replace `Volo.Abp.AutoMapper` with `Volo.Abp.Mapperly` in all `*.csproj` files. |
| 14 | +2. Replace `using Volo.Abp.AutoMapper;` with `using Volo.Abp.Mapperly;` in all `*.cs` files. |
| 15 | +3. Replace `AbpAutoMapperModule` with `AbpMapperlyModule` in all `*.cs` files. |
| 16 | +4. Replace `AddAutoMapperObjectMapper` with `AddMapperlyObjectMapper` in all `*.cs` files. |
| 17 | +5. Remove any code sections that configure `Configure<AbpAutoMapperOptions>`. |
| 18 | +6. Review any existing AutoMapper `Profile` classes and ensure all newly created Mapperly mapping classes are registered appropriately. (You can refer to the example below for guidance) |
| 19 | + |
| 20 | +**Example:** |
| 21 | + |
| 22 | +Here is an AutoMapper's `Profile` class: |
| 23 | + |
| 24 | +```csharp |
| 25 | +public class ExampleAutoMapper : Profile |
| 26 | +{ |
| 27 | + public AbpIdentityApplicationModuleAutoMapperProfile() |
| 28 | + { |
| 29 | + CreateMap<IdentityUser, IdentityUserDto>() |
| 30 | + .MapExtraProperties() |
| 31 | + .Ignore(x => x.IsLockedOut) |
| 32 | + .Ignore(x => x.SupportTwoFactor) |
| 33 | + .Ignore(x => x.RoleNames); |
| 34 | + |
| 35 | + CreateMap<IdentityUserClaim, IdentityUserClaimDto>(); |
| 36 | + |
| 37 | + CreateMap<OrganizationUnit, OrganizationUnitDto>() |
| 38 | + .MapExtraProperties(); |
| 39 | + |
| 40 | + CreateMap<OrganizationUnitRole, OrganizationUnitRoleDto>() |
| 41 | + .ReverseMap(); |
| 42 | + |
| 43 | + CreateMap<IdentityRole, OrganizationUnitRoleDto>() |
| 44 | + .ForMember(dest => dest.RoleId, src => src.MapFrom(r => r.Id)); |
| 45 | + |
| 46 | + CreateMap<IdentityUser, IdentityUserExportDto>() |
| 47 | + .ForMember(dest => dest.Active, src => src.MapFrom(r => r.IsActive ? "Yes" : "No")) |
| 48 | + .ForMember(dest => dest.EmailConfirmed, src => src.MapFrom(r => r.EmailConfirmed ? "Yes" : "No")) |
| 49 | + .ForMember(dest => dest.TwoFactorEnabled, src => src.MapFrom(r => r.TwoFactorEnabled ? "Yes" : "No")) |
| 50 | + .ForMember(dest => dest.AccountLookout, src => src.MapFrom(r => r.LockoutEnd != null && r.LockoutEnd > DateTime.UtcNow ? "Yes" : "No")) |
| 51 | + .Ignore(x => x.Roles); |
| 52 | + } |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +And the Mapperly mapping class: |
| 57 | + |
| 58 | +```csharp |
| 59 | +[Mapper] |
| 60 | +[MapExtraProperties] |
| 61 | +public partial class IdentityUserToIdentityUserDtoMapper : MapperBase<IdentityUser, IdentityUserDto> |
| 62 | +{ |
| 63 | + [MapperIgnoreTarget(nameof(IdentityUserDto.IsLockedOut))] |
| 64 | + [MapperIgnoreTarget(nameof(IdentityUserDto.SupportTwoFactor))] |
| 65 | + [MapperIgnoreTarget(nameof(IdentityUserDto.RoleNames))] |
| 66 | + public override partial IdentityUserDto Map(IdentityUser source); |
| 67 | + |
| 68 | + [MapperIgnoreTarget(nameof(IdentityUserDto.IsLockedOut))] |
| 69 | + [MapperIgnoreTarget(nameof(IdentityUserDto.SupportTwoFactor))] |
| 70 | + [MapperIgnoreTarget(nameof(IdentityUserDto.RoleNames))] |
| 71 | + public override partial void Map(IdentityUser source, IdentityUserDto destination); |
| 72 | +} |
| 73 | + |
| 74 | +[Mapper] |
| 75 | +public partial class IdentityUserClaimToIdentityUserClaimDtoMapper : MapperBase<IdentityUserClaim, IdentityUserClaimDto> |
| 76 | +{ |
| 77 | + public override partial IdentityUserClaimDto Map(IdentityUserClaim source); |
| 78 | + |
| 79 | + public override partial void Map(IdentityUserClaim source, IdentityUserClaimDto destination); |
| 80 | +} |
| 81 | + |
| 82 | +[Mapper] |
| 83 | +[MapExtraProperties] |
| 84 | +public partial class OrganizationUnitToOrganizationUnitDtoMapper : MapperBase<OrganizationUnit, OrganizationUnitDto> |
| 85 | +{ |
| 86 | + public override partial OrganizationUnitDto Map(OrganizationUnit source); |
| 87 | + public override partial void Map(OrganizationUnit source, OrganizationUnitDto destination); |
| 88 | +} |
| 89 | + |
| 90 | +[Mapper] |
| 91 | +public partial class OrganizationUnitRoleToOrganizationUnitRoleDtoMapper : TwoWayMapperBase<OrganizationUnitRole, OrganizationUnitRoleDto> |
| 92 | +{ |
| 93 | + public override partial OrganizationUnitRoleDto Map(OrganizationUnitRole source); |
| 94 | + public override partial void Map(OrganizationUnitRole source, OrganizationUnitRoleDto destination); |
| 95 | + |
| 96 | + public override partial OrganizationUnitRole ReverseMap(OrganizationUnitRoleDto destination); |
| 97 | + public override partial void ReverseMap(OrganizationUnitRoleDto destination, OrganizationUnitRole source); |
| 98 | +} |
| 99 | + |
| 100 | +[Mapper] |
| 101 | +[MapExtraProperties] |
| 102 | +public partial class OrganizationUnitToOrganizationUnitWithDetailsDtoMapper : MapperBase<OrganizationUnit, OrganizationUnitWithDetailsDto> |
| 103 | +{ |
| 104 | + [MapperIgnoreTarget(nameof(OrganizationUnitWithDetailsDto.Roles))] |
| 105 | + [MapperIgnoreTarget(nameof(OrganizationUnitWithDetailsDto.UserCount))] |
| 106 | + public override partial OrganizationUnitWithDetailsDto Map(OrganizationUnit source); |
| 107 | + |
| 108 | + [MapperIgnoreTarget(nameof(OrganizationUnitWithDetailsDto.Roles))] |
| 109 | + [MapperIgnoreTarget(nameof(OrganizationUnitWithDetailsDto.UserCount))] |
| 110 | + public override partial void Map(OrganizationUnit source, OrganizationUnitWithDetailsDto destination); |
| 111 | +} |
| 112 | + |
| 113 | +[Mapper] |
| 114 | +public partial class IdentityRoleToOrganizationUnitRoleDtoMapper : MapperBase<IdentityRole, OrganizationUnitRoleDto> |
| 115 | +{ |
| 116 | + [MapProperty(nameof(IdentityRole.Id), nameof(OrganizationUnitRoleDto.RoleId))] |
| 117 | + public override partial OrganizationUnitRoleDto Map(IdentityRole source); |
| 118 | + |
| 119 | + [MapProperty(nameof(IdentityRole.Id), nameof(OrganizationUnitRoleDto.RoleId))] |
| 120 | + public override partial void Map(IdentityRole source, OrganizationUnitRoleDto destination); |
| 121 | +} |
| 122 | + |
| 123 | +[Mapper] |
| 124 | +public partial class IdentityUserToIdentityUserExportDtoMapper : MapperBase<IdentityUser, IdentityUserExportDto> |
| 125 | +{ |
| 126 | + [MapperIgnoreTarget(nameof(IdentityUserExportDto.Roles))] |
| 127 | + public override partial IdentityUserExportDto Map(IdentityUser source); |
| 128 | + |
| 129 | + [MapperIgnoreTarget(nameof(IdentityUserExportDto.Roles))] |
| 130 | + public override partial void Map(IdentityUser source, IdentityUserExportDto destination); |
| 131 | + |
| 132 | + public override void AfterMap(IdentityUser source, IdentityUserExportDto destination) |
| 133 | + { |
| 134 | + destination.Active = source.IsActive ? "Yes" : "No"; |
| 135 | + destination.EmailConfirmed = source.EmailConfirmed ? "Yes" : "No"; |
| 136 | + destination.TwoFactorEnabled = source.TwoFactorEnabled ? "Yes" : "No"; |
| 137 | + destination.AccountLookout = source.LockoutEnd != null && source.LockoutEnd > DateTime.UtcNow ? "Yes" : "No"; |
| 138 | + } |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +## Mapperly Mapping Class |
| 143 | + |
| 144 | +To use Mapperly, you'll need to create a dedicated mapping class for each source and destination types. |
| 145 | + |
| 146 | +* Use the `[Mapper]` attribute to designate the class as a Mapperly mapper. |
| 147 | +* Replace AutoMapper's `Ignore()` method with the `[MapperIgnoreTarget]` attribute. |
| 148 | +* Replace the `MapExtraProperties()` method with the `[MapExtraProperties]` attribute. |
| 149 | +* Use the `TwoWayMapperBase` class as an alternative to AutoMapper’s `ReverseMap()` functionality. |
| 150 | +* Implement the `AfterMap()` method to execute logic after the mapping is completed. |
| 151 | + |
| 152 | +### Dependency Injection in Mapper Class |
| 153 | + |
| 154 | +All Mapperly mapping classes automatically registered in the the [dependency injection (DI)](../../framework/fundamentals/dependency-injection.md) container. To use a service within a Mapper class, simply add it to the constructor, Mapperly will inject it automatically. |
| 155 | + |
| 156 | +**Example:** |
| 157 | + |
| 158 | +```csharp |
| 159 | +public partial class IdentityUserToIdentityUserDtoMapper : MapperBase<IdentityUser, IdentityUserDto> |
| 160 | +{ |
| 161 | + public IdentityUserToIdentityUserDtoMapper(MyService myService) |
| 162 | + { |
| 163 | + _myService = myService; |
| 164 | + } |
| 165 | + |
| 166 | + public override partial IdentityUserDto Map(IdentityUser source); |
| 167 | + public override partial void Map(IdentityUser source, IdentityUserDto destination); |
| 168 | + |
| 169 | + public override void AfterMap(IdentityUser source, IdentityUserDto destination) |
| 170 | + { |
| 171 | + destination.MyProperty = _myService.GetMyProperty(source.MyProperty); |
| 172 | + } |
| 173 | +} |
| 174 | +``` |
| 175 | + |
| 176 | +## Mapperly Documentation |
| 177 | + |
| 178 | +Please refer to the [Mapperly documentation](https://mapperly.riok.app/docs/intro/) for more details. |
| 179 | + |
| 180 | +**Key points:** |
| 181 | + |
| 182 | +- [Mapperly Configuration](https://mapperly.riok.app/docs/configuration/mapper/) |
| 183 | +- [Mapperly Enums](https://mapperly.riok.app/docs/configuration/enum/) |
| 184 | +- [Mapperly Flattening and unflattening](https://mapperly.riok.app/docs/configuration/flattening/) |
0 commit comments