AutoMapper: Mapper.Initialize() 只能调用一次,Why?

最开始的代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Reflection;
 7 
 8 using AutoMapper;
 9 
10 using Happy.ExtentionMethods;
11 using Happy.Bootstrap;
12 
13 namespace Happy.Bootstrap.AutoMapper
14 {
15     /// <summary>
16     /// 自动添加配置。
17     /// </summary>
18     public class AutoAddProfilePlugin : IBootstrapPlugin
19     {
20         /// <inheritdoc />
21         public void Start(IBootstrapService bootstrapService, Assembly assembly)
22         {
23             bootstrapService.MustNotNull("bootstrapService");
24             assembly.MustNotNull("assembly");
25 
26             foreach (var type in assembly.GetTypes())
27             {
28                 if (type.IsAbstract || type.IsInterface)
29                 {
30                     continue;
31                 }
32 
33                 if (typeof(Profile).IsAssignableFrom(type))
34                 {
35                     Mapper.Initialize(x => {
36                         x.AddProfile(Activator.CreateInstance(type) as Profile);
37                     });
38                 }
39             }
40         }
41     }
42 }

问题

我的项目中,每个 dll 都是自描述的,系统启动的时候,对每个 dll 对执行上面的插件,结果, Mapper.Initialize() 只有最后一次配置才有效,前面的配置会丢失。

最后的代码

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 using System.Reflection;
 7 
 8 using AutoMapper;
 9 
10 using Happy.ExtentionMethods;
11 using Happy.Bootstrap;
12 
13 namespace Happy.Bootstrap.AutoMapper
14 {
15     /// <summary>
16     /// 自动添加配置。
17     /// </summary>
18     public class AutoAddProfilePlugin : IBootstrapPlugin
19     {
20         /// <inheritdoc />
21         public void Start(IBootstrapService bootstrapService, Assembly assembly)
22         {
23             bootstrapService.MustNotNull("bootstrapService");
24             assembly.MustNotNull("assembly");
25 
26             foreach (var type in assembly.GetTypes())
27             {
28                 if (type.IsAbstract || type.IsInterface)
29                 {
30                     continue;
31                 }
32 
33                 if (typeof(Profile).IsAssignableFrom(type))
34                 {
35                     Mapper.AddProfile(Activator.CreateInstance(type) as Profile);
36                 }
37             }
38         }
39     }
40 }

 

AutoMapper.Extensions.Microsoft.DependencyInjection是AutoMapper的一个扩展,它使得在ASP.NET Core应用程序中使用AutoMapper更加容易。它提供了一种在依赖注入容器中注册AutoMapper的方法,并自动配置AutoMapper的常用设置。 使用AutoMapper.Extensions.Microsoft.DependencyInjection,只需在Startup.cs文件中添加以下代码即可注册AutoMapper: ```csharp using AutoMapper; using Microsoft.Extensions.DependencyInjection; public class Startup { // ... public void ConfigureServices(IServiceCollection services) { // 注册AutoMapper services.AddAutoMapper(typeof(Startup)); // ... } } ``` 在上面的代码中,我们使用AddAutoMapper方法向依赖注入容器中注册AutoMapper。我们传递了typeof(Startup)参数,这告诉AutoMapper从应用程序中的所有程序集中搜索映射配置。 使用AutoMapper.Extensions.Microsoft.DependencyInjection可以让我们更容易地在应用程序的不同部分中使用AutoMapper。例如,在控制器中,我们可以在构造函数中注入IMapper接口,然后使用它来执行实体到DTO的映射: ```csharp using AutoMapper; using Microsoft.AspNetCore.Mvc; public class PersonController : Controller { private readonly IMapper _mapper; private readonly IPersonRepository _personRepository; public PersonController(IMapper mapper, IPersonRepository personRepository) { _mapper = mapper; _personRepository = personRepository; } public IActionResult Index() { var people = _personRepository.GetAll(); var peopleDto = _mapper.Map<IEnumerable<PersonDto>>(people); return View(peopleDto); } } ``` 在上面的代码中,我们在控制器的构造函数中注入IMapper接口,并使用它来执行实体到DTO的映射。这使得我们可以将数据从数据库中检索到实体中,然后将其映射到DTO并传递给视图进行呈现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值