Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: entity MVC3
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;
    }
}

 
Any answer would be appreciated!
Thanks,
Imdadhusen
Posted 3-Oct-11 2:48am
Edited 4-Oct-11 5:01am
v5

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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))
 
Thanks,
Imdadhusen
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 330
1 Sergey Alexandrovich Kryukov 279
2 BillWoodruff 245
3 Afzaal Ahmad Zeeshan 215
4 CPallini 205
0 OriginalGriff 5,635
1 DamithSL 4,496
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,180


Advertise | Privacy | Mobile
Web02 | 2.8.141216.1 | Last Updated 4 Oct 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100