4
\$\begingroup\$

Sometimes, I need to map from a Domain entity to a ViewModel - to display information.

Other times, I need to map from a ViewModel to a Domain entity - for persistance of data.

Is this kosher or does this code smell?

protected void Application_Start() 
{
 AreaRegistration.RegisterAllAreas();
 MapObjects();
 RegisterGlobalFilters(GlobalFilters.Filters);
 RegisterRoutes(RouteTable.Routes);
}
private void MapObjects()
{
 Mapper.CreateMap<UserModel, User>();
 Mapper.CreateMap<ProductBrandModel, ProductBrand>();
 Mapper.CreateMap<ProductBrand, ProductBrandModel>();
 Mapper.CreateMap<ProductCategoryModel, ProductCategory>();
 Mapper.CreateMap<ProductCategory, ProductCategoryModel>();
}
Jamal
35.2k13 gold badges134 silver badges238 bronze badges
asked Aug 24, 2011 at 20:44
\$\endgroup\$
2
  • 1
    \$\begingroup\$ I am thoroughly unfamiliar with this mapper stuff, I'm guessing based on the route things I see this is a part of the asp.net mvc which I haven't worked with. That said, even to my eyes I would say this absolutely smells funny. My immediate thought is try drawing your ProductBrandModel and ProductBrand on a whiteboard showing their relationships, then see if you can't turn make the relationships one way by breaking either of them up such that no two-way relationship exists between any of the classes on the whiteboard. \$\endgroup\$ Commented Aug 25, 2011 at 1:01
  • \$\begingroup\$ This post by Jimmy Bogard might help. \$\endgroup\$ Commented Aug 30, 2011 at 5:42

3 Answers 3

3
\$\begingroup\$

Mapping to and from domain models and view models is exactly what automapper is designed for.

answered Sep 14, 2011 at 14:25
\$\endgroup\$
3
\$\begingroup\$

Automapper ReverseMap

This is a more succint way to register your classes. Automapper is able to create a two-way mapping expression using ReverseMap.

private void MapObjects()
{
 Mapper.CreateMap<UserModel, User>();
 Mapper.CreateMap<ProductBrandModel, ProductBrand>().ReverseMap();
 Mapper.CreateMap<ProductCategoryModel, ProductCategory>().ReverseMap();
}

As opposed to the mirrored registration..

private void MapObjects()
{
 Mapper.CreateMap<UserModel, User>();
 Mapper.CreateMap<ProductBrandModel, ProductBrand>();
 Mapper.CreateMap<ProductBrand, ProductBrandModel>();
 Mapper.CreateMap<ProductCategoryModel, ProductCategory>();
 Mapper.CreateMap<ProductCategory, ProductCategoryModel>();
}
answered Jul 26, 2019 at 16:15
\$\endgroup\$
0
\$\begingroup\$

This looks fine to me although you may want to extract it out in to a separate class if it starts to get bigger. I usually end up with an Init (or similar) folder with classes for IOC, ORM, Mapper and Routes (if they get big enough). Once out of the Global.asax you can test them if needed as well (not required for basic mappings in your example of course).

Rename it to RegisterViewMapper() or similar and it will smell less.

answered Sep 14, 2011 at 22:20
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.