[已解决]如何优化自动映射的代码? [英] [SOLVED] How to optimize code for automatic Mapping?

查看:99
本文介绍了[已解决]如何优化自动映射的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 全部,

我想对下面的代码使用类似AutoMapper的东西,而不是手动映射.

我有两个表User和Role,并且都映射有RoleID. 现在的问题是,如果我要从用户那里获取记录,那么它将只给我 RoleID ,而不是 RoleName .所以我尝试通过自定义获取结果类,并使用for循环(手动)映射每个实体.

因为我有超过15个具有关系的表,所以有人可以帮助将此手动代码优化为自动吗?

代码段:

Hi All,

I would like to use something like AutoMapper for the following code instead of manual mapping.

I have two tables User and Role and both mapped with RoleID. now problem is if i am getting records from Users then it will gives me only RoleID instead of RoleName. So i tried to get result with custom class and mapped each entities with for loop (Manually).

Can anybody help to optimize this manual code to automatic?
because i have more then 15 tables with relations.

Code Snippet:

// M O D E L
public class RoleModel : IRoleModel
{
    public Guid RoleID { get; set; }
    public string RoleName { get; set; }
    public string Description { get; set; }
}
public class UserModel : IUserModel
{
    public Guid UserID { get; set; }
    public Guid RoleID { get; set; }
    public string UserName { get; set; }
}
public class UserRoleModel
{
    public Guid UserID { get; set; }
    public string UserName { get; set; }
    public string Description { get; set; }
    public Guid RoleID { get; set; }
    public string RoleName { get; set; }
}
// C O N T R O L L E R
public class UsersController : Controller
{
    private IUserService _UserService;
    public UsersController()
        : this(new UserService())
    {
    }
    public UsersController(IUserService UserService)
    {
        _UserService = UserService;
    }
    public ActionResult Index()
    {
        IList<UserRoleModel> users = _UserService.GetUsersWithRole();
        return View(users);
    }
    public ActionResult Create()
    {
        return View();
    }
}
// S E R V I C E
public class UserService : ServiceBase<IUserService, User>, IUserService
{
    private IUserRepository _UserRepository;
    public UserService()
        : this(new UserRepository())
    {
    }
    public UserService(IUserRepository UserRepository)
    {
        _UserRepository = UserRepository ?? new UserRepository();
    }
    public IList<UserRoleModel> GetUsersWithRole()
    {
        IList<User> users = _UserRepository.GetAll();
        IList<UserRoleModel> userswithrol = new List<UserRoleModel>();
        /* ---------------------------------------------
        I would like to use AUTO MAPPER instead of MANUAL MAPPING
        ------------------------------------------------  */
        foreach (User u in users)
        {
            UserRoleModel ur = new UserRoleModel();
            ur.UserID = u.UserID;
            ur.UserName = u.UserName;
            ur.Description = u.Description;
            ur.RoleID = u.RoleID.Value;
            ur.RoleName = u.Role.RoleName;
            userswithrol.Add(ur);
        }
        /*--------------------------------------------*/
        return userswithrol;
    }
    private IList<UserModel> GetAll()
    {
        IEnumerable<User> alliance;
        if (whereCondition != null)
            alliance = _UserRepository.GetAll(whereCondition);
        else
            alliance = _UserRepository.GetAll();
        UserListModel model = new UserListModel();
        model.UserList = new List<UserModel>();
        AutoMapper.Mapper.CreateMap<User, UserModel>().ForMember(dest => dest.UserID, opt => opt.MapFrom(src => src.UserID));
        model.UserList = AutoMapper.Mapper.Map(alliance, model.UserList);
        return model.UserList;
    }
}



任何答案将不胜感激!
谢谢,
Imdadhusen



Any answer would be appreciated!
Thanks,
Imdadhusen

推荐答案

我使用以下解决方案解决了这个问题:

I have SOLVED this question using following solution:

public IList<UserRoleModel> GetUsersWithRole()
        {
            IList<User> users = _UserRepository.GetAll();
            IList<UserRoleModel> userswithrol = new List<UserRoleModel>();
            AutoMapper.Mapper.CreateMap<User, UserRoleModel>()
                .ForMember(d => d.UserID, o => o.MapFrom(s => s.UserID))
                .ForMember(d => d.RoleName, o => o.MapFrom(s => s.Role.RoleName));
            userswithrol = AutoMapper.Mapper.Map(users, userswithrol);
            return userswithrol;
        }



注意:
我刚刚添加了单行代码以实现所需的输出



Note:
i have just added single line of code to achieve desired output

.ForMember(d => d.RoleName, o => o.MapFrom(s => s.Role.RoleName))



谢谢,
Imdadhusen



Thanks,
Imdadhusen


这篇关于[已解决]如何优化自动映射的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆