How to add AutoMapper to DI in .Net core 2.0 Console Application

后端 未结 3 957
遥遥无期
遥遥无期 2020-12-07 04:14

I try to use AutoMapper in my .Net Core 2.0 Console Application. My class looks like this:

public class AutoGetCurrency
{
    private readonly IMapper mapper         


        
相关标签:
3条回答
  • 2020-12-07 04:47

    Install the 'AutoMapper' and 'Microsoft.Extensions.DependencyInjection' package from NuGet.

    Implement User and UserDTO class for mapping from User to 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; }
    }
    

    Implement an interface and a class for doing Mapping operation as follows(don't forget using 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;
        }
    }
    

    After that create a RegisterServices method for doing Dependency Injection in your project and a DisposeServices method for disposing of the service provider.

    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();
               }
            }
        }
    }
    

    Good Luck

    0 讨论(0)
  • 2020-12-07 04:48

    You need to add this nuget package AutoMapper.Extensions.Microsoft.DependencyInjection to your project and add AutoMapper to your ServiceCollection like this

    var services = new ServiceCollection();
    services.AddAutoMapper(mapperConfig => mapperConfig.AddProfiles(GetType().Assembly))
    ServiceProvider = services.BuildServiceProvider();
    
    0 讨论(0)
  • 2020-12-07 04:56

    You need to map a concrete class that implements IMapper. I followed Automapper documentation and it worked fine, it would be more efficient to show their documentation than writing you code there and I don't want copy / paste their sample

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

    But it would be like this: services.AddTransient<IMapper,YourconcreteClass>();

    0 讨论(0)
提交回复
热议问题