-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Adding support for DI-enabled destination factories. #4603
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,55 +55,11 @@ When using `AddAutoMapper`, AutoMapper will automatically register implementatio | |
| - `IMemberValueResolver<TSource, TDestination, TSourceMember, TDestMember>` | ||
| - `ITypeConverter<TSource, TDestination>` | ||
| - `IValueConverter<TSourceMember, TDestinationMember>` | ||
| - `IDestinationFactory<TSource, TDestination>` | ||
| - `ICondition<TSource, TDestination, TMember>` | ||
|
jbogard marked this conversation as resolved.
|
||
| - `IPreCondition<TSource, TDestination>` | ||
| - `IMappingAction<TSource, TDestination>` | ||
|
|
||
| This allows you to use class-based conditions with dependency injection: | ||
|
|
||
| ```c# | ||
| public class MyCondition : ICondition<Source, Destination, int> | ||
| { | ||
| private readonly IMyService _myService; | ||
|
|
||
| public MyCondition(IMyService myService) | ||
| { | ||
| _myService = myService; | ||
| } | ||
|
|
||
| public bool Evaluate(Source source, Destination destination, int sourceMember, | ||
| int destMember, ResolutionContext context) | ||
| { | ||
| return _myService.ShouldMap(sourceMember); | ||
| } | ||
| } | ||
|
|
||
| public class ConditionProfile : Profile | ||
| { | ||
| public ConditionProfile() | ||
| { | ||
| CreateMap<Source, Destination>() | ||
| .ForMember(d => d.Value, o => | ||
| { | ||
| o.Condition<MyCondition>(); | ||
| o.MapFrom(s => s.Value); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| // In Startup.cs / Program.cs: | ||
| services.AddTransient<IMyService, MyService>(); | ||
| services.AddAutoMapper(cfg => { }, typeof(ConditionProfile).Assembly); | ||
| ``` | ||
|
|
||
| Or dynamic service location, to be used in the case of instance-based containers (including child/nested containers): | ||
|
|
||
| ```c# | ||
| var mapper = new Mapper(configuration, childContainer.GetInstance); | ||
|
|
||
| var dest = mapper.Map<Source, Destination>(new Source { Value = 15 }); | ||
| ``` | ||
|
|
||
| ## Queryable Extensions | ||
|
Comment on lines
-37
to
63
|
||
|
|
||
| Starting with 8.0 you can use `IMapper.ProjectTo`. For older versions you need to pass the configuration to the extension method ``` IQueryable.ProjectTo<T>(IConfigurationProvider) ```. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -157,6 +157,20 @@ public interface IMappingExpressionBase<TSource, TDestination, out TMappingExpre | |
| /// <returns>Itself</returns> | ||
| TMappingExpression ConstructUsing(Func<TSource, ResolutionContext, TDestination> ctor); | ||
| /// <summary> | ||
| /// Supply a custom object constructor type for instantiating the destination type with dependency injection support | ||
| /// </summary> | ||
| /// <remarks>Not used for LINQ projection (ProjectTo). Constructor is resolved from the DI container at configuration time.</remarks> | ||
| /// <typeparam name="TConstructor">Constructor type implementing IDestinationFactory<TSource, TDestination></typeparam> | ||
| /// <returns>Itself</returns> | ||
| TMappingExpression ConstructUsing<TConstructor>() where TConstructor : IDestinationFactory<TSource, TDestination>; | ||
|
jbogard marked this conversation as resolved.
Outdated
|
||
| /// <summary> | ||
| /// Supply a custom object constructor type for instantiating the destination type with dependency injection support. | ||
| /// Used when the constructor type is not known at compile-time. | ||
| /// </summary> | ||
| /// <remarks>Not used for LINQ projection (ProjectTo). Constructor is resolved from the DI container at configuration time.</remarks> | ||
|
jbogard marked this conversation as resolved.
Outdated
|
||
| /// <param name="objectConstructorType">Constructor type implementing IDestinationFactory</param> | ||
| void ConstructUsing(Type objectConstructorType); | ||
|
Comment on lines
+160
to
+172
|
||
| /// <summary> | ||
| /// Override the destination type mapping for looking up configuration and instantiation | ||
| /// </summary> | ||
| /// <param name="typeOverride"></param> | ||
|
|
@@ -197,6 +211,22 @@ public interface IMappingExpressionBase<TSource, TDestination, out TMappingExpre | |
| /// <typeparam name="TTypeConverter">Type converter type</typeparam> | ||
| void ConvertUsing<TTypeConverter>() where TTypeConverter : ITypeConverter<TSource, TDestination>; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Custom destination factory for instantiating destination objects with dependency injection support. | ||
| /// </summary> | ||
| /// <typeparam name="TSource">Source type</typeparam> | ||
| /// <typeparam name="TDestination">Destination type</typeparam> | ||
| public interface IDestinationFactory<in TSource, out TDestination> | ||
| { | ||
| /// <summary> | ||
| /// Construct the destination object from the source object | ||
| /// </summary> | ||
| /// <param name="source">Source object</param> | ||
| /// <param name="context">Resolution context</param> | ||
| /// <returns>New destination instance</returns> | ||
| TDestination Construct(TSource source, ResolutionContext context); | ||
| } | ||
| /// <summary> | ||
| /// Custom mapping action | ||
| /// </summary> | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,8 @@ namespace AutoMapper; | |||||
|
|
||||||
| using Features; | ||||||
| using System.Runtime.CompilerServices; | ||||||
| using static Expression; | ||||||
|
jbogard marked this conversation as resolved.
|
||||||
| using static ExpressionBuilder; | ||||||
|
Comment on lines
+5
to
+6
|
||||||
| using static Expression; | |
| using static ExpressionBuilder; |
Uh oh!
There was an error while loading. Please reload this page.