如何在 Automapper 中注册来自不同程序集的配置文件?

Ser*_*gio 3 dependencies automapper .net-core

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

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

在 WebAPI 程序集中,我使用以下行自动注册了自动映射器配置文件:

services.AddAutoMapper();
Run Code Online (Sandbox Code Playgroud)

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

但我需要注册位于服务层的配置文件以将 DTO 转换为域实体(以及向后)

显然,Automapper 没有找到这个配置文件。

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

小智 7

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

例如:

services.AddAutoMapper(
    typeof(Startup).GetTypeInfo().Assembly, 
    typeof(Class_In_Other_Assembly).GetTypeInfo().Assembly
);
Run Code Online (Sandbox Code Playgroud)