English 中文(简体)
如何优化自动制图的代码?
原标题:how to optimize code for automatic Mapping?

www.un.org/Depts/DGACM/index_spanish.htm 我愿利用诸如“自动火器”之类的东西来制定以下守则,而不是人工测绘。

www.un.org/Depts/DGACM/index_spanish.htm 我有两张用户和作用表,两张都用功能信息数据库绘制。 现在的问题是,如果从记录中删除的话。 然后,用户只给我<>RoleID,而不是RoleName。 这样一来,就试图按习俗分类,并绘制每个实体的图,以备居住(通常)。

任何人都帮助使手册自动化。 因为只有15个关系表。

// 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

最佳回答

www.un.org/Depts/DGACM/index_spanish.htm 我通过下列解决办法解决这一问题:。

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;
        }

<>说明: i 仅增加一条代码,以实现预期产出

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

Thanks,
Imdadhusen

问题回答

暂无回答




相关问题
Mock testing with Repository pattern example

I m refactoring existing class to support Products import from CSV file.   Requirements: 1) during import products are identified by special product number. 2) product has category attribute. ...

Entity Framework using Generic Predicates

I use DTO s to map between my Business and Entity Framework layer via the Repository Pattern. A Standard call would look like public IClassDTO Fetch(Guid id) { var query = from s in _db.Base....

Create, Approve, Edit, Approve pattern for site content

I m working on a modification to a site to allowing users to enter content. However, once they ve created that content it needs to be approved before it can be published. This is fairly easy to ...

Repository + NHibernate + DTO

I have an application with a repository layer (and a nhibernate implementation), service (bussiness) layer, and an asp.net mvc in the web layer. Because I also need to create a small silverlight ...

Organizing classes using the repository design pattern

I have started upgrading one of our internal software applications, written in ASP.NET Web Forms, and moving to ASP.NET MVC. I am trying to leverage the Repository design pattern for my classes, ...