Skip to content

Commit c588dcd

Browse files
authored
Merge pull request #1 from exiled78/AutoMapperConfig
Change automapper config location.
2 parents ea53e1a + f4253fe commit c588dcd

File tree

3 files changed

+65
-24
lines changed

3 files changed

+65
-24
lines changed

aspnet-core/src/AbpCompanyName.AbpProjectName.Application/AbpProjectNameApplicationModule.cs

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,33 +30,13 @@ public override void PreInitialize()
3030

3131
public override void Initialize()
3232
{
33-
IocManager.RegisterAssemblyByConvention(typeof(AbpProjectNameApplicationModule).GetAssembly());
33+
Assembly thisAssembly = typeof(AbpProjectNameApplicationModule).GetAssembly();
34+
IocManager.RegisterAssemblyByConvention(thisAssembly);
3435

35-
// TODO: Is there somewhere else to store these, with the dto classes
3636
Configuration.Modules.AbpAutoMapper().Configurators.Add(cfg =>
3737
{
38-
// Role and permission
39-
cfg.CreateMap<Permission, string>().ConvertUsing(r => r.Name);
40-
cfg.CreateMap<RolePermissionSetting, string>().ConvertUsing(r => r.Name);
41-
42-
cfg.CreateMap<CreateRoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
43-
cfg.CreateMap<RoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
44-
45-
IRepository<Role, int> repository = IocManager.Resolve<IRepository<Role, int>>();
46-
// User and role
47-
cfg.CreateMap<UserRole, string>().ConvertUsing( (r) => {
48-
//TODO: Fix, this seems hacky
49-
Role role = repository.FirstOrDefault(r.RoleId);
50-
return role.DisplayName;
51-
});
52-
53-
IocManager.Release(repository);
54-
55-
cfg.CreateMap<UserDto, User>();
56-
cfg.CreateMap<UserDto, User>().ForMember(x => x.Roles, opt => opt.Ignore());
57-
58-
cfg.CreateMap<CreateUserDto, User>();
59-
cfg.CreateMap<CreateUserDto, User>().ForMember(x => x.Roles, opt => opt.Ignore());
38+
//Scan the assembly for classes which inherit from AutoMapper.Profile
39+
cfg.AddProfiles(thisAssembly);
6040
});
6141
}
6242
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Abp.Authorization;
7+
using Abp.Authorization.Roles;
8+
using Abp.Authorization.Users;
9+
using Abp.Dependency;
10+
using Abp.Domain.Repositories;
11+
using AbpCompanyName.AbpProjectName.Authorization.Roles;
12+
using AutoMapper;
13+
14+
namespace AbpCompanyName.AbpProjectName.Roles.Dto
15+
{
16+
public class RoleMapProfile : Profile
17+
{
18+
public RoleMapProfile()
19+
{
20+
// Role and permission
21+
CreateMap<Permission, string>().ConvertUsing(r => r.Name);
22+
CreateMap<RolePermissionSetting, string>().ConvertUsing(r => r.Name);
23+
24+
CreateMap<CreateRoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
25+
CreateMap<RoleDto, Role>().ForMember(x => x.Permissions, opt => opt.Ignore());
26+
27+
IRepository<Role, int> repository = IocManager.Instance.Resolve<IRepository<Role, int>>();
28+
// User and role
29+
CreateMap<UserRole, string>().ConvertUsing((r) => {
30+
//TODO: Fix, this seems hacky
31+
Role role = repository.FirstOrDefault(r.RoleId);
32+
return role.DisplayName;
33+
});
34+
35+
IocManager.Instance.Release(repository);
36+
}
37+
}
38+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using AbpCompanyName.AbpProjectName.Authorization.Users;
7+
using AutoMapper;
8+
9+
namespace AbpCompanyName.AbpProjectName.Users.Dto
10+
{
11+
public class UserMapProfile : Profile
12+
{
13+
public UserMapProfile()
14+
{
15+
16+
CreateMap<UserDto, User>();
17+
CreateMap<UserDto, User>().ForMember(x => x.Roles, opt => opt.Ignore());
18+
19+
CreateMap<CreateUserDto, User>();
20+
CreateMap<CreateUserDto, User>().ForMember(x => x.Roles, opt => opt.Ignore());
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)