Click here to Skip to main content
12,547,922 members (46,796 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# MVC
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; }

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

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)
            TempData["message"] = string.Format("{0} has been saved", glossaryModel.Definition);
            return RedirectToAction("All");
        return View(glossaryModel);

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

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

Posted 8-Jan-13 8:40am

1 solution

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

Solution 1


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

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


Few Articles you may enjoy :[^][^][^]
Sanjay Debnath 9-Jan-13 7:23am
Bothways doesn't exist by default... You need to write an extersion method for that like

public static IMappingExpression<TDestination, TSource> BothWays<tsource, tdestination="">
(this IMappingExpression<tsource, tdestination=""> mappingExpression)
return Mapper.CreateMap<TDestination, TSource>();

Also it can be achieved by the builtin .ReverseMap() in the map chain.
Suvabrata Roy 9-Jan-13 7:32am

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.161021.1 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2016
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