项目作者: alsami

项目描述 :
Autofac plug-in for AutoMapper.
高级语言: C#
项目地址: git://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection.git
创建时间: 2019-05-16T06:24:36Z
项目社区:https://github.com/alsami/AutoMapper.Contrib.Autofac.DependencyInjection

开源协议:MIT License

下载


AutoMapper.Contrib.Autofac.DependencyInjection

Build Application
codecov

NuGet
NuGet

This is a cross platform library, written in .netstandard 2.0, that serves as an extension for autofac’s containerbuilder.
It will register all necessary classes and interfaces of Jimmy Bogard’s AutoMapper implementation to the autofac-container
so you can use AutoMapper and object-mapping right ahead without worrying about setting up required infrastructure code.

Installation

This package is available via nuget. You can install it using Visual-Studio-Nuget-Browser or by using the dotnet-cli

  1. dotnet add package AutoMapper.Contrib.Autofac.DependencyInjection

If you want to add a specific version of this package

  1. dotnet add package AutoMapper.Contrib.Autofac.DependencyInjection --version 1.0.0

For more information please visit the official dotnet-cli documentation.

Usage sample

After installing the package you define your entities and dtos and create profiles for them.

  1. public class Customer
  2. {
  3. public Guid Id { get; }
  4. public string Name { get; }
  5. public Customer(Guid id, string name)
  6. {
  7. Id = id;
  8. Name = name;
  9. }
  10. }
  11. public class CustomerDto
  12. {
  13. public Guid Id { get; }
  14. public string Name { get; }
  15. public CustomerDto(Guid id, string name)
  16. {
  17. Id = id;
  18. Name = name;
  19. }
  20. }
  21. public class CustomerProfile : Profile
  22. {
  23. public CustomerProfile()
  24. {
  25. CreateMap<Customer, CustomerDto>()
  26. .ConstructUsing(user => new UserDto(user.Id, user.Name))
  27. .ReverseMap()
  28. .ConstructUsing(userDto => new User(userDto.Id, userDto.Name));
  29. }
  30. }
  31. public static class Program
  32. {
  33. public static void Main(string args[])
  34. {
  35. var containerBuilder = new ContainerBuilder();
  36. // here you have to pass in the assembly (or assemblies) containing AutoMapper types
  37. // stuff like profiles, resolvers and type-converters will be added by this function
  38. containerBuilder.RegisterAutoMapper(typeof(Program).Assembly);
  39. var container = containerBuilder.Build();
  40. var mapper = container.Resolve<IMapper>();
  41. var customer = new Customer(Guid.NewGuid(), "Google");
  42. var customerDto = mapper.Map<CustomerDto>(customer);
  43. }
  44. }

Support for Property Injection

You can set propertiesAutowired to true to enable property based injection, just modify the prior example like so:

  1. public static class Program
  2. {
  3. public static void Main(string args[])
  4. {
  5. var containerBuilder = new ContainerBuilder();
  6. // Update this line with the setting:
  7. containerBuilder.RegisterAutoMapper(typeof(Program).Assembly, propertiesAutowired: true);
  8. var container = containerBuilder.Build();
  9. var mapper = container.Resolve<IMapper>();
  10. var customer = new Customer(Guid.NewGuid(), "Google");
  11. var customerDto = mapper.Map<CustomerDto>(customer);
  12. }
  13. }

Validating your configuration

AutoMapper allows the user to validate their mappings. This should only be done within a test project, since it’s time-consuming

  1. var containerBuilder = new ContainerBuilder();
  2. containerBuilder.RegisterAutoMapper(typeof(Program).Assembly);
  3. var container = containerBuilder.Build();
  4. var mapperConfiguration = container.Resolve<MapperConfiguration>();
  5. // this line will throw when mappings are not working as expected
  6. // it's wise to write a test for that, which is always executed within a CI pipeline for your project.
  7. mapperConfiguration.AssertConfigurationIsValid();