From 89c31eb6a7a29869787c7034b3ee9c4e638469f7 Mon Sep 17 00:00:00 2001 From: Jimmy Bogard Date: Wed, 24 Feb 2016 11:25:59 -0600 Subject: [PATCH 1/2] Removing obsolete on some of the static API --- src/AutoMapper/Mapper.cs | 47 +++++++++++++--------------------------- 1 file changed, 15 insertions(+), 32 deletions(-) diff --git a/src/AutoMapper/Mapper.cs b/src/AutoMapper/Mapper.cs index 127f903200..9252c2a1ac 100644 --- a/src/AutoMapper/Mapper.cs +++ b/src/AutoMapper/Mapper.cs @@ -37,7 +37,6 @@ public static bool AllowNullDestinationValues /// Destination type to create /// Source object to map from /// Mapped destination object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static TDestination Map(object source) { return Instance.Map(source); @@ -50,7 +49,6 @@ public static TDestination Map(object source) /// Source object to map from /// Mapping options /// Mapped destination object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static TDestination Map(object source, Action opts) { return Instance.Map(source, opts); @@ -63,7 +61,6 @@ public static TDestination Map(object source, ActionDestination type to create /// Source object to map from /// Mapped destination object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static TDestination Map(TSource source) { return Instance.Map(source); @@ -77,7 +74,6 @@ public static TDestination Map(object source, ActionSource object to map from /// Destination object to map into /// The mapped destination object, same instance as the object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static TDestination Map(TSource source, TDestination destination) { return Instance.Map(source, destination); @@ -92,7 +88,6 @@ public static TDestination Map(object source, ActionDestination object to map into /// Mapping options /// The mapped destination object, same instance as the object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static TDestination Map(TSource source, TDestination destination, Action> opts) { @@ -107,7 +102,6 @@ public static TDestination Map(object source, ActionSource object to map from /// Mapping options /// Mapped destination object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static TDestination Map(TSource source, Action> opts) { @@ -121,7 +115,6 @@ public static TDestination Map(object source, ActionSource type to use /// Destination type to create /// Mapped destination object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static object Map(object source, Type sourceType, Type destinationType) { return Instance.Map(source, sourceType, destinationType); @@ -135,7 +128,6 @@ public static object Map(object source, Type sourceType, Type destinationType) /// Destination type to create /// Mapping options /// Mapped destination object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static object Map(object source, Type sourceType, Type destinationType, Action opts) { @@ -150,7 +142,6 @@ public static object Map(object source, Type sourceType, Type destinationType) /// Source type to use /// Destination type to use /// Mapped destination object, same instance as the object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static object Map(object source, object destination, Type sourceType, Type destinationType) { return Instance.Map(source, destination, sourceType, destinationType); @@ -165,7 +156,6 @@ public static object Map(object source, object destination, Type sourceType, Typ /// Destination type to use /// Mapping options /// Mapped destination object, same instance as the object - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static object Map(object source, object destination, Type sourceType, Type destinationType, Action opts) { @@ -244,7 +234,6 @@ public static void DynamicMap(object source, object destination, Type sourceType /// This is the preferred means to configure AutoMapper. /// /// Initialization callback - [Obsolete("Construct a MapperConfiguration instance and call CreateMapper instead. Store the instances statically as needed.")] public static void Initialize(Action action) { Reset(); @@ -260,7 +249,7 @@ public static void Initialize(Action action) /// Source type /// Destination type /// Mapping expression for more configuration options - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static IMappingExpression CreateMap() { return Configuration.CreateMap(); @@ -274,7 +263,7 @@ public static void Initialize(Action action) /// Destination type /// Member list to validate /// Mapping expression for more configuration options - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static IMappingExpression CreateMap(MemberList memberList) { return Configuration.CreateMap(memberList); @@ -287,7 +276,7 @@ public static void Initialize(Action action) /// Source type /// Destination type /// Mapping expression for more configuration options - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static IMappingExpression CreateMap(Type sourceType, Type destinationType) { return Configuration.CreateMap(sourceType, destinationType); @@ -301,7 +290,7 @@ public static IMappingExpression CreateMap(Type sourceType, Type destinationType /// Destination type /// Member list to validate /// Mapping expression for more configuration options - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static IMappingExpression CreateMap(Type sourceType, Type destinationType, MemberList memberList) { return Configuration.CreateMap(sourceType, destinationType, memberList); @@ -312,7 +301,7 @@ public static IMappingExpression CreateMap(Type sourceType, Type destinationType /// /// Profile name /// Profile configuration options - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static IProfileExpression CreateProfile(string profileName) { return Configuration.CreateProfile(profileName); @@ -323,7 +312,7 @@ public static IProfileExpression CreateProfile(string profileName) /// /// Profile name /// Profile configuration callback - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static void CreateProfile(string profileName, Action profileConfiguration) { Configuration.CreateProfile(profileName, profileConfiguration); @@ -333,7 +322,7 @@ public static void CreateProfile(string profileName, Action /// Add an existing profile /// /// Profile to add - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static void AddProfile(Profile profile) { Configuration.AddProfile(profile); @@ -343,7 +332,7 @@ public static void AddProfile(Profile profile) /// Add an existing profile type. Profile will be instantiated and added to the configuration. /// /// Profile type - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static void AddProfile() where TProfile : Profile, new() { Configuration.AddProfile(); @@ -355,7 +344,7 @@ public static void AddProfile() where TProfile : Profile, new() /// Configured source type /// Configured destination type /// Type map configuration - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static TypeMap FindTypeMapFor(Type sourceType, Type destinationType) { return ConfigurationProvider.FindTypeMapFor(sourceType, destinationType); @@ -367,7 +356,7 @@ public static TypeMap FindTypeMapFor(Type sourceType, Type destinationType) /// Configured source type /// Configured destination type /// Type map configuration - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static TypeMap FindTypeMapFor() { return ConfigurationProvider.FindTypeMapFor(typeof (TSource), typeof (TDestination)); @@ -377,7 +366,7 @@ public static TypeMap FindTypeMapFor(Type sourceType, Type destinationType) /// Get all configured type maps created /// /// All configured type maps - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static TypeMap[] GetAllTypeMaps() { return ConfigurationProvider.GetAllTypeMaps(); @@ -386,7 +375,6 @@ public static TypeMap[] GetAllTypeMaps() /// /// Dry run all configured type maps and throw for each problem /// - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static void AssertConfigurationIsValid() { ConfigurationProvider.AssertConfigurationIsValid(); @@ -396,7 +384,6 @@ public static void AssertConfigurationIsValid() /// Dry run single type map /// /// Type map to check - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static void AssertConfigurationIsValid(TypeMap typeMap) { ConfigurationProvider.AssertConfigurationIsValid(typeMap); @@ -406,7 +393,6 @@ public static void AssertConfigurationIsValid(TypeMap typeMap) /// Dry run all type maps in given profile /// /// Profile name of type maps to test - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static void AssertConfigurationIsValid(string profileName) { ConfigurationProvider.AssertConfigurationIsValid(profileName); @@ -416,7 +402,6 @@ public static void AssertConfigurationIsValid(string profileName) /// Dry run all type maps in given profile /// /// Profile type - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static void AssertConfigurationIsValid() where TProfile : Profile, new() { ConfigurationProvider.AssertConfigurationIsValid(); @@ -425,7 +410,7 @@ public static void AssertConfigurationIsValid() where TProfile : Profi /// /// Clear out all existing configuration /// - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static void Reset() { MapperRegistry.Reset(); @@ -436,16 +421,14 @@ public static void Reset() /// /// Mapping engine used to perform mappings /// - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static IMappingEngine Engine => _mappingEngine.Value._engine; - private static IMapper Instance => _mappingEngine.Value; - internal static IConfigurationProvider ConfigurationProvider => _configuration.Value; + public static IMapper Instance => _mappingEngine.Value; + public static IConfigurationProvider ConfigurationProvider => _configuration.Value; private static IDynamicMapper DynamicInstance => _mappingEngine.Value; /// /// Store for all configuration /// - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] public static IMapperConfiguration Configuration => _configuration.Value; @@ -453,7 +436,7 @@ public static void Reset() /// Globally ignore all members starting with a prefix /// /// Prefix of members to ignore. Call this before all other maps created. - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed. Use CreateMapper to create a mapper instance.")] + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static void AddGlobalIgnore(string startingwith) { Configuration.AddGlobalIgnore(startingwith); From 8638e9c46f463506a620fc6311fa4f9344fc37f0 Mon Sep 17 00:00:00 2001 From: Jimmy Bogard Date: Wed, 24 Feb 2016 13:13:55 -0600 Subject: [PATCH 2/2] Restoring queryable extensions --- src/AutoMapper/Mapper.cs | 3 +- .../QueryableExtensions/Extensions.cs | 75 +++++++++++++------ src/UnitTests/StaticMapping.cs | 52 +++++++++++++ src/UnitTests/UnitTests.Net4.csproj | 1 + 4 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 src/UnitTests/StaticMapping.cs diff --git a/src/AutoMapper/Mapper.cs b/src/AutoMapper/Mapper.cs index 9252c2a1ac..8a523283c1 100644 --- a/src/AutoMapper/Mapper.cs +++ b/src/AutoMapper/Mapper.cs @@ -423,12 +423,13 @@ public static void Reset() /// public static IMappingEngine Engine => _mappingEngine.Value._engine; public static IMapper Instance => _mappingEngine.Value; - public static IConfigurationProvider ConfigurationProvider => _configuration.Value; + internal static IConfigurationProvider ConfigurationProvider => _configuration.Value; private static IDynamicMapper DynamicInstance => _mappingEngine.Value; /// /// Store for all configuration /// + [Obsolete("Dynamically creating maps will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, or Mapper.Initialize. Use CreateMapper to create a mapper instance.")] public static IMapperConfiguration Configuration => _configuration.Value; diff --git a/src/AutoMapper/QueryableExtensions/Extensions.cs b/src/AutoMapper/QueryableExtensions/Extensions.cs index 4690763f1b..7e3119a9ca 100644 --- a/src/AutoMapper/QueryableExtensions/Extensions.cs +++ b/src/AutoMapper/QueryableExtensions/Extensions.cs @@ -12,8 +12,16 @@ namespace AutoMapper.QueryableExtensions /// public static class Extensions { - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, passing in to the overload that accepts an instance")] - public static Func ConfigurationFactory = () => Mapper.ConfigurationProvider; + /// + /// Maps a queryable expression of a source type to a queryable expression of a destination type + /// + /// Source type + /// Destination type + /// Source queryable + /// Destination queryable + /// Mapped destination queryable + public static IQueryable Map(this IQueryable sourceQuery, IQueryable destQuery) + => sourceQuery.Map(destQuery, Mapper.ConfigurationProvider); /// /// Maps a queryable expression of a source type to a queryable expression of a destination type @@ -26,9 +34,12 @@ public static class Extensions /// Mapped destination queryable public static IQueryable Map(this IQueryable sourceQuery, IQueryable destQuery, IConfigurationProvider config) { - return QueryMapperVisitor.Map(sourceQuery, destQuery, config); + return QueryMapperVisitor.Map(sourceQuery, destQuery, config); } + public static IQueryDataSourceInjection UseAsDataSource(this IQueryable dataSource) + => dataSource.UseAsDataSource(Mapper.Instance); + public static IQueryDataSourceInjection UseAsDataSource(this IQueryable dataSource, IMapper mapper) { return new QueryDataSourceInjection(dataSource, mapper); @@ -40,7 +51,19 @@ public static IQueryDataSourceInjection UseAsDataSource(this I /// Projections are only calculated once and cached /// Destination type /// Queryable source - /// Maper configuration + /// Optional parameter object for parameterized mapping expressions + /// Explicit members to expand + /// Expression to project into + public static IQueryable ProjectTo(this IQueryable source, object parameters, params Expression>[] membersToExpand) + => source.ProjectTo(Mapper.ConfigurationProvider, parameters, membersToExpand); + + /// + /// Extension method to project from a queryable using the provided mapping engine + /// + /// Projections are only calculated once and cached + /// Destination type + /// Queryable source + /// Mapper configuration /// Optional parameter object for parameterized mapping expressions /// Explicit members to expand /// Expression to project into @@ -49,12 +72,6 @@ public static IQueryable ProjectTo(this IQueryable s return new ProjectionExpression(source, configuration.ExpressionBuilder).To(parameters, membersToExpand); } - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, passing in to the overload that accepts an instance")] - public static IQueryable ProjectTo(this IQueryable source, object parameters, params Expression>[] membersToExpand) - { - return new ProjectionExpression(source, ConfigurationFactory().ExpressionBuilder).To(parameters, membersToExpand); - } - /// /// Extension method to project from a queryable using the provided mapping engine /// @@ -69,18 +86,34 @@ public static IQueryable ProjectTo(this IQueryable s IConfigurationProvider configuration, params Expression>[] membersToExpand ) - { - return source.ProjectTo(configuration, null, membersToExpand); - } + => source.ProjectTo(configuration, null, membersToExpand); - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, passing in to the overload that accepts an instance")] + /// + /// Extension method to project from a queryable using the provided mapping engine + /// + /// Projections are only calculated once and cached + /// Destination type + /// Queryable source + /// Explicit members to expand + /// Expression to project into public static IQueryable ProjectTo( this IQueryable source, params Expression>[] membersToExpand ) - { - return source.ProjectTo(ConfigurationFactory(), null, membersToExpand); - } + => source.ProjectTo(Mapper.ConfigurationProvider, null, membersToExpand); + + /// + /// Projects the source type to the destination type given the mapping configuration + /// + /// Destination type to map to + /// Queryable source + /// Mapper configuration + /// Optional parameter object for parameterized mapping expressions + /// Explicit members to expand + /// Queryable result, use queryable extension methods to project and execute result + public static IQueryable ProjectTo(this IQueryable source, + IDictionary parameters, params string[] membersToExpand) + => source.ProjectTo(Mapper.ConfigurationProvider, parameters, membersToExpand); /// /// Projects the source type to the destination type given the mapping configuration @@ -95,11 +128,5 @@ public static IQueryable ProjectTo(this IQueryable s { return new ProjectionExpression(source, configuration.ExpressionBuilder).To(parameters, membersToExpand); } - - [Obsolete("The static API will be removed in version 5.0. Use a MapperConfiguration instance and store statically as needed, passing in to the overload that accepts an instance")] - public static IQueryable ProjectTo(this IQueryable source, IDictionary parameters, params string[] membersToExpand) - { - return new ProjectionExpression(source, ConfigurationFactory().ExpressionBuilder).To(parameters, membersToExpand); - } } -} +} \ No newline at end of file diff --git a/src/UnitTests/StaticMapping.cs b/src/UnitTests/StaticMapping.cs new file mode 100644 index 0000000000..165060c0d0 --- /dev/null +++ b/src/UnitTests/StaticMapping.cs @@ -0,0 +1,52 @@ +namespace AutoMapper.UnitTests +{ + using System.Linq; + using Should; + using Xunit; + using QueryableExtensions; + + public class StaticMapping + { + public class Source + { + public int Value { get; set; } + } + + public class Dest + { + public int Value { get; set; } + } + + [Fact] + public void Can_map_statically() + { + Mapper.Initialize(cfg => + { + cfg.CreateMap(); + }); + + var source = new Source { Value = 5 }; + + var dest = Mapper.Map(source); + + dest.Value.ShouldEqual(source.Value); + } + + [Fact] + public void Can_project_statically() + { + Mapper.Initialize(cfg => + { + cfg.CreateMap(); + }); + + var source = new Source { Value = 5 }; + var sources = new[] { source }.AsQueryable(); + + var dests = sources.ProjectTo().ToArray(); + + dests.Length.ShouldEqual(1); + dests[0].Value.ShouldEqual(source.Value); + } + } +} \ No newline at end of file diff --git a/src/UnitTests/UnitTests.Net4.csproj b/src/UnitTests/UnitTests.Net4.csproj index e8ee53aa24..61de5a965f 100644 --- a/src/UnitTests/UnitTests.Net4.csproj +++ b/src/UnitTests/UnitTests.Net4.csproj @@ -298,6 +298,7 @@ +