HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Injecting AutoMapper profiles

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
injectingautomapperprofiles

Problem

I'd like to know if I'm doing profile configuration in the wrong place or in the wrong way.

I'm following the Onion Architecture, so that restricts the direction of my dependencies towards the center.

Core

My domain model and AutoMapper facade:

namespace Core.Domain
{
    public class MyModel
    {
        // model stuff
    }
}

namespace Core.Services
{
    public interface IMapper
    {
        object Map(object source, Type sourceType, Type destinationType);
    }
}


Infrastructure

AutoMapper facade implementation:

namespace Infrastructure.Mapping
{
    public class Mapper : IMapper
    {
        private readonly IMappingEngine _mappingEngine;

        public Mapper(IMappingEngine mappingEngine)
        {
            _mappingEngine = mappingEngine;
        }

        public object Map(object source, Type sourceType, Type destinationType)
        {
            return _mappingEngine.Map(source, sourceType, destinationType);
        }
    }
}


UI

This is my controller and view model. I'm using the AutoMapper via a filter, following this example.

namespace UI.Controllers
{
    public class HomeController : Controller
    {    
        [AutoMap(typeof(MyModel), typeof(MyViewModel))]
        public ActionResult Index()
        {
            var myItem = _myRepository.GetById(0);
            return View(myItem);
        }
    }
}

namespace UI.ViewModels
{
    public class MyViewModel
    {
        // view stuff
    }
}


Dependency Resolution

This is where I have my doubts:

```
namespace DependencyResolution
{
public class MappingModule : NinjectModule
{
public override void Load()
{
Mapper.Initialize(cfg => cfg.AddProfile(new MyProfile()));
Bind().ToMethod(ctx => Mapper.Engine);
Bind().To();

Kernel.BindFilter(FilterScope.Controller, 0)
.WhenActionMethodHas()
.WithConstructorArgumentFromActionAttribute("sourceType", att => a

Solution

What is the purpose of the IMapper interface and Mapper class? It looks to me that they are just wrapping the IMappingEngine interface and MappingEngine class. While this is a good method when you have a third party class that doesn't have an interface, I think it is overkill here. Why don't you just use the IMappingEngine where you need that functionality?

If you are going to keep your Mapper class, I would rename it, having two Mapper classes is confusing.

As for where it is, I don't have a problem with doing it this way. All the wire-up is done in one place, and its easy to find and add to as needed.

Context

StackExchange Code Review Q#23625, answer score: 5

Revisions (0)

No revisions yet.