1

我正在尝试配置我的对象映射器,但不知道我正在使用哪个映射器。:/

这听起来可能有点奇怪。这样做的原因是我正在尝试洋葱架构,因此我的 UI 无法了解位于我的基础架构中的对象映射器。有关示例,请参见此解决方案。

洋葱架构

我在弄清楚我应该如何“委托”非默认映射行为时遇到了一些麻烦。像这样的东西:

Mapper
    .CreateMap<MyModel, MyDestViewModel>()
    .ForMember(
        dest => dest.SomeDestinationProperty,
        opt => opt.MapFrom(src => src.SomeSourceProperty)
    );

我在我的 MVC 项目中设置了一个从 Global.asax 调用的类,这是我要配置我的映射的地方。

public static class MapConfig
{
    public static void RegisterMaps()
    {

    }
}

我在想我可以做如下的事情。(IMapper是一个位于Domain中的自定义接口)

public static void RegisterMaps(HttpConfiguration config)
{
    var mapper = config.DependencyResolver.GetService(IMapper);
    mapper.CreateMap<MyModel, MyViewModel>();
}

现在......我将如何设置像.ForMember这样的特殊行为?请记住,它不能特定于 AutoMapper。

我在想一些事情,在这些方面mapper.CreateMap<MyModel, MyViewModel>(Expression<Func<T>>),Func 会做一些我现在无法弄清楚的黑魔法:( - 我是在正确的道路上还是我错过了一些重要的事情?

4

2 回答 2

11

Onion Architecture 与配置无关,它与执行有关。

只需创建一个IMapper 接口来执行映射,而不必担心配置。这适用于您的 ORM、IoC 容器和其他一切。

Also, Onion Architecture isn't about project structure, it's about the direction of your dependencies. Just call CreateMap in your UI. You can then define an IMapper interface all the way down in Core, with the other pieces implementing a version that delegates to AutoMapper.

于 2013-03-07T10:02:39.997 回答
3

you're abstracting away useful functionality that will cost you more time than you initially realize. Why not spend the time choosing a mapper and sticking with it?

Why is it so important that your UI doesnt know about your mapper? Assuming that you are using MVC, you are going to be flexing a lot of your chosen mappers functionality to flatten our your domain models to view models anyway.

Its the same kind of nonsense where people use generic repository implementations 'just in case' they decide to switch ORM mid project.

仔细选择您的基础架构并坚持下去。

于 2013-03-07T10:20:45.903 回答