在ASP.NET Core 1.0 MVC6中使用内置IoC配置AutoMapper 4.2

Bla*_*ell 21 c# asp.net automapper asp.net-core-1.0

我试图找出在我的应用程序的Startup.cs文件中配置AutoMapper的正确方法,然后在我的应用程序中使用它.

我正在尝试使用这个文档,它解释了如何在没有旧的静态API的情况下仍然给AutoMapper一个静态的感觉.该示例使用StructureMap.

我想知道如何做类似的事情,但是在使用内置服务容器的Core 1.0应用程序中.

我假设在配置函数中我将配置AutoMapper,然后在ConfigureServices函数中我将其添加为瞬态.

我假设最后最干净,最正确的方法是使用依赖注入.这是我目前的尝试,但它不起作用:

Startup.cs

public IMapper Mapper { get; set; }
private MapperConfiguration MapperConfiguration { get; set; }
public void ConfigureServices(IServiceCollection services)
{
     services.AddTransient<IMapper, Mapper>();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    MapperConfiguration MapperConfiguration = new MapperConfiguration(cfg =>
    {
        cfg.CreateMap<Product, ProductViewModel>().ReverseMap();
    });

    Mapper = MapperConfiguration.CreateMapper();
}
Run Code Online (Sandbox Code Playgroud)

在我的控制器中:

private IMapper _mapper { get; set; }
// Constructor
public ProductsController(IMapper mapper)
{
    _mapper = mapper;
}

public IActionResult Create(ProductViewModel vm)
{
    Product product = _mapper.Map<ProductViewModel, Product>(vm);
}
Run Code Online (Sandbox Code Playgroud)

它根本就不起作用......我必须错过一些步骤或做错事.

Chr*_*xon 37

这个答案适合围绕Controller层的MVC 6方法:

我从AutoMapper 4.1.1迁移到4.2.0,有一些问题找出错综复杂但最终到达那里.

首先,我将AutoMapper Profile构建分离为一个新类(见下文),以节省阻塞Startup类.

using AutoMapper;
using YourModels;
using YourViewModels;

namespace YourNamespace
{
    public class AutoMapperProfileConfiguration : Profile
    {
        protected override void Configure()
        {
            CreateMap<Application, ApplicationViewModel>();
            CreateMap<ApplicationViewModel, Application>();
            ...
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我对Startup类进行了以下修改.

我添加了MapperConfiguration类型的私有成员变量.

private MapperConfiguration _mapperConfiguration { get; set; }
Run Code Online (Sandbox Code Playgroud)

在Startup构造函数中,我添加了以下代码来实例化我的新AutoMapper Profile.

_mapperConfiguration = new MapperConfiguration(cfg =>
{
    cfg.AddProfile(new AutoMapperProfileConfiguration());
});
Run Code Online (Sandbox Code Playgroud)

ConfigureServices()我将新的AutoMapper配置文件放入Singleton中.

services.AddSingleton<IMapper>(sp => _mapperConfiguration.CreateMapper());
Run Code Online (Sandbox Code Playgroud)

然后,只需一个简单的操作即可将相关控制器注入其中.

using AutoMapper;
using ...

namespace YourNamespace
{
    public class ApplicationsController : BaseController
    {
        [FromServices]
        private IMapper _mapper { get; set; }

        [FromServices]
        private IApplicationRepository _applicationRepository { get; set; }

        public ApplicationsController(
            IMapper mapper,
            IApplicationRepository applicationRepository)
        {
            _mapper = mapper;
            _applicationRepository = applicationRepository;
        }

        // GET: Applications
        public async Task<IActionResult> Index()
        {
            IEnumerable<Application> applications = await _applicationRepository.GetForIdAsync(...);

            if (applications == null)
                return HttpNotFound();

            List<ApplicationViewModel> viewModel = _mapper.Map<List<ApplicationViewModel>>(applications);

            return View(viewModel);
        }

        ...
}
Run Code Online (Sandbox Code Playgroud)

感谢Rexebin在https://pintoservice.wordpress.com/2016/01/31/dependency-injection-for-automapper-4-2-in-asp-net-vnext-mvc-project/为他的帖子提供帮助enourmously.


Stu*_*art 7

您还可以使用automapper创建者的扩展包.

您可以传入配置,指定要扫描的程序集或不传递任何内容,并让它从DependencyContext扫描程序集.

https://github.com/AutoMapper/AutoMapper.Extensions.Microsoft.DependencyInjection

https://www.nuget.org/packages/AutoMapper.Extensions.Microsoft.DependencyInjection/

public void ConfigureServices(IServiceCollection services)
{
    //configure DI
    services.AddTransient<IFoo, Foo>();

    //Add automapper - scans for Profiles
    services.AddAutoMapper();
    //or specify
    services.AddAutoMapper(cfg =>
    {
        cfg.AddProfile<ViewModelProfile>();
        ...
    });
    ...
Run Code Online (Sandbox Code Playgroud)