我想在我的业务层中放置 Automapper 配置文件 [英] I want to place Automapper profile in my Business Layer

查看:39
本文介绍了我想在我的业务层中放置 Automapper 配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个 web api 核心 2.0 应用程序.我有我的主应用程序和业务层.我想将 automapper 配置文件放在业务层中,以便所有映射都在业务层中进行.我的业务层只是一个类库项目.

I've created a web api core 2.0 application. I've got my main app and the Business Layer. I want to place the automapper profile in the business layer so that all the mappings are made in the business layer. My business layer is just a class library project.

这可能吗?还是我需要将所有映射放在主应用程序的 Profile 类中?

Is this possible? or do I need to place all my mapping in a Profile class in the main app?

理论上的解释会有所帮助.

Just a theoretical explanation can help.

推荐答案

是的,这是可能的,但这取决于模型类所在的位置.

Yes, it's possible but it depends on where the model classes reside.

您可以为每个层或项目提供一个 Profile,您可以在其中映射适当的模型类.然后在要使用映射器的项目中,创建 ObjectMapper 类以加载配置文件.

You can give each layer or project a Profile where you map the appropriate model classes. Then in the project where you want to use the mapper, create the ObjectMapper class to load the Profiles.

namespace BL.Config
{
    public class MapperProfile : Profile
    {
        public MapperProfile()
        {
            CreateMap<Entity, Dto>();
            ...
        }
    }

    public class ObjectMapper
    {
        public static IMapper Mapper
        {
            get { return mapper.Value; }
        }

        public static IConfigurationProvider Configuration
        {
            get { return config.Value; }
        }

        public static Lazy<IMapper> mapper = new Lazy<IMapper>(() =>
        {
            var mapper = new Mapper(Configuration);
            return mapper;
        });

        public static Lazy<IConfigurationProvider> config = new Lazy<IConfigurationProvider>(() =>
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.AddProfile<BL.Config.MapperProfile>();
                cfg.AddProfile<AppCore.Config.MapperProfile>();  // any other profiles you need to use
            });

            return config;
        });
    }
}

当我需要使用 AutoMapper 时,我使用 ObjectMapper.Mapper 来获取我的映射器实例.我喜欢将其添加到抽象服务中.

When I need to use AutoMapper, I use the ObjectMapper.Mapper to get my mapper instance. I like to add this to an abstract service.

public interface IAutoMapperService
{
    IMapper Mapper { get; }
}

public abstract class AutoMapperService : IAutoMapperService
{
    public IMapper Mapper
    {
        get { return BAL.Config.ObjectMapper.Mapper; }
    }
}

和用法:该服务具有 Mapper 成员.

And usage: The service has the Mapper member.

public class SomeService : AutoMapperService, ISomeService
{
    public Foo GetFoo()
    {
        var foo = Mapper.Map<Foo>(bar);
        return foo;
    }
}

或者,如果您不能继承另一个基类,则只需实现 IAutoMapperService.

Or just implement the IAutoMapperService if you can't inherit another base class.

缺点是 BL 需要 AutoMapper 依赖项.但是使用这种方式,我发现我可以对其他层隐藏许多模型.

The downside is BL requires the AutoMapper dependency. But using this way I find I can hide many models from the other layers.

这篇关于我想在我的业务层中放置 Automapper 配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆