如何从不同的程序集中在Automapper中注册配置文件? [英] How can register profiles in Automapper from different assemblies?

查看:61
本文介绍了如何从不同的程序集中在Automapper中注册配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有许多程序集的应用程序(NET Core):

I have an application (NET Core) with many assemblies:

  • WebAPI(包含视图模型并使用DTO)
  • 服务(包含DTO并使用Domain实体)

在WebAPI程序集上,我通过以下这一行自动注册了自动映射器配置文件:

On WebAPI assembly I registered automapper profiles automatically with this line:

services.AddAutoMapper();

通过这一行,我可以将视图模型转换为DTO(并向后)

With this line I can convert view models to DTO (and backwards)

但是我需要位于Services层的注册配置文件才能将DTO转换为Domain实体(以及向后转换)

But I need register profiles located on Services layer to convert DTO to Domain entities (and backwards)

很明显,Automapper找不到此配置文件.

Evidently, Automapper not found this profiles.

从不同程序集中注册配置文件的最佳方法是什么?

What's the best way to register profiles from different assemblies?

推荐答案

我使用services.AddAutoMapper(params Assembly []程序集).

I use services.AddAutoMapper(params Assembly[] assemblies).

例如:

services.AddAutoMapper(
    typeof(Startup).GetTypeInfo().Assembly, 
    typeof(Class_In_Other_Assembly).GetTypeInfo().Assembly
);

这篇关于如何从不同的程序集中在Automapper中注册配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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