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

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

问题描述

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

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天全站免登陆