|
9 | 9 | namespace AutoMapper.Configuration |
10 | 10 | { |
11 | 11 | using static Expression; |
12 | | - using static Execution.ExpressionBuilder; |
13 | 12 | using Execution; |
14 | 13 | [EditorBrowsable(EditorBrowsableState.Never)] |
15 | 14 | public interface ITypeMapConfiguration |
@@ -308,70 +307,40 @@ public TMappingExpression MaxDepth(int depth) |
308 | 307 |
|
309 | 308 | public TMappingExpression ConstructUsingServiceLocator() |
310 | 309 | { |
311 | | - TypeMapActions.Add(tm => tm.CustomCtorFunction = Lambda(ServiceLocator(tm.DestinationType))); |
| 310 | + TypeMapActions.Add(tm => tm.ConstructUsingServiceLocator()); |
312 | 311 |
|
313 | 312 | return this as TMappingExpression; |
314 | 313 | } |
315 | 314 |
|
316 | | - public TMappingExpression BeforeMap(Action<TSource, TDestination> beforeFunction) |
317 | | - { |
318 | | - TypeMapActions.Add(tm => |
319 | | - { |
320 | | - Expression<Action<TSource, TDestination, ResolutionContext>> expr = |
321 | | - (src, dest, ctxt) => beforeFunction(src, dest); |
322 | | - |
323 | | - tm.AddBeforeMapAction(expr); |
324 | | - }); |
| 315 | + public TMappingExpression BeforeMap(Action<TSource, TDestination> beforeFunction) => BeforeMapCore((src, dest, ctxt) => beforeFunction(src, dest)); |
325 | 316 |
|
326 | | - return this as TMappingExpression; |
327 | | - } |
328 | | - |
329 | | - public TMappingExpression BeforeMap(Action<TSource, TDestination, ResolutionContext> beforeFunction) |
| 317 | + private TMappingExpression BeforeMapCore(Expression<Action<TSource, TDestination, ResolutionContext>> expr) |
330 | 318 | { |
331 | | - TypeMapActions.Add(tm => |
332 | | - { |
333 | | - Expression<Action<TSource, TDestination, ResolutionContext>> expr = |
334 | | - (src, dest, ctxt) => beforeFunction(src, dest, ctxt); |
335 | | - |
336 | | - tm.AddBeforeMapAction(expr); |
337 | | - }); |
338 | | - |
| 319 | + TypeMapActions.Add(tm => tm.AddBeforeMapAction(expr)); |
339 | 320 | return this as TMappingExpression; |
340 | 321 | } |
341 | 322 |
|
| 323 | + public TMappingExpression BeforeMap(Action<TSource, TDestination, ResolutionContext> beforeFunction) => |
| 324 | + BeforeMapCore((src, dest, ctxt) => beforeFunction(src, dest, ctxt)); |
| 325 | + |
342 | 326 | public TMappingExpression BeforeMap<TMappingAction>() where TMappingAction : IMappingAction<TSource, TDestination> => |
343 | 327 | BeforeMap(CallMapAction<TMappingAction>); |
344 | 328 | public TMappingExpression AfterMap<TMappingAction>() where TMappingAction : IMappingAction<TSource, TDestination> => |
345 | 329 | AfterMap(CallMapAction<TMappingAction>); |
346 | 330 | private static void CallMapAction<TMappingAction>(TSource source, TDestination destination, ResolutionContext context) => |
347 | 331 | ((IMappingAction<TSource, TDestination>)context.CreateInstance(typeof(TMappingAction))).Process(source, destination, context); |
348 | 332 |
|
349 | | - public TMappingExpression AfterMap(Action<TSource, TDestination> afterFunction) |
350 | | - { |
351 | | - TypeMapActions.Add(tm => |
352 | | - { |
353 | | - Expression<Action<TSource, TDestination, ResolutionContext>> expr = |
354 | | - (src, dest, ctxt) => afterFunction(src, dest); |
355 | | - |
356 | | - tm.AddAfterMapAction(expr); |
357 | | - }); |
358 | | - |
359 | | - return this as TMappingExpression; |
360 | | - } |
| 333 | + public TMappingExpression AfterMap(Action<TSource, TDestination> afterFunction) => AfterMapCore((src, dest, ctxt) => afterFunction(src, dest)); |
361 | 334 |
|
362 | | - public TMappingExpression AfterMap(Action<TSource, TDestination, ResolutionContext> afterFunction) |
| 335 | + private TMappingExpression AfterMapCore(Expression<Action<TSource, TDestination, ResolutionContext>> expr) |
363 | 336 | { |
364 | | - TypeMapActions.Add(tm => |
365 | | - { |
366 | | - Expression<Action<TSource, TDestination, ResolutionContext>> expr = |
367 | | - (src, dest, ctxt) => afterFunction(src, dest, ctxt); |
368 | | - |
369 | | - tm.AddAfterMapAction(expr); |
370 | | - }); |
371 | | - |
| 337 | + TypeMapActions.Add(tm => tm.AddAfterMapAction(expr)); |
372 | 338 | return this as TMappingExpression; |
373 | 339 | } |
374 | 340 |
|
| 341 | + public TMappingExpression AfterMap(Action<TSource, TDestination, ResolutionContext> afterFunction) => |
| 342 | + AfterMapCore((src, dest, ctxt) => afterFunction(src, dest, ctxt)); |
| 343 | + |
375 | 344 | public TMappingExpression PreserveReferences() |
376 | 345 | { |
377 | 346 | TypeMapActions.Add(tm => tm.PreserveReferences = true); |
@@ -460,26 +429,17 @@ public void ConvertUsing(Type typeConverterType) |
460 | 429 | TypeMapActions.Add(tm => tm.TypeConverter = new ClassTypeConverter(typeConverterType, tm.Types.ITypeConverter())); |
461 | 430 | } |
462 | 431 |
|
463 | | - public void ConvertUsing(Func<TSource, TDestination, TDestination> mappingFunction) |
464 | | - { |
465 | | - HasTypeConverter = true; |
466 | | - TypeMapActions.Add(tm => |
467 | | - { |
468 | | - Expression<Func<TSource, TDestination, ResolutionContext, TDestination>> expr = (src, dest, ctxt) => mappingFunction(src, dest); |
469 | | - tm.TypeConverter = new LambdaTypeConverter(expr); |
470 | | - }); |
471 | | - } |
| 432 | + public void ConvertUsing(Func<TSource, TDestination, TDestination> mappingFunction) => ConvertUsingCore((src, dest, ctxt) => mappingFunction(src, dest)); |
472 | 433 |
|
473 | | - public void ConvertUsing(Func<TSource, TDestination, ResolutionContext, TDestination> mappingFunction) |
| 434 | + private void ConvertUsingCore(Expression<Func<TSource, TDestination, ResolutionContext, TDestination>> expr) |
474 | 435 | { |
475 | 436 | HasTypeConverter = true; |
476 | | - TypeMapActions.Add(tm => |
477 | | - { |
478 | | - Expression<Func<TSource, TDestination, ResolutionContext, TDestination>> expr = (src, dest, ctxt) => mappingFunction(src, dest, ctxt); |
479 | | - tm.TypeConverter = new LambdaTypeConverter(expr); |
480 | | - }); |
| 437 | + TypeMapActions.Add(tm => tm.TypeConverter = new LambdaTypeConverter(expr)); |
481 | 438 | } |
482 | 439 |
|
| 440 | + public void ConvertUsing(Func<TSource, TDestination, ResolutionContext, TDestination> mappingFunction) => |
| 441 | + ConvertUsingCore((src, dest, ctxt) => mappingFunction(src, dest, ctxt)); |
| 442 | + |
483 | 443 | public void ConvertUsing(ITypeConverter<TSource, TDestination> converter) => ConvertUsing(converter.Convert); |
484 | 444 |
|
485 | 445 | public void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination> |
|
0 commit comments