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

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

问题描述

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

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

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

在 WebAPI 程序集上,我使用这一行自动注册了 automapper 配置文件:

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)

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

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