|
1 | | -using System; |
2 | | - |
3 | | -namespace TestApp |
| 1 | +using AutoMapper; |
| 2 | +using AutoMapper.Internal; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | + |
| 5 | +IServiceCollection services = new ServiceCollection(); |
| 6 | +services.AddTransient<ISomeService>(sp => new FooService(5)); |
| 7 | +services.AddAutoMapper(typeof(Source)); |
| 8 | +var provider = services.BuildServiceProvider(); |
| 9 | +using (var scope = provider.CreateScope()) |
4 | 10 | { |
5 | | - using AutoMapper; |
6 | | - using AutoMapper.Internal; |
7 | | - using Microsoft.Extensions.DependencyInjection; |
| 11 | + var mapper = scope.ServiceProvider.GetRequiredService<IMapper>(); |
8 | 12 |
|
9 | | - public class Program |
| 13 | + foreach (var typeMap in mapper.ConfigurationProvider.Internal().GetAllTypeMaps()) |
10 | 14 | { |
11 | | - public static void Main(string[] args) |
12 | | - { |
13 | | - IServiceCollection services = new ServiceCollection(); |
14 | | - services.AddTransient<ISomeService>(sp => new FooService(5)); |
15 | | - services.AddAutoMapper(typeof(Source)); |
16 | | - var provider = services.BuildServiceProvider(); |
17 | | - using (var scope = provider.CreateScope()) |
18 | | - { |
19 | | - var mapper = scope.ServiceProvider.GetService<IMapper>(); |
20 | | - |
21 | | - foreach (var typeMap in mapper.ConfigurationProvider.Internal().GetAllTypeMaps()) |
22 | | - { |
23 | | - Console.WriteLine($"{typeMap.SourceType.Name} -> {typeMap.DestinationType.Name}"); |
24 | | - } |
25 | | - |
26 | | - foreach (var service in services) |
27 | | - { |
28 | | - Console.WriteLine(service.ServiceType + " - " + service.ImplementationType); |
29 | | - } |
30 | | - |
31 | | - var dest = mapper.Map<Dest2>(new Source2()); |
32 | | - Console.WriteLine(dest.ResolvedValue); |
33 | | - } |
34 | | - |
35 | | - Console.ReadKey(); |
36 | | - } |
| 15 | + Console.WriteLine($"{typeMap.SourceType.Name} -> {typeMap.DestinationType.Name}"); |
37 | 16 | } |
38 | 17 |
|
39 | | - public class Source |
| 18 | + foreach (var service in services) |
40 | 19 | { |
| 20 | + Console.WriteLine(service.ServiceType + " - " + service.ImplementationType); |
41 | 21 | } |
42 | 22 |
|
43 | | - public class Dest |
44 | | - { |
45 | | - } |
| 23 | + var dest = mapper.Map<Dest2>(new Source2()); |
| 24 | + Console.WriteLine(dest.ResolvedValue); |
| 25 | +} |
46 | 26 |
|
47 | | - public class Source2 |
48 | | - { |
49 | | - } |
| 27 | +Console.ReadKey(); |
50 | 28 |
|
51 | | - public class Dest2 |
52 | | - { |
53 | | - public int ResolvedValue { get; set; } |
54 | | - } |
| 29 | +public class Source |
| 30 | +{ |
| 31 | +} |
55 | 32 |
|
56 | | - public class Profile1 : Profile |
57 | | - { |
58 | | - public Profile1() |
59 | | - { |
60 | | - CreateMap<Source, Dest>(); |
61 | | - } |
62 | | - } |
| 33 | +public class Dest |
| 34 | +{ |
| 35 | +} |
63 | 36 |
|
64 | | - public class Profile2 : Profile |
| 37 | +public class Source2 |
| 38 | +{ |
| 39 | +} |
| 40 | + |
| 41 | +public class Dest2 |
| 42 | +{ |
| 43 | + public int ResolvedValue { get; set; } |
| 44 | +} |
| 45 | + |
| 46 | +public class Profile1 : Profile |
| 47 | +{ |
| 48 | + public Profile1() |
65 | 49 | { |
66 | | - public Profile2() |
67 | | - { |
68 | | - CreateMap<Source2, Dest2>() |
69 | | - .ForMember(d => d.ResolvedValue, opt => opt.MapFrom<DependencyResolver>()); |
70 | | - } |
| 50 | + CreateMap<Source, Dest>(); |
71 | 51 | } |
| 52 | +} |
72 | 53 |
|
73 | | - public class DependencyResolver : IValueResolver<object, object, int> |
| 54 | +public class Profile2 : Profile |
| 55 | +{ |
| 56 | + public Profile2() |
74 | 57 | { |
75 | | - private readonly ISomeService _service; |
| 58 | + CreateMap<Source2, Dest2>() |
| 59 | + .ForMember(d => d.ResolvedValue, opt => opt.MapFrom<DependencyResolver>()); |
| 60 | + } |
| 61 | +} |
76 | 62 |
|
77 | | - public DependencyResolver(ISomeService service) |
78 | | - { |
79 | | - _service = service; |
80 | | - } |
| 63 | +public class DependencyResolver : IValueResolver<object, object, int> |
| 64 | +{ |
| 65 | + private readonly ISomeService _service; |
81 | 66 |
|
82 | | - public int Resolve(object source, object destination, int destMember, ResolutionContext context) |
83 | | - { |
84 | | - return _service.Modify(destMember); |
85 | | - } |
| 67 | + public DependencyResolver(ISomeService service) |
| 68 | + { |
| 69 | + _service = service; |
86 | 70 | } |
87 | 71 |
|
88 | | - public interface ISomeService |
| 72 | + public int Resolve(object source, object destination, int destMember, ResolutionContext context) |
89 | 73 | { |
90 | | - int Modify(int value); |
| 74 | + return _service.Modify(destMember); |
91 | 75 | } |
| 76 | +} |
92 | 77 |
|
93 | | - public class FooService : ISomeService |
94 | | - { |
95 | | - private readonly int _value; |
| 78 | +public interface ISomeService |
| 79 | +{ |
| 80 | + int Modify(int value); |
| 81 | +} |
96 | 82 |
|
97 | | - public FooService(int value) |
98 | | - { |
99 | | - _value = value; |
100 | | - } |
| 83 | +public class FooService : ISomeService |
| 84 | +{ |
| 85 | + private readonly int _value; |
101 | 86 |
|
102 | | - public int Modify(int value) => value + _value; |
| 87 | + public FooService(int value) |
| 88 | + { |
| 89 | + _value = value; |
103 | 90 | } |
| 91 | + |
| 92 | + public int Modify(int value) => value + _value; |
104 | 93 | } |
| 94 | + |
0 commit comments