Automapper AfterMap class initialization function

Can someone explain how the AfterMap function initializes the class objects passed in the lambda expression? When I run this bit of code as myself, it populates the dest object with my details. i can't figure out how it's done? but if someone can tell me how it goes with the initialization of classes, it will help me go in the right direction please.

    AutoMapper.Mapper.CreateMap(Of Service.User, Models.User)() _
        .ForMember(Function(dest As Models.User) dest.EmployeeNumber, Sub(opt) opt.MapFrom(Function(src As Service.User) src.IdentityNumber)) _
    .AfterMap((Sub(src As Service.User, dest As Models.User)

                   'Extract the group names for the current application.
                   dest.Groups = New List(Of String)

                   Using service = ApplicationSecurityManager.Service.Factory.GetService()

                       Dim applicationPermissions = service.LoadPermissionsForUser(dest.Username, MyBase.EnvironmentCode)

                       If (Not applicationPermissions Is Nothing AndAlso applicationPermissions.Any(Function(x) x.Code = MyBase.ApplicationCode)) Then

                           dest.Groups = applicationPermissions.Single(Function(x) x.Code = MyBase.ApplicationCode).GroupNames.ToList()

                       End If

                   End Using

               End Sub))

      

------- Edited with answer ----

Objects matter in them because they are called after calling the Mapping.Map function and that is where the actual object with the values ​​is passed and then the AfterMap function is called and how it matters in it.

+3


source to share


2 answers


Objects matter in them because they are called after calling the Mapping.Map function and that is where the actual object with the values ​​is passed and then the AfterMap function is called and how it matters in it.



0


source


you may notice that AfterMap is a Lambda (Action) expression taking input as source and target objects using Reflection, can easily copy values ​​from one object to another. In this case, just an action is performed because Func is just a delegate and you just defined this method (in Lambda). In fact, the above statement can be rewritten as

 AutoMapper.Mapper.CreateMap(Of Service.User, Models.User)() _
    .ForMember(Function(dest As Models.User) dest.EmployeeNumber, Sub(opt) opt.MapFrom(Function(src As Service.User) src.IdentityNumber)) _
.AfterMap(AfterProc)

      

.....



Public Sub AfterProc(ByVal src As Service.User,ByVal dest As Models.User)
 'Extract the group names for the current application.
               dest.Groups = New List(Of String)

               Using service = ApplicationSecurityManager.Service.Factory.GetService()

                   Dim applicationPermissions = service.LoadPermissionsForUser(dest.Username, MyBase.EnvironmentCode)

                   If (Not applicationPermissions Is Nothing AndAlso applicationPermissions.Any(Function(x) x.Code = MyBase.ApplicationCode)) Then

                       dest.Groups = applicationPermissions.Single(Function(x) x.Code = MyBase.ApplicationCode).GroupNames.ToList()

                   End If

               End Using

           End Sub
End Sub

      

you can see the source of AutoMapper at

https://github.com/AutoMapper/AutoMapper/blob/32959279d8b81087d1c84903f56755cc57d35740/src/AutoMapper/Mappers/TypeMapObjectMapperRegistry.cs (line 98)

+1


source







All Articles