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<IMappingEngine>().ToMethod(ctx => Mapper.Engine);
Bind<IMapper>().To<Mapping.Mapper>();
Kernel.BindFilter<AutoMapFilter>(FilterScope.Controller, 0)
.WhenActionMethodHas<AutoMapAttribute>()
.WithConstructorArgumentFromActionAttribute<AutoMapAttribute>("sourceType", att => att.SourceType)
.WithConstructorArgumentFromActionAttribute<AutoMapAttribute>("destType", att => att.DestType);
}
}
public class MyProfile : Profile
{
protected override void Configure()
{
Mapper.CreateMap<MyModel, MyViewModel>().ForMember(...);
}
}
}
Questions
- Is the way I bind to
AutoMapper
wrong? - Is this the wrong place for the profile (keep in mind the dependency restriction)?
In the ideal world I would have placed
Mapper.CreateMap<MyModel, MyViewModel>().ForMember(...)
in Global.asax, but how do I expose CreateMap
without referencing AutoMapper
?
Is there anything else you have noticed?
Mapper.CreateMap<MyModel, MyViewModel>().ForMember(...)
??? \$\endgroup\$