|
| 1 | +# Dependency Injection |
| 2 | + |
1 | 3 | ## Examples |
2 | 4 |
|
3 | 5 | ### ASP.NET Core |
@@ -28,181 +30,8 @@ There is a third-party [NuGet package](https://www.nuget.org/packages/AutoMapper |
28 | 30 |
|
29 | 31 | Also, check [this blog](https://dotnetfalcon.com/autofac-support-for-automapper/). |
30 | 32 |
|
31 | | -### Ninject |
32 | | - |
33 | | -For those using Ninject here is an example of a Ninject module for AutoMapper |
34 | | - |
35 | | -```c# |
36 | | -public class AutoMapperModule : NinjectModule |
37 | | -{ |
38 | | - public override void Load() |
39 | | - { |
40 | | - Bind<IValueResolver<SourceEntity, DestModel, bool>>().To<MyResolver>(); |
41 | | - |
42 | | - var mapperConfiguration = CreateConfiguration(); |
43 | | - Bind<MapperConfiguration>().ToConstant(mapperConfiguration).InSingletonScope(); |
44 | | - |
45 | | - // This teaches Ninject how to create automapper instances say if for instance |
46 | | - // MyResolver has a constructor with a parameter that needs to be injected |
47 | | - Bind<IMapper>().ToMethod(ctx => |
48 | | - new Mapper(mapperConfiguration, type => ctx.Kernel.Get(type))); |
49 | | - } |
50 | | - |
51 | | - private MapperConfiguration CreateConfiguration() |
52 | | - { |
53 | | - var config = new MapperConfiguration(cfg => |
54 | | - { |
55 | | - // Add all profiles in current assembly |
56 | | - cfg.AddMaps(GetType().Assembly); |
57 | | - }); |
58 | | - |
59 | | - return config; |
60 | | - } |
61 | | -} |
62 | | -``` |
63 | | - |
64 | | -### Simple Injector |
65 | | - |
66 | | -The workflow is as follows: |
67 | | - |
68 | | -1) Register your types via MyRegistrar.Register |
69 | | -2) The MapperProvider allows you to directly inject an instance of IMapper into your other classes |
70 | | -3) SomeProfile resolves a value using PropertyThatDependsOnIocValueResolver |
71 | | -4) PropertyThatDependsOnIocValueResolver has IService injected into it, which is then able to be used |
72 | | - |
73 | | -The ValueResolver has access to IService because we register our container via MapperConfigurationExpression.ConstructServicesUsing |
74 | | - |
75 | | -```c# |
76 | | -public class MyRegistrar |
77 | | -{ |
78 | | - public void Register(Container container) |
79 | | - { |
80 | | - // Injectable service |
81 | | - container.RegisterSingleton<IService, SomeService>(); |
82 | | - |
83 | | - // Automapper |
84 | | - container.RegisterSingleton(() => GetMapper(container)); |
85 | | - } |
86 | | - |
87 | | - private AutoMapper.IMapper GetMapper(Container container) |
88 | | - { |
89 | | - var mp = container.GetInstance<MapperProvider>(); |
90 | | - return mp.GetMapper(); |
91 | | - } |
92 | | -} |
93 | | - |
94 | | -public class MapperProvider |
95 | | -{ |
96 | | - private readonly Container _container; |
97 | | - |
98 | | - public MapperProvider(Container container) |
99 | | - { |
100 | | - _container = container; |
101 | | - } |
102 | | - |
103 | | - public IMapper GetMapper() |
104 | | - { |
105 | | - var mce = new MapperConfigurationExpression(); |
106 | | - mce.ConstructServicesUsing(_container.GetInstance); |
107 | | - |
108 | | - mce.AddMaps(typeof(SomeProfile).Assembly); |
109 | | - |
110 | | - var mc = new MapperConfiguration(mce); |
111 | | - mc.AssertConfigurationIsValid(); |
112 | | - |
113 | | - IMapper m = new Mapper(mc, t => _container.GetInstance(t)); |
114 | | - |
115 | | - return m; |
116 | | - } |
117 | | -} |
118 | | - |
119 | | -public class SomeProfile : Profile |
120 | | -{ |
121 | | - public SomeProfile() |
122 | | - { |
123 | | - var map = CreateMap<MySourceType, MyDestinationType>(); |
124 | | - map.ForMember(d => d.PropertyThatDependsOnIoc, opt => opt.MapFrom<PropertyThatDependsOnIocValueResolver>()); |
125 | | - } |
126 | | -} |
127 | | - |
128 | | -public class PropertyThatDependsOnIocValueResolver : IValueResolver<MySourceType, object, int> |
129 | | -{ |
130 | | - private readonly IService _service; |
131 | | - |
132 | | - public PropertyThatDependsOnIocValueResolver(IService service) |
133 | | - { |
134 | | - _service = service; |
135 | | - } |
| 33 | +### [Other DI engines](https://github.com/AutoMapper/AutoMapper/wiki/DI-examples) |
136 | 34 |
|
137 | | - int IValueResolver<MySourceType, object, int>.Resolve(MySourceType source, object destination, int destMember, ResolutionContext context) |
138 | | - { |
139 | | - return _service.MyMethod(source); |
140 | | - } |
141 | | -} |
142 | | -``` |
143 | | - |
144 | | -### Castle Windsor |
145 | | - |
146 | | -For those using Castle Windsor here is an example of an installer for AutoMapper |
147 | | - |
148 | | -```c# |
149 | | -public class AutoMapperInstaller : IWindsorInstaller |
150 | | -{ |
151 | | - public void Install(IWindsorContainer container, IConfigurationStore store) |
152 | | - { |
153 | | - // Register all mapper profiles |
154 | | - container.Register( |
155 | | - Classes.FromAssemblyInThisApplication(GetType().Assembly) |
156 | | - .BasedOn<Profile>().WithServiceBase()); |
157 | | - |
158 | | - // Register IConfigurationProvider with all registered profiles |
159 | | - container.Register(Component.For<IConfigurationProvider>().UsingFactoryMethod(kernel => |
160 | | - { |
161 | | - return new MapperConfiguration(configuration => |
162 | | - { |
163 | | - kernel.ResolveAll<Profile>().ToList().ForEach(configuration.AddProfile); |
164 | | - }); |
165 | | - }).LifestyleSingleton()); |
166 | | - |
167 | | - // Register IMapper with registered IConfigurationProvider |
168 | | - container.Register( |
169 | | - Component.For<IMapper>().UsingFactoryMethod(kernel => |
170 | | - new Mapper(kernel.Resolve<IConfigurationProvider>(), kernel.Resolve))); |
171 | | - } |
172 | | -} |
173 | | -``` |
174 | | - |
175 | | -### Catel.IoC |
176 | | - |
177 | | -For those using Catel.IoC here is how you register AutoMapper. First define the configuration using [profiles](Configuration.html#profile-instances). And then you let AutoMapper know in what assemblies those profiles are defined by registering AutoMapper in the ServiceLocator at startup: |
178 | | -```c# |
179 | | -ServiceLocator.Default.RegisterInstance(typeof(IMapper), new Mapper(CreateConfiguration())); |
180 | | -``` |
181 | | - |
182 | | -Configuration Creation Method: |
183 | | -```c# |
184 | | -public static MapperConfiguration CreateConfiguration() |
185 | | -{ |
186 | | - var config = new MapperConfiguration(cfg => |
187 | | - { |
188 | | - // Add all profiles in current assembly |
189 | | - cfg.AddMaps(GetType().Assembly); |
190 | | - }); |
191 | | - |
192 | | - return config; |
193 | | -} |
194 | | -``` |
195 | | - |
196 | | -Now you can inject AutoMapper at runtime into your services/controllers: |
197 | | -```c# |
198 | | -public class EmployeesController { |
199 | | - private readonly IMapper _mapper; |
200 | | - |
201 | | - public EmployeesController(IMapper mapper) => _mapper = mapper; |
202 | | - |
203 | | - // use _mapper.Map or _mapper.ProjectTo |
204 | | -} |
205 | | -``` |
206 | 35 | ## Low level API-s |
207 | 36 |
|
208 | 37 | AutoMapper supports the ability to construct [Custom Value Resolvers](Custom-value-resolvers.html), [Custom Type Converters](Custom-type-converters.html), and [Value Converters](Value-converters.html) using static service location: |
|
0 commit comments