Automapper AfterMap function initialising classes

1k Views Asked by At

Can anyone please explain how does the AfterMap function initalises 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 is it doing that? but if someone can tell me how does it go about initialising classes, that 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----

The objects have values in them because they are called after Mapping.Map function is called and that's where actual object with values is passed and then AfterMap function is called and that's how it has the values in it.

2

There are 2 best solutions below

0
On BEST ANSWER

The objects have values in them because they are called after Mapping.Map function is called and that's where actual object with values is passed and then AfterMap function is called and that's how it has the values in it.

1
On

you may note that AfterMap is Lambda Expression (Action) taking input as source and destination objects using Reflection one may easily copy values from one Object into another.in this case simply the Action gets executed since Func is just a delegate and you have just defined that method (in Lambda).In fact the above statement may 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 may see source of AutoMapper at

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