如何在.Net core 2.0控制台应用程序中将AutoMapper添加到DI

时间:2018-01-20 22:45:04

标签: c# .net-core console-application automapper

我尝试在.Net Core 2.0控制台应用程序中使用AutoMapper。我的班级看起来像这样:

public class AutoGetCurrency
{
    private readonly IMapper mapper;
    public AutoGetCurrency(IMapper mapper) {
    this.mapper = mapper;
    }
    ....
}

我尝试将服务添加到方法中:

static void Main(string[] args)
{
    var services = new ServiceCollection();
    ServiceProvider = services.BuildServiceProvider();
    services.AddTransient<IMapper>();
    ....
}

但我有下一个错误&#39;发生了一个或多个错误。 (对象引用未设置为对象的实例。)&#39;

请告诉我如何添加AutoMapper以使用DI。

3 个答案:

答案 0 :(得分:6)

您需要将此nuget包AutoMapper.Extensions.Microsoft.DependencyInjection添加到您的项目中,并将AutoMapper添加到ServiceCollection这样的

var services = new ServiceCollection();
services.AddAutoMapper(mapperConfig => mapperConfig.AddProfiles(GetType().Assembly))
ServiceProvider = services.BuildServiceProvider();

答案 1 :(得分:2)

从NuGet安装'AutoMapper'和'Microsoft.Extensions.DependencyInjection'程序包。

实现用于从User到UserDTO映射的User和UserDTO类:

public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
}
public class UserDTO
{
    public int Id { get; set; }
    public string Name { get; set; }
}

实现如下接口和用于进行Mapping操作的类(不要忘记使用AutoMapper):

public interface ICustomMapper<T>
{
    T MapUserToUserDTO();
}
public class CustomMapper : ICustomMapper<UserDTO>
{
    private readonly IMapper mapper;
    public CustomMapper(IMapper mapper) =>
        this.mapper = mapper;
    public UserDTO MapUserToUserDTO()
    {
        User user = new User()
        {
           Id = 5,
           Name = "Iman"
        };
        UserDTO userDTO = mapper.Map<UserDTO>(user);
        return userDTO;
    }
}

之后,创建一个RegisterServices方法以在项目中进行依赖注入,并创建一个DisposeServices方法以处理服务提供者。

using AutoMapper;
using Microsoft.Extensions.DependencyInjection;

namespace AutoMapperProject
{
    class Program
    {
        private static IServiceProvider _serviceProvider;
        static void Main(string[] args)
        {
            RegisterServices();
            ICustomMapper<UserDTO> service = 
            _serviceProvider.GetService<ICustomMapper<UserDTO>>();
            UserDTO userDTO = service.MapUserToUserDTO();
            DisposeServices();
            Console.WriteLine("Hello AutoMapper!");
        }
        private static void RegisterServices()
        {
            var collection = new ServiceCollection();
            collection.AddAutoMapper(typeof(Program));
            collection.AddScoped<ICustomMapper<UserDTO>, CustomMapper>();
            // ...
            // Add other services
            // ...
            _serviceProvider = collection.BuildServiceProvider();
        }
        private static void DisposeServices()
        {
           if (_serviceProvider == null)
           {
              return;
           }
           if (_serviceProvider is IDisposable)
           {
              ((IDisposable)_serviceProvider).Dispose();
           }
        }
    }
}

祝你好运

答案 2 :(得分:0)

您需要映射实现IMapper的具体类。 我遵循Automapper文档,它工作正常,显示他们的文档比在那里编写代码更有效,我不想复制/粘贴他们的样本

http://automapper.readthedocs.io/en/latest/Dependency-injection.html

但它会是这样的:services.AddTransient<IMapper,YourconcreteClass>();