Skip to content

Commit 42ef755

Browse files
committed
code reuse
1 parent a2c4ba0 commit 42ef755

File tree

4 files changed

+88
-320
lines changed

4 files changed

+88
-320
lines changed

src/AutoMapper/Configuration/MappingExpressionBase.cs

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace AutoMapper.Configuration
1010
{
1111
using static Expression;
12-
using static Execution.ExpressionBuilder;
1312
using Execution;
1413
[EditorBrowsable(EditorBrowsableState.Never)]
1514
public interface ITypeMapConfiguration
@@ -308,70 +307,40 @@ public TMappingExpression MaxDepth(int depth)
308307

309308
public TMappingExpression ConstructUsingServiceLocator()
310309
{
311-
TypeMapActions.Add(tm => tm.CustomCtorFunction = Lambda(ServiceLocator(tm.DestinationType)));
310+
TypeMapActions.Add(tm => tm.ConstructUsingServiceLocator());
312311

313312
return this as TMappingExpression;
314313
}
315314

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));
325316

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)
330318
{
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));
339320
return this as TMappingExpression;
340321
}
341322

323+
public TMappingExpression BeforeMap(Action<TSource, TDestination, ResolutionContext> beforeFunction) =>
324+
BeforeMapCore((src, dest, ctxt) => beforeFunction(src, dest, ctxt));
325+
342326
public TMappingExpression BeforeMap<TMappingAction>() where TMappingAction : IMappingAction<TSource, TDestination> =>
343327
BeforeMap(CallMapAction<TMappingAction>);
344328
public TMappingExpression AfterMap<TMappingAction>() where TMappingAction : IMappingAction<TSource, TDestination> =>
345329
AfterMap(CallMapAction<TMappingAction>);
346330
private static void CallMapAction<TMappingAction>(TSource source, TDestination destination, ResolutionContext context) =>
347331
((IMappingAction<TSource, TDestination>)context.CreateInstance(typeof(TMappingAction))).Process(source, destination, context);
348332

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));
361334

362-
public TMappingExpression AfterMap(Action<TSource, TDestination, ResolutionContext> afterFunction)
335+
private TMappingExpression AfterMapCore(Expression<Action<TSource, TDestination, ResolutionContext>> expr)
363336
{
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));
372338
return this as TMappingExpression;
373339
}
374340

341+
public TMappingExpression AfterMap(Action<TSource, TDestination, ResolutionContext> afterFunction) =>
342+
AfterMapCore((src, dest, ctxt) => afterFunction(src, dest, ctxt));
343+
375344
public TMappingExpression PreserveReferences()
376345
{
377346
TypeMapActions.Add(tm => tm.PreserveReferences = true);
@@ -460,26 +429,17 @@ public void ConvertUsing(Type typeConverterType)
460429
TypeMapActions.Add(tm => tm.TypeConverter = new ClassTypeConverter(typeConverterType, tm.Types.ITypeConverter()));
461430
}
462431

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));
472433

473-
public void ConvertUsing(Func<TSource, TDestination, ResolutionContext, TDestination> mappingFunction)
434+
private void ConvertUsingCore(Expression<Func<TSource, TDestination, ResolutionContext, TDestination>> expr)
474435
{
475436
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));
481438
}
482439

440+
public void ConvertUsing(Func<TSource, TDestination, ResolutionContext, TDestination> mappingFunction) =>
441+
ConvertUsingCore((src, dest, ctxt) => mappingFunction(src, dest, ctxt));
442+
483443
public void ConvertUsing(ITypeConverter<TSource, TDestination> converter) => ConvertUsing(converter.Convert);
484444

485445
public void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>

0 commit comments

Comments
 (0)