AutoMapper extensions for Microsoft DI 5.0 released

综合编程 2018-07-19 阅读原文

Hot off the presses, with some pretty big breaking changes. For the unfamiliar, the AutoMapper.Extensions.Microsoft.DependencyInjection
package is the extension to the MS DI libraries used by ASP.NET Core and others. It lets you do:

services.AddMvc();  
services.AddAutoMapper();

And this package finds all AutoMapper configuration in the current AppDomain, or assemblies you specify, configure everything, and register the AutoMapper-specific services with the container.

In the past, I've supported both the static- and instance-based means of using AutoMapper with this library. However, because of a number of issues, the static version is fundamentally incompatible with dependency injection, making it far to easy to make mistakes.

This version removes the support for static usage of AutoMapper, and you now must
inject an IMapper
for instance mapping or IConfigurationProvider
for LINQ projections ( ProjectTo
).

It's a bit of a breaking change, but I think for the better as it removes this wonky static global instance of AutoMapper that's at odds with how anyone using ASP.NET Core/MS DI deals with any other dependency.

To migrate, you'll need to replace all instances of using the static Mapper
class with an IMapper
dependency. I've also removed the configuration option to force the instance-based registration, so if you use that, just remove it.

The moral of the story - if you're doing DI, don't go halfway.

责编内容by:Jimmy Bogard 【阅读原文】。感谢您的支持!

您可能感兴趣的

Contoso University Examples with CQRS, MediatR, Au... I was a bit surprised yesterday to see Scott Hanselman's post on my Contoso University sample app . That application...
8分钟学会使用AutoMapper 一.什么是AutoMapper与为什么用它。 它是一种对象与对象之间的映射器,让AutoMapper有意思的就是在于它提供了一些将类型A映射到类型B这种无聊的实例,只要B遵循AutoMapper已经建立的惯例,那么大多数情况下就可以进行...
AutoMapper – Declarative data mapper for PHP 7 AutoMapper can map data from array/object to existing array/object or marshal a new one. Mapping rules spe...
Importing Automapper into a web storage project an... I am trying to use the ts automapper libary in an Angular2 webpack project but not sure how to import it or include ...
ABP框架 – 介绍 在14,15年间带领几个不同的团队,交付了几个项目,在这个过程中,虽然几个项目的业务不一样,但是很多应用程序架构基础性的功能却是大同小异,例如认证、授权、请求验证、异常处理、DTO、日志、审计、定时任务、调度、多语言、应用配置管理等等这些功...