diff --git a/src/AutoMapper/Configuration/MapperConfigurationExpression.cs b/src/AutoMapper/Configuration/MapperConfigurationExpression.cs index 4fc9e7543c..69f616d2e4 100644 --- a/src/AutoMapper/Configuration/MapperConfigurationExpression.cs +++ b/src/AutoMapper/Configuration/MapperConfigurationExpression.cs @@ -28,7 +28,7 @@ public void CreateProfile(string profileName, Action config) public AdvancedConfiguration Advanced { get; } = new AdvancedConfiguration(); - public GlobalFeatures Features { get; } = new GlobalFeatures(); + public Features Features { get; } = new Features(); private class NamedProfile : Profile { diff --git a/src/AutoMapper/Configuration/MappingExpressionBase.cs b/src/AutoMapper/Configuration/MappingExpressionBase.cs index ea724ccbfd..10356d103e 100644 --- a/src/AutoMapper/Configuration/MappingExpressionBase.cs +++ b/src/AutoMapper/Configuration/MappingExpressionBase.cs @@ -28,7 +28,7 @@ protected MappingExpressionBase(MemberList memberList, TypePair types) public bool IsOpenGeneric { get; } public Type SourceType => Types.SourceType; public Type DestinationType => Types.DestinationType; - public MappingFeatures Features { get; } = new MappingFeatures(); + public Features Features { get; } = new Features(); public ITypeMapConfiguration ReverseTypeMap => ReverseMapExpression; public IList ValueTransformers { get; } = new List(); diff --git a/src/AutoMapper/Configuration/PrimitiveExtensions.cs b/src/AutoMapper/Configuration/PrimitiveExtensions.cs index 73f9469596..33e514f83c 100644 --- a/src/AutoMapper/Configuration/PrimitiveExtensions.cs +++ b/src/AutoMapper/Configuration/PrimitiveExtensions.cs @@ -7,6 +7,14 @@ namespace AutoMapper.Configuration { internal static class PrimitiveExtensions { + public static void ForAll(this IEnumerable enumerable, Action action) + { + foreach (var feature in enumerable) + { + action(feature); + } + } + public static bool IsSetType(this Type type) => type.ImplementsGenericInterface(typeof(ISet<>)); diff --git a/src/AutoMapper/Features/FeatureExtensions.cs b/src/AutoMapper/Features/FeatureExtensions.cs new file mode 100644 index 0000000000..5af0dabf63 --- /dev/null +++ b/src/AutoMapper/Features/FeatureExtensions.cs @@ -0,0 +1,38 @@ +using AutoMapper.Configuration; + +namespace AutoMapper.Features +{ + public static class FeatureExtensions + { + public static IMapperConfigurationExpression SetFeature(this IMapperConfigurationExpression configuration, IGlobalFeature feature) + { + configuration.Features.Set(feature); + return configuration; + } + + public static IMappingExpression SetFeature(this IMappingExpression mapping, IMappingFeature feature) + { + mapping.Features.Set(feature); + return mapping; + } + + internal static void Configure(this Features features, MapperConfiguration mapperConfiguration) => features.ForAll(feature => feature.Configure(mapperConfiguration)); + + public static void ReverseTo(this Features features, Features reversedFeatures) => features.ForAll(feature => + { + var reverse = feature.Reverse(); + if (reverse != null) + { + reversedFeatures.Set(reverse); + } + }); + + internal static void Configure(this Features features, TypeMap typeMap) => features.ForAll(feature => feature.Configure(typeMap)); + + internal static void Seal(this Features features, IConfigurationProvider configurationProvider) + { + features.ForAll(feature => feature.Seal(configurationProvider)); + features.MakeReadOnly(); + } + } +} \ No newline at end of file diff --git a/src/AutoMapper/Features/Features.cs b/src/AutoMapper/Features/Features.cs index 5ecbebefd5..b422e10a78 100644 --- a/src/AutoMapper/Features/Features.cs +++ b/src/AutoMapper/Features/Features.cs @@ -27,14 +27,6 @@ public class Features : IEnumerable IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); - protected void MakeReadOnly() => _features = new ReadOnlyDictionary(_features); - - public void ForAll(Action action) - { - foreach (var feature in this) - { - action(feature); - } - } + internal void MakeReadOnly() => _features = new ReadOnlyDictionary(_features); } } \ No newline at end of file diff --git a/src/AutoMapper/Features/GlobalFeatures.cs b/src/AutoMapper/Features/GlobalFeatures.cs deleted file mode 100644 index 67fd763a9d..0000000000 --- a/src/AutoMapper/Features/GlobalFeatures.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace AutoMapper.Features -{ - public class GlobalFeatures : Features - { - internal void Configure(MapperConfiguration mapperConfiguration) => ForAll(feature => feature.Configure(mapperConfiguration)); - } -} \ No newline at end of file diff --git a/src/AutoMapper/Features/IRuntimeFeature.cs b/src/AutoMapper/Features/IRuntimeFeature.cs index df52a0f409..36dd09e994 100644 --- a/src/AutoMapper/Features/IRuntimeFeature.cs +++ b/src/AutoMapper/Features/IRuntimeFeature.cs @@ -4,19 +4,4 @@ public interface IRuntimeFeature { void Seal(IConfigurationProvider configurationProvider); } - - public static class FeatureExtensions - { - public static IMapperConfigurationExpression AddOrUpdateFeature(this IMapperConfigurationExpression configuration, IGlobalFeature feature) - { - configuration.Features.Set(feature); - return configuration; - } - - public static IMappingExpression AddOrUpdateFeature(this IMappingExpression mapping, IMappingFeature feature) - { - mapping.Features.Set(feature); - return mapping; - } - } -} +} \ No newline at end of file diff --git a/src/AutoMapper/Features/MappingFeatures.cs b/src/AutoMapper/Features/MappingFeatures.cs deleted file mode 100644 index ffcc128685..0000000000 --- a/src/AutoMapper/Features/MappingFeatures.cs +++ /dev/null @@ -1,16 +0,0 @@ -namespace AutoMapper.Features -{ - public class MappingFeatures : Features - { - public void ReverseTo(MappingFeatures features) => ForAll(feature => - { - var reverse = feature.Reverse(); - if (reverse != null) - { - features.Set(reverse); - } - }); - - internal void Configure(TypeMap typeMap) => ForAll(feature => feature.Configure(typeMap)); - } -} \ No newline at end of file diff --git a/src/AutoMapper/Features/RuntimeFeatures.cs b/src/AutoMapper/Features/RuntimeFeatures.cs deleted file mode 100644 index 7a16de8e37..0000000000 --- a/src/AutoMapper/Features/RuntimeFeatures.cs +++ /dev/null @@ -1,11 +0,0 @@ -namespace AutoMapper.Features -{ - public class RuntimeFeatures : Features - { - internal void Seal(IConfigurationProvider configurationProvider) - { - ForAll(feature => feature.Seal(configurationProvider)); - MakeReadOnly(); - } - } -} \ No newline at end of file diff --git a/src/AutoMapper/IConfigurationProvider.cs b/src/AutoMapper/IConfigurationProvider.cs index 5ecc1e1c39..50ce38afca 100644 --- a/src/AutoMapper/IConfigurationProvider.cs +++ b/src/AutoMapper/IConfigurationProvider.cs @@ -105,7 +105,7 @@ public interface IConfigurationProvider /// Gets the features collection. /// /// The feature colection. - RuntimeFeatures Features { get; } + Features Features { get; } /// /// Find a matching object mapper. diff --git a/src/AutoMapper/IMapperConfigurationExpression.cs b/src/AutoMapper/IMapperConfigurationExpression.cs index b510f9b8e7..f365a4f362 100644 --- a/src/AutoMapper/IMapperConfigurationExpression.cs +++ b/src/AutoMapper/IMapperConfigurationExpression.cs @@ -136,7 +136,7 @@ public interface IMapperConfigurationExpression : IProfileExpression /// /// Get the features collection. /// - GlobalFeatures Features { get; } + Features Features { get; } /// /// Object mappers diff --git a/src/AutoMapper/IMappingExpressionBase.cs b/src/AutoMapper/IMappingExpressionBase.cs index 3338b66509..33fac5459b 100644 --- a/src/AutoMapper/IMappingExpressionBase.cs +++ b/src/AutoMapper/IMappingExpressionBase.cs @@ -15,7 +15,7 @@ namespace AutoMapper public interface IMappingExpressionBase where TMappingExpression : IMappingExpressionBase { - MappingFeatures Features { get; } + Features Features { get; } /// /// Construct the destination object using the service locator diff --git a/src/AutoMapper/MapperConfiguration.cs b/src/AutoMapper/MapperConfiguration.cs index 95c0f117c0..c25f119132 100644 --- a/src/AutoMapper/MapperConfiguration.cs +++ b/src/AutoMapper/MapperConfiguration.cs @@ -88,7 +88,7 @@ public void Validate(ValidationContext context) public IEnumerable Binders { get; } - public RuntimeFeatures Features { get; } = new RuntimeFeatures(); + public Features Features { get; } = new Features(); public Func GetMapperFunc( TypePair types, IMemberMap memberMap = null) diff --git a/src/AutoMapper/TypeMap.cs b/src/AutoMapper/TypeMap.cs index 9f6c578a4b..902da1d87f 100644 --- a/src/AutoMapper/TypeMap.cs +++ b/src/AutoMapper/TypeMap.cs @@ -55,7 +55,7 @@ public PathMap FindOrCreatePathMapFor(LambdaExpression destinationExpression, Me private void AddPathMap(PathMap pathMap) => _pathMaps.Add(pathMap.MemberPath, pathMap); - public RuntimeFeatures Features { get; } = new RuntimeFeatures(); + public Features Features { get; } = new Features(); public PathMap FindPathMapByDestinationPath(string destinationFullPath) => PathMaps.SingleOrDefault(item => string.Join(".", item.MemberPath.Members.Select(m => m.Name)) == destinationFullPath); diff --git a/src/UnitTests/ConfigurationFeatureTest.cs b/src/UnitTests/ConfigurationFeatureTest.cs index 3786f75289..e729458f5d 100644 --- a/src/UnitTests/ConfigurationFeatureTest.cs +++ b/src/UnitTests/ConfigurationFeatureTest.cs @@ -14,12 +14,12 @@ public void Adding_same_feature_multiple_times_should_replace_eachother() var featureB = new ConfigurationExpressionFeatureB(1); var config = new MapperConfiguration(cfg => { - cfg.AddOrUpdateFeature(new ConfigurationExpressionFeatureA(3)); - cfg.AddOrUpdateFeature(new ConfigurationExpressionFeatureA(2)); - cfg.AddOrUpdateFeature(featureA); - cfg.AddOrUpdateFeature(new ConfigurationExpressionFeatureB(3)); - cfg.AddOrUpdateFeature(new ConfigurationExpressionFeatureB(2)); - cfg.AddOrUpdateFeature(featureB); + cfg.SetFeature(new ConfigurationExpressionFeatureA(3)); + cfg.SetFeature(new ConfigurationExpressionFeatureA(2)); + cfg.SetFeature(featureA); + cfg.SetFeature(new ConfigurationExpressionFeatureB(3)); + cfg.SetFeature(new ConfigurationExpressionFeatureB(2)); + cfg.SetFeature(featureB); }); Validate(featureA, config); @@ -32,7 +32,7 @@ public void Add_single_feature() var featureA = new ConfigurationExpressionFeatureA(1); var config = new MapperConfiguration(cfg => { - cfg.AddOrUpdateFeature(featureA); + cfg.SetFeature(featureA); }); Validate(featureA, config); @@ -45,8 +45,8 @@ public void Add_multiple_features() var featureB = new ConfigurationExpressionFeatureB(2); var config = new MapperConfiguration(cfg => { - cfg.AddOrUpdateFeature(featureA); - cfg.AddOrUpdateFeature(featureB); + cfg.SetFeature(featureA); + cfg.SetFeature(featureB); }); Validate(featureA, config); diff --git a/src/UnitTests/MappingExpressionFeatureWithReverseTest.cs b/src/UnitTests/MappingExpressionFeatureWithReverseTest.cs index 1ac2b23f87..0866cd9c78 100644 --- a/src/UnitTests/MappingExpressionFeatureWithReverseTest.cs +++ b/src/UnitTests/MappingExpressionFeatureWithReverseTest.cs @@ -17,12 +17,12 @@ public void Adding_same_feature_multiple_times_should_replace_eachother() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(new MappingExpressionFeatureA(3)) - .AddOrUpdateFeature(new MappingExpressionFeatureA(2)) - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(new MappingExpressionFeatureB(3)) - .AddOrUpdateFeature(new MappingExpressionFeatureB(2)) - .AddOrUpdateFeature(featureB) + .SetFeature(new MappingExpressionFeatureA(3)) + .SetFeature(new MappingExpressionFeatureA(2)) + .SetFeature(featureA) + .SetFeature(new MappingExpressionFeatureB(3)) + .SetFeature(new MappingExpressionFeatureB(2)) + .SetFeature(featureB) .ReverseMap(); }); @@ -50,7 +50,7 @@ public void Add_single_feature_with_reverse() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) + .SetFeature(featureA) .ReverseMap(); }); @@ -88,8 +88,8 @@ public void Add_multiple_features_with_reverse() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(featureB) + .SetFeature(featureA) + .SetFeature(featureB) .ReverseMap(); }); @@ -129,10 +129,10 @@ public void Add_multiple_features_with_reverse_overriden() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(featureB) + .SetFeature(featureA) + .SetFeature(featureB) .ReverseMap() - .AddOrUpdateFeature(overridenFeatureB); + .SetFeature(overridenFeatureB); }); var typeMap = config.FindTypeMapFor(); diff --git a/src/UnitTests/MappingExpressionFeatureWithoutReverseTest.cs b/src/UnitTests/MappingExpressionFeatureWithoutReverseTest.cs index c188f4a2ec..d9067288ac 100644 --- a/src/UnitTests/MappingExpressionFeatureWithoutReverseTest.cs +++ b/src/UnitTests/MappingExpressionFeatureWithoutReverseTest.cs @@ -16,12 +16,12 @@ public void Adding_same_feature_should_replace_eachother() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(new MappingExpressionFeatureA(3)) - .AddOrUpdateFeature(new MappingExpressionFeatureA(2)) - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(new MappingExpressionFeatureB(3)) - .AddOrUpdateFeature(new MappingExpressionFeatureB(2)) - .AddOrUpdateFeature(featureB); + .SetFeature(new MappingExpressionFeatureA(3)) + .SetFeature(new MappingExpressionFeatureA(2)) + .SetFeature(featureA) + .SetFeature(new MappingExpressionFeatureB(3)) + .SetFeature(new MappingExpressionFeatureB(2)) + .SetFeature(featureB); }); @@ -48,7 +48,7 @@ public void Add_single_feature() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA); + .SetFeature(featureA); }); var typeMap = config.FindTypeMapFor(); @@ -79,7 +79,7 @@ public void Add_single_feature_with_reverse() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) + .SetFeature(featureA) .ReverseMap(); }); @@ -112,8 +112,8 @@ public void Add_multiple_features() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(featureB); + .SetFeature(featureA) + .SetFeature(featureB); }); @@ -147,8 +147,8 @@ public void Add_multiple_features_with_reverse() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(featureB) + .SetFeature(featureA) + .SetFeature(featureB) .ReverseMap(); }); @@ -183,10 +183,10 @@ public void Add_multiple_features_with_reverse_overriden() var config = new MapperConfiguration(cfg => { cfg.CreateMap() - .AddOrUpdateFeature(featureA) - .AddOrUpdateFeature(featureB) + .SetFeature(featureA) + .SetFeature(featureB) .ReverseMap() - .AddOrUpdateFeature(overridenFeatureB); + .SetFeature(overridenFeatureB); }); var typeMap = config.FindTypeMapFor();