ViewModel的域对象&反之亦然使用Automapper [英] Domain object to ViewModel & vice versa using Automapper

查看:94
本文介绍了ViewModel的域对象&反之亦然使用Automapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好日子,



我正在为我的公司开发一个新的MVC网站&有点混淆如何创建从Domain / POCO对象到ViewModel类的映射[包含验证]&反之亦然。这是一个示例。



我的域类:

Hi guys good day,

I''m developing a new MVC site for my company & kind of confused as how to create mapping from Domain/POCO objects to ViewModel classes [contains validation] & vice versa. Here''s an sample example.

My domain class:

public partial class Glossary
    {
        public int Id { get; set; }
        public string GlossaryItem { get; set; }
        public string Definition { get; set; }
    }





我的ViewModel类:



my ViewModel class:

public class GlossaryModel
    {
        [HiddenInput(DisplayValue = false)]
        public int Id { get; set; }

        [Required(ErrorMessage = "Please enter a GlossaryItem")]
        public string GlossaryItem { get; set; }

        [Required(ErrorMessage = "Please enter a Definition")]
        public string Definition { get; set; }
    }





我的DTO到域模型的Automapper配置:



my Automapper configuration for DTO to Domain Model:

protected override void Configure()
    {
         CreateMap<GlossaryModel, Glossary>();
         //....... etc
    }





我的控制器的动作方法:



My controller''s action method:

public class GlossaryController : Controller
{
    IGlossaryRepository _glossaryRepository;
    IMappingService _mappingService;

    public GlossaryController(IGlossaryRepository glossaryRepository, IMappingService autoMapperMappingService)
    {
        _glossaryRepository = glossaryRepository;
        _mappingService = autoMapperMappingService;
    }

    // .... etc

    [HttpPost, ValidateAntiForgeryToken]
    public virtual ActionResult Edit(GlossaryModel glossaryModel)
    {
        if (ModelState.IsValid)
        {
            var glossary = _mappingService.Map<GlossaryModel, Glossary>(glossaryModel);
            if (glossaryModel.Id <= 0)
                _glossaryRepository.Add(glossary);
            else
                _glossaryRepository.Edit(glossary);
            _glossaryRepository.Save();
            TempData["message"] = string.Format("{0} has been saved", glossaryModel.Definition);
            return RedirectToAction("All");
        }
        return View(glossaryModel);
    }

    //....etc
 }





它工作正常,但我的问题是......现在说我需要一个将列出所有词汇表项目的操作,例如



And it''s working fine, but my question is... Now say I need an action that will list down all glossary items like

public ActionResult All()
        {
            var allItems = _glossaryRepository.Glossary;
            if (allItems.Count() == 0) return View(new List<GlossaryModel>());
            var allItemsModel = _mappingService.Map<IEnumerable<Glossary>, IEnumerable<GlossaryModel>>(allItems);
            return View(allItemsModel);
        }



但现在我需要使用automapper从Domain对象转换为DTO [从List(Glossary)转换为List(GlossaryModel)],正好在Edit的对面方法,将数据推送到视图。所以我再次需要在automapper配置中映射相反的绑定... !!喜欢


But now I need automapper to convert from Domain objects to DTO [from List(Glossary) to List(GlossaryModel)], just opposite of the Edit method, to push the data to the view. So do I again need to map the opposite binding in the automapper config...!! like

protected override void Configure()
    {
         CreateMap<GlossaryModel, Glossary>(); // Added before for DTO to Domain object 
         CreateMap<Glossary, GlossaryModel>();// Added for Domain object to DTO
         //....... etc
    }





绑定两种方式是一个好的设计吗?或者有更好的解决方案我想念,请帮助



谢谢,

Sanjay



Is it a good design to bind both ways? or there''s better solution I''m missing, Please help

Thanks,
Sanjay

推荐答案

您好,



您的代码没有问题,但如果您需要最小化代码,那么试试这个,



Hi,

There is no issue with your code but if you need to minimize you code then try this,

CreateMap<Source, Dest>().BothWays();





[更新]



您可以享受的文章很少:



http://lostechies.com/jimmybogard/ 2009/09/18 / the-case-for-two-way-mapping-in-automapper / [ ^ ]



< a href =http://bengtbe.com/blog/2009/04/14/using-automapper-to-map-view-models-in-asp-net-mvc/> http://bengtbe.com/博客/ 2009/04/14 /使用-automapper到地图视图的模型,我n-asp-net-mvc / [ ^ ]



http://prashantbrall.wordpress.com/2010/11/02/automapper/ [ ^ ]



[Update]

Few Articles you may enjoy :

http://lostechies.com/jimmybogard/2009/09/18/the-case-for-two-way-mapping-in-automapper/[^]

http://bengtbe.com/blog/2009/04/14/using-automapper-to-map-view-models-in-asp-net-mvc/[^]

http://prashantbrall.wordpress.com/2010/11/02/automapper/[^]


这篇关于ViewModel的域对象&amp;反之亦然使用Automapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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