Skip to content

Instantly share code, notes, and snippets.

@anytizer
Last active December 31, 2017 15:11
Show Gist options
  • Save anytizer/029a9c6c982340507e8bc523784cac47 to your computer and use it in GitHub Desktop.
Save anytizer/029a9c6c982340507e8bc523784cac47 to your computer and use it in GitHub Desktop.
Auto Mapper
Mapper.Initialize(cfg => {
cfg.CreateMap<account_roles, RoleDTO>()
.ForMember(x => x.id, opt => opt.MapFrom(x => x.role_id))
.ForMember(x => x.name, opt => opt.MapFrom(x => x.role_name))
;
cfg.CreateMap<account_users, UserDTO>()
.ForMember(x => x.id, opt => opt.MapFrom(x => x.user_id))
.ForMember(x => x.email, opt => opt.MapFrom(x => x.user_email))
;
});
public RoleDTO[] roles()
{
account_roles[] roles = ae.account_roles.ToArray();
RoleDTO[] r = new RoleDTO[roles.Length];
for(int i=0; i<roles.Length; ++i)
{
r[i] = Mapper.Map<account_roles, RoleDTO>(roles[i]);
}
return r;
}
public UserDTO[] users()
{
account_users[] users = ae.account_users.ToArray();
UserDTO[] r = new UserDTO[users.Length];
for (int i = 0; i < users.Length; ++i)
{
r[i] = Mapper.Map<account_users, UserDTO>(users[i]);
}
return r;
}
// array
UserDTO[] r = Mapper.Map<account_users[], UserDTO[]>(users);
@anytizer
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment