From e53f846665d9559adc0d168f44574b03f62b95c8 Mon Sep 17 00:00:00 2001 From: "Serhii A. Hrytsenko" Date: Sun, 17 Jul 2022 20:08:41 -0400 Subject: [PATCH 01/14] Refactor IMapper.From to return interface --- src/Mapster/Interfaces/IMapper.cs | 2 +- src/Mapster/Interfaces/ITypeAdapterBuilder.cs | 25 +++++++++++++++++++ src/Mapster/Mapper.cs | 6 ++--- src/Mapster/TypeAdapter.cs | 4 +-- src/Mapster/TypeAdapterBuilder.cs | 14 +++++------ 5 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 src/Mapster/Interfaces/ITypeAdapterBuilder.cs diff --git a/src/Mapster/Interfaces/IMapper.cs b/src/Mapster/Interfaces/IMapper.cs index 4ef3d2b0..f32b4b51 100644 --- a/src/Mapster/Interfaces/IMapper.cs +++ b/src/Mapster/Interfaces/IMapper.cs @@ -7,7 +7,7 @@ namespace MapsterMapper public interface IMapper { TypeAdapterConfig Config { get; } - TypeAdapterBuilder From(TSource source); + ITypeAdapterBuilder From(TSource source); TDestination Map(object source); TDestination Map(TSource source); TDestination Map(TSource source, TDestination destination); diff --git a/src/Mapster/Interfaces/ITypeAdapterBuilder.cs b/src/Mapster/Interfaces/ITypeAdapterBuilder.cs new file mode 100644 index 00000000..8dd9ef21 --- /dev/null +++ b/src/Mapster/Interfaces/ITypeAdapterBuilder.cs @@ -0,0 +1,25 @@ +using System; +using System.Diagnostics.CodeAnalysis; +using System.Linq.Expressions; +using System.Runtime.CompilerServices; + +namespace Mapster +{ + public interface ITypeAdapterBuilder : IAdapterBuilder + { + [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")] + ITypeAdapterBuilder ForkConfig(Action action, +#if !NET40 + [CallerFilePath] +#endif + string key1 = "", +#if !NET40 + [CallerLineNumber] +#endif + int key2 = 0); + ITypeAdapterBuilder AddParameters(string name, object value); + Expression> CreateMapExpression(); + Expression> CreateMapToTargetExpression(); + Expression> CreateProjectionExpression(); + } +} diff --git a/src/Mapster/Mapper.cs b/src/Mapster/Mapper.cs index ccb075b1..d63f6213 100644 --- a/src/Mapster/Mapper.cs +++ b/src/Mapster/Mapper.cs @@ -16,9 +16,9 @@ public Mapper(TypeAdapterConfig config) Config = config; } - public virtual TypeAdapterBuilder From(TSource source) + public virtual ITypeAdapterBuilder From(TSource source) { - return new TypeAdapterBuilder(source, Config); + return TypeAdapter.BuildAdapter(source, Config); } public virtual TDestination Map(object source) @@ -76,4 +76,4 @@ public virtual object Map(object source, object destination, Type sourceType, Ty } } -} \ No newline at end of file +} diff --git a/src/Mapster/TypeAdapter.cs b/src/Mapster/TypeAdapter.cs index 8e1af524..85bc2f62 100644 --- a/src/Mapster/TypeAdapter.cs +++ b/src/Mapster/TypeAdapter.cs @@ -5,12 +5,12 @@ namespace Mapster { public static class TypeAdapter { - public static TypeAdapterBuilder BuildAdapter(this TSource source) + public static ITypeAdapterBuilder BuildAdapter(this TSource source) { return new TypeAdapterBuilder(source, TypeAdapterConfig.GlobalSettings); } - public static TypeAdapterBuilder BuildAdapter(this TSource source, TypeAdapterConfig config) + public static ITypeAdapterBuilder BuildAdapter(this TSource source, TypeAdapterConfig config) { return new TypeAdapterBuilder(source, config); } diff --git a/src/Mapster/TypeAdapterBuilder.cs b/src/Mapster/TypeAdapterBuilder.cs index 5a7bebac..5b0c314c 100644 --- a/src/Mapster/TypeAdapterBuilder.cs +++ b/src/Mapster/TypeAdapterBuilder.cs @@ -7,7 +7,7 @@ namespace Mapster { - public class TypeAdapterBuilder : IAdapterBuilder + public class TypeAdapterBuilder : ITypeAdapterBuilder { TSource Source { get; } TSource IAdapterBuilder.Source => this.Source; @@ -26,7 +26,7 @@ internal TypeAdapterBuilder(TSource source, TypeAdapterConfig config) } [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")] - public TypeAdapterBuilder ForkConfig(Action action, + public ITypeAdapterBuilder ForkConfig(Action action, #if !NET40 [CallerFilePath] #endif @@ -40,7 +40,7 @@ public TypeAdapterBuilder ForkConfig(Action action, return this; } - public TypeAdapterBuilder AddParameters(string name, object value) + public ITypeAdapterBuilder AddParameters(string name, object value) { this.Parameters.Add(name, value); return this; @@ -97,19 +97,19 @@ private TDestination MapToTarget(TDestination destination) public Expression> CreateMapExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); - return (Expression>) this.Config.CreateMapExpression(tuple, MapType.Map); + return (Expression>)this.Config.CreateMapExpression(tuple, MapType.Map); } public Expression> CreateMapToTargetExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); - return (Expression>) this.Config.CreateMapExpression(tuple, MapType.MapToTarget); + return (Expression>)this.Config.CreateMapExpression(tuple, MapType.MapToTarget); } public Expression> CreateProjectionExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); - return (Expression>) this.Config.CreateMapExpression(tuple, MapType.Projection); + return (Expression>)this.Config.CreateMapExpression(tuple, MapType.Projection); } } -} \ No newline at end of file +} From 0489cf736441f65c3a2d9473a15eae2c75e436d0 Mon Sep 17 00:00:00 2001 From: "Serhii A. Hrytsenko" Date: Sun, 17 Jul 2022 20:18:18 -0400 Subject: [PATCH 02/14] Remove unnecessary edits --- src/Mapster/TypeAdapterBuilder.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Mapster/TypeAdapterBuilder.cs b/src/Mapster/TypeAdapterBuilder.cs index 5b0c314c..7de08459 100644 --- a/src/Mapster/TypeAdapterBuilder.cs +++ b/src/Mapster/TypeAdapterBuilder.cs @@ -97,19 +97,19 @@ private TDestination MapToTarget(TDestination destination) public Expression> CreateMapExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); - return (Expression>)this.Config.CreateMapExpression(tuple, MapType.Map); + return (Expression>) this.Config.CreateMapExpression(tuple, MapType.Map); } public Expression> CreateMapToTargetExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); - return (Expression>)this.Config.CreateMapExpression(tuple, MapType.MapToTarget); + return (Expression>) this.Config.CreateMapExpression(tuple, MapType.MapToTarget); } public Expression> CreateProjectionExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); - return (Expression>)this.Config.CreateMapExpression(tuple, MapType.Projection); + return (Expression>) this.Config.CreateMapExpression(tuple, MapType.Projection); } } } From 4486ab103d10ce0b778c1e8d395c657c69afd13e Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Tue, 6 Sep 2022 13:33:32 +0200 Subject: [PATCH 03/14] Fixed build errors --- src/Mapster.DependencyInjection/ServiceMapper.cs | 2 +- src/Mapster.EF6/TypeAdapterBuilderExtensions.cs | 2 +- src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Mapster.DependencyInjection/ServiceMapper.cs b/src/Mapster.DependencyInjection/ServiceMapper.cs index bfaddb3d..03c8fc3d 100644 --- a/src/Mapster.DependencyInjection/ServiceMapper.cs +++ b/src/Mapster.DependencyInjection/ServiceMapper.cs @@ -13,7 +13,7 @@ public ServiceMapper(IServiceProvider serviceProvider, TypeAdapterConfig config) _serviceProvider = serviceProvider; } - public override TypeAdapterBuilder From(TSource source) + public override ITypeAdapterBuilder From(TSource source) { return base.From(source) .AddParameters(DI_KEY, _serviceProvider); diff --git a/src/Mapster.EF6/TypeAdapterBuilderExtensions.cs b/src/Mapster.EF6/TypeAdapterBuilderExtensions.cs index 748b3a21..847d2068 100644 --- a/src/Mapster.EF6/TypeAdapterBuilderExtensions.cs +++ b/src/Mapster.EF6/TypeAdapterBuilderExtensions.cs @@ -12,7 +12,7 @@ namespace Mapster { public static class TypeAdapterBuilderExtensions { - public static TypeAdapterBuilder EntityFromContext(this TypeAdapterBuilder builder, IObjectContextAdapter context) + public static ITypeAdapterBuilder EntityFromContext(this ITypeAdapterBuilder builder, IObjectContextAdapter context) { const string DB_KEY = "Mapster.EF6.db"; return builder diff --git a/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs b/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs index a6253980..11fa744d 100644 --- a/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs +++ b/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs @@ -12,7 +12,7 @@ namespace Mapster { public static class TypeAdapterBuilderExtensions { - public static TypeAdapterBuilder EntityFromContext(this TypeAdapterBuilder builder, DbContext context) + public static ITypeAdapterBuilder EntityFromContext(this ITypeAdapterBuilder builder, DbContext context) { const string dbKey = "Mapster.EFCore.db"; return builder From e407f6ea553a52fa794187dfa869748e1f1f25e6 Mon Sep 17 00:00:00 2001 From: Christopher Edwards Date: Mon, 14 Nov 2022 17:04:56 +0000 Subject: [PATCH 04/14] Add .net 7.0 TFM to all projects (except benchmark) whilst maintaining .net 6 TFM. Also fix AddProblemDetails in sample project to use thrid party extension in .net 6 and built in service in .net 7 (doing this avoids naming collision). --- .../ExpressionDebugger.Console.csproj | 2 +- src/ExpressionDebugger.Tests/ExpressionDebugger.Tests.csproj | 2 +- src/ExpressionDebugger/ExpressionDebugger.csproj | 2 +- src/ExpressionTranslator/ExpressionTranslator.csproj | 2 +- src/Mapster.Async.Tests/Mapster.Async.Tests.csproj | 2 +- src/Mapster.Async/Mapster.Async.csproj | 2 +- src/Mapster.Core/Mapster.Core.csproj | 2 +- .../Mapster.DependencyInjection.Tests.csproj | 2 +- .../Mapster.DependencyInjection.csproj | 2 +- src/Mapster.EF6/Mapster.EF6.csproj | 2 +- src/Mapster.EFCore.Tests/Mapster.EFCore.Tests.csproj | 2 +- src/Mapster.EFCore/Mapster.EFCore.csproj | 2 +- src/Mapster.Immutable.Tests/Mapster.Immutable.Tests.csproj | 2 +- src/Mapster.Immutable/Mapster.Immutable.csproj | 2 +- src/Mapster.JsonNet.Tests/Mapster.JsonNet.Tests.csproj | 2 +- src/Mapster.JsonNet/Mapster.JsonNet.csproj | 2 +- src/Mapster.SourceGenerator/Mapster.SourceGenerator.csproj | 2 +- src/Mapster.Tests/Mapster.Tests.csproj | 2 +- src/Mapster.Tool/Mapster.Tool.csproj | 2 +- src/Mapster/Mapster.csproj | 2 +- src/Sample.AspNetCore/Sample.AspNetCore.csproj | 2 +- src/Sample.AspNetCore/Startup.cs | 4 ++++ src/Sample.CodeGen/Sample.CodeGen.csproj | 2 +- src/Sample.CodeGen/Startup.cs | 5 +++++ src/TemplateTest/TemplateTest.csproj | 2 +- 25 files changed, 32 insertions(+), 23 deletions(-) diff --git a/src/ExpressionDebugger.Console/ExpressionDebugger.Console.csproj b/src/ExpressionDebugger.Console/ExpressionDebugger.Console.csproj index e7fd22a3..b78b4910 100644 --- a/src/ExpressionDebugger.Console/ExpressionDebugger.Console.csproj +++ b/src/ExpressionDebugger.Console/ExpressionDebugger.Console.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net6.0;net7.0 true diff --git a/src/ExpressionDebugger.Tests/ExpressionDebugger.Tests.csproj b/src/ExpressionDebugger.Tests/ExpressionDebugger.Tests.csproj index 657fc325..5140c085 100644 --- a/src/ExpressionDebugger.Tests/ExpressionDebugger.Tests.csproj +++ b/src/ExpressionDebugger.Tests/ExpressionDebugger.Tests.csproj @@ -1,7 +1,7 @@  - net6.0 + net6.0;net7.0 false diff --git a/src/ExpressionDebugger/ExpressionDebugger.csproj b/src/ExpressionDebugger/ExpressionDebugger.csproj index df9357e4..6b9241c3 100644 --- a/src/ExpressionDebugger/ExpressionDebugger.csproj +++ b/src/ExpressionDebugger/ExpressionDebugger.csproj @@ -1,7 +1,7 @@  - net6.0 + net6.0;net7.0 True Chaowlert Chaisrichalermpol Step into debugging from linq expressions diff --git a/src/ExpressionTranslator/ExpressionTranslator.csproj b/src/ExpressionTranslator/ExpressionTranslator.csproj index 44eb22ac..70f574b7 100644 --- a/src/ExpressionTranslator/ExpressionTranslator.csproj +++ b/src/ExpressionTranslator/ExpressionTranslator.csproj @@ -1,7 +1,7 @@  - net6.0 + net6.0;net7.0 True Chaowlert Chaisrichalermpol Translate from linq expressions to C# code diff --git a/src/Mapster.Async.Tests/Mapster.Async.Tests.csproj b/src/Mapster.Async.Tests/Mapster.Async.Tests.csproj index a73717fc..fea91e85 100644 --- a/src/Mapster.Async.Tests/Mapster.Async.Tests.csproj +++ b/src/Mapster.Async.Tests/Mapster.Async.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 false diff --git a/src/Mapster.Async/Mapster.Async.csproj b/src/Mapster.Async/Mapster.Async.csproj index e3615037..5bfc252d 100644 --- a/src/Mapster.Async/Mapster.Async.csproj +++ b/src/Mapster.Async/Mapster.Async.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 Async supports for Mapster true Mapster;Async diff --git a/src/Mapster.Core/Mapster.Core.csproj b/src/Mapster.Core/Mapster.Core.csproj index 702e8d02..736edb34 100644 --- a/src/Mapster.Core/Mapster.Core.csproj +++ b/src/Mapster.Core/Mapster.Core.csproj @@ -1,7 +1,7 @@  Lightweight library for Mapster and Mapster CodeGen - net6.0 + net7.0;net6.0 Mapster.Core mapster 1.2.1-pre02 diff --git a/src/Mapster.DependencyInjection.Tests/Mapster.DependencyInjection.Tests.csproj b/src/Mapster.DependencyInjection.Tests/Mapster.DependencyInjection.Tests.csproj index 5c136560..e0b62dae 100644 --- a/src/Mapster.DependencyInjection.Tests/Mapster.DependencyInjection.Tests.csproj +++ b/src/Mapster.DependencyInjection.Tests/Mapster.DependencyInjection.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 false diff --git a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj index 8a4a1d3b..5b614ceb 100644 --- a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj +++ b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 Dependency Injection supports for Mapster true Mapster;DependencyInjection diff --git a/src/Mapster.EF6/Mapster.EF6.csproj b/src/Mapster.EF6/Mapster.EF6.csproj index b8c57b12..ff64a239 100644 --- a/src/Mapster.EF6/Mapster.EF6.csproj +++ b/src/Mapster.EF6/Mapster.EF6.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 EF6 plugin for Mapster true Mapster;EF6 diff --git a/src/Mapster.EFCore.Tests/Mapster.EFCore.Tests.csproj b/src/Mapster.EFCore.Tests/Mapster.EFCore.Tests.csproj index 411eaa44..ca407d5b 100644 --- a/src/Mapster.EFCore.Tests/Mapster.EFCore.Tests.csproj +++ b/src/Mapster.EFCore.Tests/Mapster.EFCore.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 false diff --git a/src/Mapster.EFCore/Mapster.EFCore.csproj b/src/Mapster.EFCore/Mapster.EFCore.csproj index 77d618cd..120f7b3c 100644 --- a/src/Mapster.EFCore/Mapster.EFCore.csproj +++ b/src/Mapster.EFCore/Mapster.EFCore.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 EFCore plugin for Mapster true Mapster;EFCore diff --git a/src/Mapster.Immutable.Tests/Mapster.Immutable.Tests.csproj b/src/Mapster.Immutable.Tests/Mapster.Immutable.Tests.csproj index ffe876ac..4b52ed69 100644 --- a/src/Mapster.Immutable.Tests/Mapster.Immutable.Tests.csproj +++ b/src/Mapster.Immutable.Tests/Mapster.Immutable.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 false diff --git a/src/Mapster.Immutable/Mapster.Immutable.csproj b/src/Mapster.Immutable/Mapster.Immutable.csproj index a89af4d9..7a273b42 100644 --- a/src/Mapster.Immutable/Mapster.Immutable.csproj +++ b/src/Mapster.Immutable/Mapster.Immutable.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 Immutable collection supports for Mapster true Mapster;Immutable diff --git a/src/Mapster.JsonNet.Tests/Mapster.JsonNet.Tests.csproj b/src/Mapster.JsonNet.Tests/Mapster.JsonNet.Tests.csproj index 9411f47b..b7173dfe 100644 --- a/src/Mapster.JsonNet.Tests/Mapster.JsonNet.Tests.csproj +++ b/src/Mapster.JsonNet.Tests/Mapster.JsonNet.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 false diff --git a/src/Mapster.JsonNet/Mapster.JsonNet.csproj b/src/Mapster.JsonNet/Mapster.JsonNet.csproj index 49266168..69387e62 100644 --- a/src/Mapster.JsonNet/Mapster.JsonNet.csproj +++ b/src/Mapster.JsonNet/Mapster.JsonNet.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 Json.net conversion supports for Mapster true Mapster;Json.net diff --git a/src/Mapster.SourceGenerator/Mapster.SourceGenerator.csproj b/src/Mapster.SourceGenerator/Mapster.SourceGenerator.csproj index 883eb758..f436bada 100644 --- a/src/Mapster.SourceGenerator/Mapster.SourceGenerator.csproj +++ b/src/Mapster.SourceGenerator/Mapster.SourceGenerator.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 Source generator to generate mapping using Mapster source-generator;mapster true diff --git a/src/Mapster.Tests/Mapster.Tests.csproj b/src/Mapster.Tests/Mapster.Tests.csproj index 63eb3b6e..bc5331c3 100644 --- a/src/Mapster.Tests/Mapster.Tests.csproj +++ b/src/Mapster.Tests/Mapster.Tests.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 false Mapster.Tests Mapster.Tests.snk diff --git a/src/Mapster.Tool/Mapster.Tool.csproj b/src/Mapster.Tool/Mapster.Tool.csproj index 8e4c1b2f..f62994ab 100644 --- a/src/Mapster.Tool/Mapster.Tool.csproj +++ b/src/Mapster.Tool/Mapster.Tool.csproj @@ -2,7 +2,7 @@ Exe - net6.0 + net7.0;net6.0 true true dotnet-mapster diff --git a/src/Mapster/Mapster.csproj b/src/Mapster/Mapster.csproj index d7588978..267d910e 100644 --- a/src/Mapster/Mapster.csproj +++ b/src/Mapster/Mapster.csproj @@ -4,7 +4,7 @@ A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster. Copyright (c) 2016 Chaowlert Chaisrichalermpol, Eric Swann chaowlert;eric_swann - net6.0 + net7.0;net6.0 Mapster A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster. Mapster diff --git a/src/Sample.AspNetCore/Sample.AspNetCore.csproj b/src/Sample.AspNetCore/Sample.AspNetCore.csproj index 9866b4a4..c5761156 100644 --- a/src/Sample.AspNetCore/Sample.AspNetCore.csproj +++ b/src/Sample.AspNetCore/Sample.AspNetCore.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 diff --git a/src/Sample.AspNetCore/Startup.cs b/src/Sample.AspNetCore/Startup.cs index 8b1ee9b8..f5cdc6a1 100644 --- a/src/Sample.AspNetCore/Startup.cs +++ b/src/Sample.AspNetCore/Startup.cs @@ -1,6 +1,8 @@ using System.Linq.Expressions; using ExpressionDebugger; +#if NET6_0 using Hellang.Middleware.ProblemDetails; +#endif using Mapster; using Sample.AspNetCore.Controllers; using Sample.AspNetCore.Models; @@ -69,7 +71,9 @@ private static TypeAdapterConfig GetConfiguredMappingConfig() // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + #if NET6_0 app.UseProblemDetails(); + #endif app.UseRouting(); app.UseAuthorization(); app.UseMvc(); diff --git a/src/Sample.CodeGen/Sample.CodeGen.csproj b/src/Sample.CodeGen/Sample.CodeGen.csproj index 03a79b04..27485d31 100644 --- a/src/Sample.CodeGen/Sample.CodeGen.csproj +++ b/src/Sample.CodeGen/Sample.CodeGen.csproj @@ -1,7 +1,7 @@  - net6.0 + net7.0;net6.0 enable diff --git a/src/Sample.CodeGen/Startup.cs b/src/Sample.CodeGen/Startup.cs index 8b0cad18..5b16a61c 100644 --- a/src/Sample.CodeGen/Startup.cs +++ b/src/Sample.CodeGen/Startup.cs @@ -1,4 +1,6 @@ +#if NET6_0 using Hellang.Middleware.ProblemDetails; +#endif using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.OData; @@ -27,6 +29,7 @@ public void ConfigureServices(IServiceCollection services) .AddNewtonsoftJson(); services.AddDbContext(options => options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); + services.AddProblemDetails(); services.Scan(selector => selector.FromCallingAssembly() .AddClasses().AsMatchingInterface().WithSingletonLifetime()); @@ -35,7 +38,9 @@ public void ConfigureServices(IServiceCollection services) // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { + #if NET6_0 app.UseProblemDetails(); + #endif app.UseRouting(); app.UseAuthorization(); app.UseMvc(); diff --git a/src/TemplateTest/TemplateTest.csproj b/src/TemplateTest/TemplateTest.csproj index 34af174b..2f6524f3 100644 --- a/src/TemplateTest/TemplateTest.csproj +++ b/src/TemplateTest/TemplateTest.csproj @@ -1,7 +1,7 @@ - net6.0 + net7.0;net6.0 enable false From 56611d5ceed0efebfde35bc740b1ab16ba82e1ac Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Tue, 3 Jan 2023 22:29:24 +0100 Subject: [PATCH 05/14] Bump package version numbers and update Pack.bat --- src/Mapster.Async/Mapster.Async.csproj | 2 +- src/Mapster.Core/Mapster.Core.csproj | 2 +- .../Mapster.DependencyInjection.csproj | 2 +- src/Mapster.EF6/Mapster.EF6.csproj | 2 +- src/Mapster.EFCore/Mapster.EFCore.csproj | 2 +- src/Mapster.Immutable/Mapster.Immutable.csproj | 2 +- src/Mapster.JsonNet/Mapster.JsonNet.csproj | 2 +- src/Mapster.Tool/Mapster.Tool.csproj | 2 +- src/Mapster/Mapster.csproj | 2 +- src/Pack.bat | 4 ++-- 10 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Mapster.Async/Mapster.Async.csproj b/src/Mapster.Async/Mapster.Async.csproj index 5bfc252d..e1b5de64 100644 --- a/src/Mapster.Async/Mapster.Async.csproj +++ b/src/Mapster.Async/Mapster.Async.csproj @@ -7,7 +7,7 @@ Mapster;Async true Mapster.Async.snk - 2.0.0 + 2.0.1-pre01 diff --git a/src/Mapster.Core/Mapster.Core.csproj b/src/Mapster.Core/Mapster.Core.csproj index 736edb34..c0718384 100644 --- a/src/Mapster.Core/Mapster.Core.csproj +++ b/src/Mapster.Core/Mapster.Core.csproj @@ -4,7 +4,7 @@ net7.0;net6.0 Mapster.Core mapster - 1.2.1-pre02 + 1.2.1-pre03 enable true true diff --git a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj index 5b614ceb..bd58ffbb 100644 --- a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj +++ b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj @@ -7,7 +7,7 @@ Mapster;DependencyInjection true Mapster.DependencyInjection.snk - 1.0.0 + 1.0.1-pre01 diff --git a/src/Mapster.EF6/Mapster.EF6.csproj b/src/Mapster.EF6/Mapster.EF6.csproj index ff64a239..d52c7cce 100644 --- a/src/Mapster.EF6/Mapster.EF6.csproj +++ b/src/Mapster.EF6/Mapster.EF6.csproj @@ -8,7 +8,7 @@ True true Mapster.EF6.snk - 2.0.0 + 2.0.1-pre01 diff --git a/src/Mapster.EFCore/Mapster.EFCore.csproj b/src/Mapster.EFCore/Mapster.EFCore.csproj index 120f7b3c..ca434975 100644 --- a/src/Mapster.EFCore/Mapster.EFCore.csproj +++ b/src/Mapster.EFCore/Mapster.EFCore.csproj @@ -8,7 +8,7 @@ True true Mapster.EFCore.snk - 5.1.0 + 5.1.1-pre01 diff --git a/src/Mapster.Immutable/Mapster.Immutable.csproj b/src/Mapster.Immutable/Mapster.Immutable.csproj index 7a273b42..a5ff59a6 100644 --- a/src/Mapster.Immutable/Mapster.Immutable.csproj +++ b/src/Mapster.Immutable/Mapster.Immutable.csproj @@ -7,7 +7,7 @@ Mapster;Immutable true Mapster.Immutable.snk - 1.0.0 + 1.0.1-pre01 enable diff --git a/src/Mapster.JsonNet/Mapster.JsonNet.csproj b/src/Mapster.JsonNet/Mapster.JsonNet.csproj index 69387e62..3459a1c0 100644 --- a/src/Mapster.JsonNet/Mapster.JsonNet.csproj +++ b/src/Mapster.JsonNet/Mapster.JsonNet.csproj @@ -7,7 +7,7 @@ Mapster;Json.net true Mapster.JsonNet.snk - 1.1.1-pre01 + 1.1.1-pre02 diff --git a/src/Mapster.Tool/Mapster.Tool.csproj b/src/Mapster.Tool/Mapster.Tool.csproj index f62994ab..2b5eefc9 100644 --- a/src/Mapster.Tool/Mapster.Tool.csproj +++ b/src/Mapster.Tool/Mapster.Tool.csproj @@ -10,7 +10,7 @@ Mapster;Tool true Mapster.Tool.snk - 8.4.0-pre03 + 8.4.0-pre04 enable diff --git a/src/Mapster/Mapster.csproj b/src/Mapster/Mapster.csproj index 267d910e..45723f66 100644 --- a/src/Mapster/Mapster.csproj +++ b/src/Mapster/Mapster.csproj @@ -17,7 +17,7 @@ 1.6.1 true Mapster - 7.4.0-pre03 + 7.4.0-pre04 enable 1701;1702;8618 diff --git a/src/Pack.bat b/src/Pack.bat index cfb24046..f999173d 100644 --- a/src/Pack.bat +++ b/src/Pack.bat @@ -1,4 +1,4 @@ if not exist "Packages" (mkdir "Packages") else (del /F /Q "Packages\*") dotnet restore -dotnet msbuild /t:build /p:Configuration=Release /p:GeneratePackageOnBuild=false /p:ExcludeGeneratedDebugSymbol=false -dotnet pack -c Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -o Packages \ No newline at end of file +dotnet msbuild /t:build /p:Configuration=Release /p:GeneratePackageOnBuild=false /p:ExcludeGeneratedDebugSymbol=false Mapster.sln +dotnet pack -c Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -o Packages Mapster.sln \ No newline at end of file From 7f49243b002b1194f269a8e4ccd5807b9b951bfc Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Sat, 7 Jan 2023 15:07:53 +0100 Subject: [PATCH 06/14] Bump version numbers --- src/Mapster.Tool/Mapster.Tool.csproj | 2 +- src/Mapster/Mapster.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Mapster.Tool/Mapster.Tool.csproj b/src/Mapster.Tool/Mapster.Tool.csproj index 2b5eefc9..c8c37bca 100644 --- a/src/Mapster.Tool/Mapster.Tool.csproj +++ b/src/Mapster.Tool/Mapster.Tool.csproj @@ -10,7 +10,7 @@ Mapster;Tool true Mapster.Tool.snk - 8.4.0-pre04 + 8.4.0-pre05 enable diff --git a/src/Mapster/Mapster.csproj b/src/Mapster/Mapster.csproj index 45723f66..aac4e232 100644 --- a/src/Mapster/Mapster.csproj +++ b/src/Mapster/Mapster.csproj @@ -17,7 +17,7 @@ 1.6.1 true Mapster - 7.4.0-pre04 + 7.4.0-pre05 enable 1701;1702;8618 From 84cd3478f4bfd0e922397e7150d5f0b1cb66d246 Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Tue, 7 Feb 2023 22:40:05 +0100 Subject: [PATCH 07/14] Added preprocessing flag that forces MapContext.Current to use ThreadStatic instead of AsyncLocal. --- src/Mapster.Core/MapContext/MapContext.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Mapster.Core/MapContext/MapContext.cs b/src/Mapster.Core/MapContext/MapContext.cs index 6af8263d..bd16dd7f 100644 --- a/src/Mapster.Core/MapContext/MapContext.cs +++ b/src/Mapster.Core/MapContext/MapContext.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; using System.Threading; - namespace Mapster { /// @@ -15,7 +14,7 @@ namespace Mapster /// public class MapContext { -#if NETSTANDARD || NET6_0_OR_GREATER +#if (NETSTANDARD || NET6_0_OR_GREATER) && !MAPSTER_FORCE_LEGACY_MAPCONTEXT private static readonly AsyncLocal _localContext = new AsyncLocal(); public static MapContext? Current { From d154ced44ac4180dbfcb8f196cc3984b020a84af Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Tue, 7 Feb 2023 22:43:19 +0100 Subject: [PATCH 08/14] Added regression test to test that mapping to an existing struct works as intended --- .../WhenMappingWithAdaptIgnoreRegression.cs | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/Mapster.Tests/WhenMappingWithAdaptIgnoreRegression.cs diff --git a/src/Mapster.Tests/WhenMappingWithAdaptIgnoreRegression.cs b/src/Mapster.Tests/WhenMappingWithAdaptIgnoreRegression.cs new file mode 100644 index 00000000..13638083 --- /dev/null +++ b/src/Mapster.Tests/WhenMappingWithAdaptIgnoreRegression.cs @@ -0,0 +1,55 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; +using System; + +namespace Mapster.Tests +{ + /// + /// Regression test for https://github.com/MapsterMapper/Mapster/issues/450 + /// + [TestClass] + public class WhenMappingWithAdaptIgnoreRegression + { + public abstract class Base + { + public DateTime CreatedOn { get; private set; } + + public DateTime UpdatedOn { get; set; } + + public int State { get; set; } + + public Base() + { + CreatedOn = DateTime.UtcNow; + UpdatedOn = DateTime.UtcNow; + State = 1; + } + } + + public class Poco : Base + { + public string Name { get; set; } + } + + public class Dto + { + public string Name { get; set; } + } + + [TestMethod] + public void TestMapStructToExistingStruct() + { + TypeAdapterConfig + .ForType() + .Ignore(s => s.State) + .Ignore(s => s.CreatedOn) + .Ignore(s => s.UpdatedOn); + + var destination = new Poco() { Name = "Destination", State = 2 }; + var source = new Dto() { Name = "Source" }; + var result = source.Adapt(destination); + result.State.ShouldBe(2); + result.Name.ShouldBe("Source"); + } + } +} \ No newline at end of file From 3a4ee19ba412aa1e030c4b7890d9eeccf4c78c05 Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Tue, 7 Feb 2023 22:56:49 +0100 Subject: [PATCH 09/14] Added regression test that test mapping records with parameters --- ...appingWithParametersOnRecordsRegression.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/Mapster.Tests/WhenMappingWithParametersOnRecordsRegression.cs diff --git a/src/Mapster.Tests/WhenMappingWithParametersOnRecordsRegression.cs b/src/Mapster.Tests/WhenMappingWithParametersOnRecordsRegression.cs new file mode 100644 index 00000000..3468660a --- /dev/null +++ b/src/Mapster.Tests/WhenMappingWithParametersOnRecordsRegression.cs @@ -0,0 +1,32 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; +using System; + +namespace Mapster.Tests +{ + /// + /// This turned out not to be a bug, but I left the regression test here anyway + /// + [TestClass] + public class WhenMappingWithParametersOnRecordsRegression + { + record Class1(string Title); + record Class2(int Id, string Title); + + [TestMethod] + public void TestMapRecordsWithParameters() + { + TypeAdapterConfig.NewConfig() + .Map(dest => dest.Id, + src => MapContext.Current.Parameters["Id"]); + + var c1 = new Class1("title1"); + var c2 = c1.BuildAdapter() + .AddParameters("Id", 1) + .AdaptToType(); + + c2.Id.ShouldBe(1); + c2.Title.ShouldBe("title1"); + } + } +} \ No newline at end of file From 08621ce7652a564416da95303e8d048477a79389 Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Tue, 7 Feb 2023 22:57:28 +0100 Subject: [PATCH 10/14] Added regression test for testing mapping from a secondary source object. --- .../WhenMappingWithSecondSourceObject.cs | 63 +++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 src/Mapster.Tests/WhenMappingWithSecondSourceObject.cs diff --git a/src/Mapster.Tests/WhenMappingWithSecondSourceObject.cs b/src/Mapster.Tests/WhenMappingWithSecondSourceObject.cs new file mode 100644 index 00000000..a3b31181 --- /dev/null +++ b/src/Mapster.Tests/WhenMappingWithSecondSourceObject.cs @@ -0,0 +1,63 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Shouldly; +using System; + +namespace Mapster.Tests +{ + + /// + /// Regression for https://github.com/MapsterMapper/Mapster/issues/485 + /// + [TestClass] + public class WhenMappingWithSecondSourceObject + { + public interface ISomeType + { + public int Id { get; set; } + public string Name { get; set; } + public string Address { get; set; } + } + public class ConcreteType1 : ISomeType + { + public int Id { get; set; } + public string Name { get; set; } + + public string Address { get; set; } + } + + public class ConcreteType2 + { + public int Id { get; set; } + public string Name { get; set; } + } + + [TestMethod] + public void TestMapFromSecondSourceObject() + { + var c1 = new ConcreteType1 + { + Id = 1, + Name = "Name 1", + Address = "Address 1" + }; + + var c2 = new ConcreteType2 + { + Id = 2, + Name = "Name 2" + }; + + var generatedType = c1.Adapt(); + + generatedType.Id.ShouldBe(1); + generatedType.Name.ShouldBe("Name 1"); + generatedType.Address.ShouldBe("Address 1"); + + generatedType = c2.Adapt(generatedType); + + generatedType.Id.ShouldBe(2); + generatedType.Name.ShouldBe("Name 2"); + generatedType.Address.ShouldBe("Address 1"); + } + } +} \ No newline at end of file From 48cac1a08e976015277e0dc58853010845c7f00d Mon Sep 17 00:00:00 2001 From: EniacMlezi <15785664+EniacMlezi@users.noreply.github.com> Date: Tue, 14 Feb 2023 15:50:16 -0500 Subject: [PATCH 11/14] Mapster.Tool catch ReflectionTypeLoadException --- src/Mapster.Core/Utils/Extensions.cs | 16 +++- src/Mapster.Tool/AssemblyResolver.cs | 114 --------------------------- src/Mapster.Tool/Extensions.cs | 5 +- src/Mapster.Tool/Program.cs | 16 ++-- src/Mapster/TypeAdapterConfig.cs | 2 +- 5 files changed, 26 insertions(+), 127 deletions(-) delete mode 100644 src/Mapster.Tool/AssemblyResolver.cs diff --git a/src/Mapster.Core/Utils/Extensions.cs b/src/Mapster.Core/Utils/Extensions.cs index b8c8dc5a..b8ecf8de 100644 --- a/src/Mapster.Core/Utils/Extensions.cs +++ b/src/Mapster.Core/Utils/Extensions.cs @@ -1,9 +1,12 @@ using System; +using System.Collections.Generic; +using System.Linq; using System.Linq.Expressions; +using System.Reflection; namespace Mapster.Utils { - static class Extensions + public static class Extensions { #if NET40 public static Type GetTypeInfo(this Type type) @@ -27,5 +30,16 @@ public static string GetMemberName(this LambdaExpression lambda) return prop; } + public static IEnumerable GetLoadableTypes(this Assembly assembly) + { + try + { + return assembly.GetTypes(); + } + catch (ReflectionTypeLoadException e) + { + return e.Types.Where(t => t != null).Cast(); + } + } } } diff --git a/src/Mapster.Tool/AssemblyResolver.cs b/src/Mapster.Tool/AssemblyResolver.cs deleted file mode 100644 index 41325cb3..00000000 --- a/src/Mapster.Tool/AssemblyResolver.cs +++ /dev/null @@ -1,114 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Reflection; -using System.Runtime.Loader; -using System.Text.Json.Serialization; -using Microsoft.Extensions.DependencyModel; -using Microsoft.Extensions.DependencyModel.Resolution; - -namespace Mapster.Tool -{ - // https://www.codeproject.com/Articles/1194332/Resolving-Assemblies-in-NET-Core - internal sealed class AssemblyResolver : IDisposable - { - private readonly ICompilationAssemblyResolver _assemblyResolver; - private readonly DependencyContext _dependencyContext; - private readonly AssemblyLoadContext _loadContext; - - public AssemblyResolver(string path) - { - Assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(path); - _dependencyContext = DependencyContext.Load(Assembly); - - _assemblyResolver = new CompositeCompilationAssemblyResolver - (new ICompilationAssemblyResolver[] - { - new AppBaseCompilationAssemblyResolver(Path.GetDirectoryName(path)), - new ReferenceAssemblyPathResolver(), - new PackageCompilationAssemblyResolver(), - }); - - _loadContext = AssemblyLoadContext.GetLoadContext(Assembly)!; - _loadContext.Resolving += OnResolving; - } - - public Assembly Assembly { get; } - - public void Dispose() - { - _loadContext.Resolving -= OnResolving; - } - - private Assembly? OnResolving(AssemblyLoadContext context, AssemblyName name) - { - //hack for loaded assemblies - if (name.Name == "Mapster") - return typeof(TypeAdapterConfig).Assembly; - if (name.Name == "Mapster.Core") - return typeof(MapperAttribute).Assembly; - if (name.Name == "System.Text.Json") - return typeof(JsonIgnoreAttribute).Assembly; - - var (library, assetPath) = (from lib in _dependencyContext.RuntimeLibraries - from grp in lib.RuntimeAssemblyGroups - where grp.Runtime == string.Empty - from path in grp.AssetPaths - where string.Equals(GetAssemblyName(path), name.Name, StringComparison.OrdinalIgnoreCase) - select (lib, path)).FirstOrDefault(); - - if (library == null) - { - Console.WriteLine("Cannot find library: " + name.Name); - return null; - } - - try - { - var wrapped = new CompilationLibrary( - library.Type, - library.Name, - library.Version, - library.Hash, - new[] {assetPath}, - library.Dependencies, - library.Serviceable); - - var assemblies = new List(); - _assemblyResolver.TryResolveAssemblyPaths(wrapped, assemblies); - - if (assemblies.Count == 0) - { - Console.WriteLine($"Cannot find assembly path: {name.Name} (type={library.Type}, version={library.Version})"); - return null; - } - - return _loadContext.LoadFromAssemblyPath(assemblies[0]); - } - catch (Exception ex) - { - Console.WriteLine($"Cannot find assembly path: {name.Name} (type={library.Type}, version={library.Version})"); - Console.WriteLine("exception: " + ex.Message); - return null; - } - } - - private const string NativeImageSufix = ".ni"; - private static string GetAssemblyName(string assetPath) - { - var name = Path.GetFileNameWithoutExtension(assetPath); - if (name == null) - { - throw new ArgumentException($"Provided path has empty file name '{assetPath}'", nameof(assetPath)); - } - - if (name.EndsWith(NativeImageSufix)) - { - name = name.Substring(0, name.Length - NativeImageSufix.Length); - } - - return name; - } - } -} \ No newline at end of file diff --git a/src/Mapster.Tool/Extensions.cs b/src/Mapster.Tool/Extensions.cs index 0deba21a..da6c7644 100644 --- a/src/Mapster.Tool/Extensions.cs +++ b/src/Mapster.Tool/Extensions.cs @@ -1,4 +1,5 @@ -using System; +using Mapster.Utils; +using System; using System.Collections; using System.Collections.Generic; using System.Linq; @@ -174,7 +175,7 @@ public static Type MakeNullable(this Type type) public static void Scan(this CodeGenerationConfig config, Assembly assembly) { - var registers = assembly.GetTypes() + var registers = assembly.GetLoadableTypes() .Where(x => typeof(ICodeGenerationRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract) .Select(type => (ICodeGenerationRegister) Activator.CreateInstance(type)!); diff --git a/src/Mapster.Tool/Program.cs b/src/Mapster.Tool/Program.cs index 9874279d..0d4babc0 100644 --- a/src/Mapster.Tool/Program.cs +++ b/src/Mapster.Tool/Program.cs @@ -7,6 +7,7 @@ using CommandLine; using ExpressionDebugger; using Mapster.Models; +using Mapster.Utils; namespace Mapster.Tool { @@ -58,13 +59,12 @@ private static void WriteFile(string code, string path) private static void GenerateMappers(MapperOptions opt) { - using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly)); - var assembly = dynamicContext.Assembly; + var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly)); var config = TypeAdapterConfig.GlobalSettings; config.SelfContainedCodeGeneration = true; config.Scan(assembly); - foreach (var type in assembly.GetTypes()) + foreach (var type in assembly.GetLoadableTypes()) { if (!type.IsInterface) continue; @@ -149,12 +149,11 @@ private static string GetImplName(string name) private static void GenerateModels(ModelOptions opt) { - using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly)); - var assembly = dynamicContext.Assembly; + var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly)); var codeGenConfig = new CodeGenerationConfig(); codeGenConfig.Scan(assembly); - var types = assembly.GetTypes().ToHashSet(); + var types = assembly.GetLoadableTypes().ToHashSet(); foreach (var builder in codeGenConfig.AdaptAttributeBuilders) { foreach (var setting in builder.TypeSettings) @@ -381,8 +380,7 @@ private static void ApplySettings(TypeAdapterSetter setter, BaseAdaptAttribute a private static void GenerateExtensions(ExtensionOptions opt) { - using var dynamicContext = new AssemblyResolver(Path.GetFullPath(opt.Assembly)); - var assembly = dynamicContext.Assembly; + var assembly = Assembly.LoadFrom(Path.GetFullPath(opt.Assembly)); var config = TypeAdapterConfig.GlobalSettings; config.SelfContainedCodeGeneration = true; config.Scan(assembly); @@ -397,7 +395,7 @@ private static void GenerateExtensions(ExtensionOptions opt) assemblies.Add(setting.Key.Assembly); } } - var types = assemblies.SelectMany(it => it.GetTypes()).ToHashSet(); + var types = assemblies.SelectMany(it => it.GetLoadableTypes()).ToHashSet(); var configDict = new Dictionary(); foreach (var builder in codeGenConfig.AdaptAttributeBuilders) { diff --git a/src/Mapster/TypeAdapterConfig.cs b/src/Mapster/TypeAdapterConfig.cs index 265e4164..ae08b2a1 100644 --- a/src/Mapster/TypeAdapterConfig.cs +++ b/src/Mapster/TypeAdapterConfig.cs @@ -643,7 +643,7 @@ public void CompileProjection(Type sourceType, Type destinationType) public IList Scan(params Assembly[] assemblies) { - List registers = assemblies.Select(assembly => assembly.GetTypes() + List registers = assemblies.Select(assembly => assembly.GetLoadableTypes() .Where(x => typeof(IRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract)) .SelectMany(registerTypes => registerTypes.Select(registerType => (IRegister)Activator.CreateInstance(registerType))).ToList(); From 48745ed8cec4d26a2dc3732eba705b75b8a8bf32 Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Mon, 27 Feb 2023 00:59:28 +0100 Subject: [PATCH 12/14] Fix warning message --- src/Pack.bat | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pack.bat b/src/Pack.bat index f999173d..5c4f3a11 100644 --- a/src/Pack.bat +++ b/src/Pack.bat @@ -1,4 +1,4 @@ if not exist "Packages" (mkdir "Packages") else (del /F /Q "Packages\*") -dotnet restore +dotnet restore Mapster.sln dotnet msbuild /t:build /p:Configuration=Release /p:GeneratePackageOnBuild=false /p:ExcludeGeneratedDebugSymbol=false Mapster.sln dotnet pack -c Release -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg -o Packages Mapster.sln \ No newline at end of file From 068c1740bb5480a1ca173f15999f9b3ab40ca8fa Mon Sep 17 00:00:00 2001 From: Andreas Ravnestad Date: Mon, 27 Feb 2023 00:59:54 +0100 Subject: [PATCH 13/14] Update version numbers --- src/Mapster.Async/Mapster.Async.csproj | 2 +- src/Mapster.Core/Mapster.Core.csproj | 2 +- .../Mapster.DependencyInjection.csproj | 2 +- src/Mapster.EF6/Mapster.EF6.csproj | 2 +- src/Mapster.EFCore/Mapster.EFCore.csproj | 2 +- src/Mapster.Immutable/Mapster.Immutable.csproj | 2 +- src/Mapster.JsonNet/Mapster.JsonNet.csproj | 2 +- src/Mapster.Tool/Mapster.Tool.csproj | 2 +- src/Mapster/Mapster.csproj | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Mapster.Async/Mapster.Async.csproj b/src/Mapster.Async/Mapster.Async.csproj index e1b5de64..2e5503bb 100644 --- a/src/Mapster.Async/Mapster.Async.csproj +++ b/src/Mapster.Async/Mapster.Async.csproj @@ -7,7 +7,7 @@ Mapster;Async true Mapster.Async.snk - 2.0.1-pre01 + 2.0.1-pre02 diff --git a/src/Mapster.Core/Mapster.Core.csproj b/src/Mapster.Core/Mapster.Core.csproj index c0718384..e5902ce7 100644 --- a/src/Mapster.Core/Mapster.Core.csproj +++ b/src/Mapster.Core/Mapster.Core.csproj @@ -4,7 +4,7 @@ net7.0;net6.0 Mapster.Core mapster - 1.2.1-pre03 + 1.2.1-pre04 enable true true diff --git a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj index 2a11e5f1..87a2a89b 100644 --- a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj +++ b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj @@ -7,7 +7,7 @@ Mapster;DependencyInjection true Mapster.DependencyInjection.snk - 1.0.1-pre01 + 1.0.1-pre02 diff --git a/src/Mapster.EF6/Mapster.EF6.csproj b/src/Mapster.EF6/Mapster.EF6.csproj index d52c7cce..e7b1489b 100644 --- a/src/Mapster.EF6/Mapster.EF6.csproj +++ b/src/Mapster.EF6/Mapster.EF6.csproj @@ -8,7 +8,7 @@ True true Mapster.EF6.snk - 2.0.1-pre01 + 2.0.1-pre02 diff --git a/src/Mapster.EFCore/Mapster.EFCore.csproj b/src/Mapster.EFCore/Mapster.EFCore.csproj index ca434975..40c4294e 100644 --- a/src/Mapster.EFCore/Mapster.EFCore.csproj +++ b/src/Mapster.EFCore/Mapster.EFCore.csproj @@ -8,7 +8,7 @@ True true Mapster.EFCore.snk - 5.1.1-pre01 + 5.1.1-pre02 diff --git a/src/Mapster.Immutable/Mapster.Immutable.csproj b/src/Mapster.Immutable/Mapster.Immutable.csproj index a5ff59a6..0d6addb3 100644 --- a/src/Mapster.Immutable/Mapster.Immutable.csproj +++ b/src/Mapster.Immutable/Mapster.Immutable.csproj @@ -7,7 +7,7 @@ Mapster;Immutable true Mapster.Immutable.snk - 1.0.1-pre01 + 1.0.1-pre02 enable diff --git a/src/Mapster.JsonNet/Mapster.JsonNet.csproj b/src/Mapster.JsonNet/Mapster.JsonNet.csproj index 3459a1c0..0a22e89b 100644 --- a/src/Mapster.JsonNet/Mapster.JsonNet.csproj +++ b/src/Mapster.JsonNet/Mapster.JsonNet.csproj @@ -7,7 +7,7 @@ Mapster;Json.net true Mapster.JsonNet.snk - 1.1.1-pre02 + 1.1.1-pre03 diff --git a/src/Mapster.Tool/Mapster.Tool.csproj b/src/Mapster.Tool/Mapster.Tool.csproj index c8c37bca..b04a51c5 100644 --- a/src/Mapster.Tool/Mapster.Tool.csproj +++ b/src/Mapster.Tool/Mapster.Tool.csproj @@ -10,7 +10,7 @@ Mapster;Tool true Mapster.Tool.snk - 8.4.0-pre05 + 8.4.0-pre06 enable diff --git a/src/Mapster/Mapster.csproj b/src/Mapster/Mapster.csproj index aac4e232..6e314e65 100644 --- a/src/Mapster/Mapster.csproj +++ b/src/Mapster/Mapster.csproj @@ -17,7 +17,7 @@ 1.6.1 true Mapster - 7.4.0-pre05 + 7.4.0-pre06 enable 1701;1702;8618 From 1e1d1f909134809afa6b0dacca52360d921bb1b7 Mon Sep 17 00:00:00 2001 From: hoseinhabibiyan Date: Sun, 3 Sep 2023 11:38:41 +0330 Subject: [PATCH 14/14] Methods Documention --- src/Mapster.Async/TypeAdapterExtensions.cs | 42 ++++- .../Register/AdaptAttributeBuilder.cs | 135 ++++++++++++-- .../Register/PropertySettingBuilder.cs | 42 ++++- .../ServiceMapper.cs | 59 +++++- .../TypeAdapterBuilderExtensions.cs | 9 +- src/Mapster/Interfaces/IMapper.cs | 66 ++++++- src/Mapster/Mapper.cs | 57 +++++- src/Mapster/TypeAdapterBuilder.cs | 69 ++++++- src/Mapster/TypeAdapterConfig.cs | 168 +++++++++++++++--- 9 files changed, 572 insertions(+), 75 deletions(-) diff --git a/src/Mapster.Async/TypeAdapterExtensions.cs b/src/Mapster.Async/TypeAdapterExtensions.cs index 5ace867c..b42a754e 100644 --- a/src/Mapster.Async/TypeAdapterExtensions.cs +++ b/src/Mapster.Async/TypeAdapterExtensions.cs @@ -13,7 +13,16 @@ internal static U GetValueOrDefault(this IDictionary dict, T key) return dict.TryGetValue(key, out var value) ? value : default; } - public static TypeAdapterSetter AfterMappingAsync( + + /// + /// Setup async operation + /// + /// + /// + /// + /// + /// + public static TypeAdapterSetter AfterMappingAsync( this TypeAdapterSetter setter, Func action) { setter.AfterMapping(dest => @@ -27,7 +36,17 @@ public static TypeAdapterSetter AfterMappingAsync( return setter; } - public static TypeAdapterSetter AfterMappingAsync( + + /// + /// Setup async operation + /// + /// + /// + /// + /// + /// + /// + public static TypeAdapterSetter AfterMappingAsync( this TypeAdapterSetter setter, Func action) { setter.AfterMapping((src, dest) => @@ -41,7 +60,14 @@ public static TypeAdapterSetter AfterMappingAsync AdaptToTypeAsync(this IAdapterBuilder builder) + + /// + /// Map asynchronously to destination type. + /// + /// Destination type to map. + /// + /// Type of destination object that mapped. + public static async Task AdaptToTypeAsync(this IAdapterBuilder builder) { var tasks = new List(); builder.Parameters[ASYNC_KEY] = tasks; @@ -54,7 +80,15 @@ public static async Task AdaptToTypeAsync(this IAdap } } - public static async Task AdaptToAsync(this IAdapterBuilder builder, TDestination destination) + + /// + /// Map asynchronously to destination type. + /// + /// Destination type to map. + /// + /// Destination object to map. + /// Type of destination object that mapped. + public static async Task AdaptToAsync(this IAdapterBuilder builder, TDestination destination) { var tasks = new List(); builder.Parameters[ASYNC_KEY] = tasks; diff --git a/src/Mapster.Core/Register/AdaptAttributeBuilder.cs b/src/Mapster.Core/Register/AdaptAttributeBuilder.cs index 93a85de1..6797700c 100644 --- a/src/Mapster.Core/Register/AdaptAttributeBuilder.cs +++ b/src/Mapster.Core/Register/AdaptAttributeBuilder.cs @@ -16,7 +16,12 @@ public AdaptAttributeBuilder(BaseAdaptAttribute attribute) this.Attribute = attribute; } - public AdaptAttributeBuilder ForTypes(params Type[] types) + /// + /// Configures the builder for specific types. + /// + /// Types to configure. + /// + public AdaptAttributeBuilder ForTypes(params Type[] types) { foreach (var type in types) { @@ -27,7 +32,14 @@ public AdaptAttributeBuilder ForTypes(params Type[] types) return this; } - public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace) + + /// + /// Configures the builder for all types in a given namespace within an assembly. + /// + /// The assembly containing the types. + /// The namespace of the types to include. + /// + public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace) { foreach (var type in assembly.GetTypes()) { @@ -40,7 +52,14 @@ public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @n return this; } - public AdaptAttributeBuilder ForType(Action>? propertyConfig = null) + + /// + /// Configures the builder for a specific type and allows for property-specific configuration. + /// + /// + /// An optional action for configuring properties of the specified type. + /// + public AdaptAttributeBuilder ForType(Action>? propertyConfig = null) { if (!this.TypeSettings.TryGetValue(typeof(T), out var settings)) { @@ -52,7 +71,13 @@ public AdaptAttributeBuilder ForType(Action>? prope return this; } - public AdaptAttributeBuilder ExcludeTypes(params Type[] types) + + /// + /// Excludes specific types from the configuration. + /// + /// An array of types to exclude. + /// + public AdaptAttributeBuilder ExcludeTypes(params Type[] types) { foreach (var type in types) { @@ -62,7 +87,13 @@ public AdaptAttributeBuilder ExcludeTypes(params Type[] types) return this; } - public AdaptAttributeBuilder ExcludeTypes(Func predicate) + + /// + /// Exclude certain types from the adaptation process based on a provided predicate. + /// + /// Predicate function should evaluate to true for types that you want to exclude from the mapping and false for types that should not be excluded. + /// + public AdaptAttributeBuilder ExcludeTypes(Func predicate) { foreach (var type in this.TypeSettings.Keys.ToList()) { @@ -73,67 +104,135 @@ public AdaptAttributeBuilder ExcludeTypes(Func predicate) return this; } - public AdaptAttributeBuilder IgnoreAttributes(params Type[] attributes) + + /// + /// Specifies attributes to ignore during mapping. + /// + /// An array of attributes to ignore. + /// + public AdaptAttributeBuilder IgnoreAttributes(params Type[] attributes) { this.Attribute.IgnoreAttributes = attributes; return this; } - public AdaptAttributeBuilder IgnoreNoAttributes(params Type[] attributes) + + /// + /// Specifies attributes that should not be ignored during mapping. + /// + /// An array of attributes that should not be ignored. + /// + public AdaptAttributeBuilder IgnoreNoAttributes(params Type[] attributes) { this.Attribute.IgnoreNoAttributes = attributes; return this; } - public AdaptAttributeBuilder IgnoreNamespaces(params string[] namespaces) + + /// + /// Specifies namespaces to ignore during mapping. + /// + /// An array of namespaces to ignore. + /// + public AdaptAttributeBuilder IgnoreNamespaces(params string[] namespaces) { this.Attribute.IgnoreNamespaces = namespaces; return this; } - public AdaptAttributeBuilder IgnoreNullValues(bool value) + + /// + /// Configures whether null values should be ignored during mapping. + /// + /// A boolean value indicating whether to ignore null values. + /// + public AdaptAttributeBuilder IgnoreNullValues(bool value) { this.Attribute.IgnoreNullValues = value; return this; } - public AdaptAttributeBuilder RequireDestinationMemberSource(bool value) + + /// + /// Configures whether a destination member source is required during. + /// + /// A boolean value indicating whether a destination member source is required. + /// + public AdaptAttributeBuilder RequireDestinationMemberSource(bool value) { this.Attribute.RequireDestinationMemberSource = value; return this; } - public AdaptAttributeBuilder MapToConstructor(bool value) + + /// + /// Configures whether mapping should be performed to constructors. + /// + /// A boolean value indicating whether mapping to constructors is enabled. + /// + public AdaptAttributeBuilder MapToConstructor(bool value) { this.Attribute.MapToConstructor = value; return this; } - public AdaptAttributeBuilder MaxDepth(int depth) + + /// + /// Sets the maximum depth for mapping. + /// + /// The maximum depth for mapping. + /// + public AdaptAttributeBuilder MaxDepth(int depth) { this.Attribute.MaxDepth = depth; return this; } - - public AdaptAttributeBuilder PreserveReference(bool value) + + + /// + /// Configures whether to preserve object references during mapping. + /// + /// A boolean value indicating whether to preserve object references. + /// + public AdaptAttributeBuilder PreserveReference(bool value) { this.Attribute.PreserveReference = value; return this; } - - public AdaptAttributeBuilder ShallowCopyForSameType(bool value) + + + /// + /// Configures whether to perform a shallow copy for the same source and destination type. + /// + /// A boolean value indicating whether to perform a shallow copy. + /// + public AdaptAttributeBuilder ShallowCopyForSameType(bool value) { this.Attribute.ShallowCopyForSameType = value; return this; } - public AdaptAttributeBuilder AlterType() + + /// + /// Forward property types. + /// + /// Forward property from type. + /// Forward property to type. + /// + public AdaptAttributeBuilder AlterType() { this.AlterTypes.Add(type => type == typeof(TFrom) ? typeof(TTo) : null); return this; } - public AdaptAttributeBuilder AlterType(Func predicate, Type toType) + + /// + /// Forward property types for Code generation. + /// + /// A function that takes a Type as input and returns a Boolean value. This function is used to evaluate whether the forward property should be applied to the target type. If the predicate returns true, the target type will be replaced; otherwise, it remains unchanged. + /// Type of destination to forward property type. + /// + public AdaptAttributeBuilder AlterType(Func predicate, Type toType) { this.AlterTypes.Add(type => predicate(type) ? toType : null); return this; diff --git a/src/Mapster.Core/Register/PropertySettingBuilder.cs b/src/Mapster.Core/Register/PropertySettingBuilder.cs index 03d10634..69b35b1c 100644 --- a/src/Mapster.Core/Register/PropertySettingBuilder.cs +++ b/src/Mapster.Core/Register/PropertySettingBuilder.cs @@ -23,21 +23,45 @@ private PropertySetting ForProperty(string name) return setting; } - public PropertySettingBuilder Ignore(Expression> member) + + /// + /// Ignore a specific property during mapping. + /// + /// + /// A lambda expression that identifies the property to be ignored during mapping. + /// + public PropertySettingBuilder Ignore(Expression> member) { var setting = ForProperty(member.GetMemberName()); setting.Ignore = true; return this; } - public PropertySettingBuilder Map(Expression> member, string targetPropertyName) + + /// + /// Map a specific property of the source type to a target property with a different name during mapping. + /// + /// + /// A lambda expression that identifies the source property to be mapped. + /// The name of the target property to which the source property should be mapped during the mapping process. + /// + public PropertySettingBuilder Map(Expression> member, string targetPropertyName) { var setting = ForProperty(member.GetMemberName()); setting.TargetPropertyName = targetPropertyName; return this; } - public PropertySettingBuilder Map(Expression> member, Type targetPropertyType, string? targetPropertyName = null) + + /// + /// Map a specific property of the source type to a target property with a different type and name during mapping. + /// + /// + /// A lambda expression that identifies the source property to be mapped. + /// The type of the target property to which the source property should be mapped during the mapping process. + /// The name of the target property to which the source property should be mapped. + /// + public PropertySettingBuilder Map(Expression> member, Type targetPropertyType, string? targetPropertyName = null) { var setting = ForProperty(member.GetMemberName()); setting.TargetPropertyType = targetPropertyType; @@ -45,7 +69,17 @@ public PropertySettingBuilder Map(Expression> membe return this; } - public PropertySettingBuilder Map(Expression> member, Expression> mapFunc, string? targetPropertyName = null) + + /// + /// Map a specific property of the source type to a target property using a custom mapping function. + /// + /// Type of source property. + /// Type of target property type. + /// A lambda expression that identifies the source property to be mapped. + /// A lambda expression that defines the custom mapping function. + /// The name of the target property to which the source property should be mapped. + /// + public PropertySettingBuilder Map(Expression> member, Expression> mapFunc, string? targetPropertyName = null) { var setting = ForProperty(member.GetMemberName()); setting.MapFunc = mapFunc; diff --git a/src/Mapster.DependencyInjection/ServiceMapper.cs b/src/Mapster.DependencyInjection/ServiceMapper.cs index 03c8fc3d..1d909893 100644 --- a/src/Mapster.DependencyInjection/ServiceMapper.cs +++ b/src/Mapster.DependencyInjection/ServiceMapper.cs @@ -13,41 +13,88 @@ public ServiceMapper(IServiceProvider serviceProvider, TypeAdapterConfig config) _serviceProvider = serviceProvider; } - public override ITypeAdapterBuilder From(TSource source) + /// + /// Create mapping builder. + /// + /// Source type to create mapping builder. + /// Source object to create mapping builder. + /// + public override ITypeAdapterBuilder From(TSource source) { return base.From(source) .AddParameters(DI_KEY, _serviceProvider); } - public override TDestination Map(object source) + + /// + /// Perform mapping from source object to type of destination. + /// + /// Destination type to create mapping builder. + /// Source object to create mapping builder. + /// Type of destination object that mapped. + public override TDestination Map(object source) { using var scope = new MapContextScope(); scope.Context.Parameters[DI_KEY] = _serviceProvider; return base.Map(source); } - public override TDestination Map(TSource source) + + /// + /// Perform mapping from type of source to type of destination. + /// + /// Source type to map. + /// Destination type to map. + /// Source object to map. + /// Type of destination object that mapped. + public override TDestination Map(TSource source) { using var scope = new MapContextScope(); scope.Context.Parameters[DI_KEY] = _serviceProvider; return base.Map(source); } - public override TDestination Map(TSource source, TDestination destination) + + /// + /// Perform mapping from type of source to type of destination. + /// + /// Source type to map. + /// Destination type to map. + /// Source object to map. + /// Destination object to map. + /// Type of destination object that mapped. + public override TDestination Map(TSource source, TDestination destination) { using var scope = new MapContextScope(); scope.Context.Parameters[DI_KEY] = _serviceProvider; return base.Map(source, destination); } - public override object Map(object source, Type sourceType, Type destinationType) + + /// + /// Perform mapping source object from source type to destination type. + /// + /// Source object to map. + /// Source type to map. + /// Destination type to map. + /// Mapped object. + public override object Map(object source, Type sourceType, Type destinationType) { using var scope = new MapContextScope(); scope.Context.Parameters[DI_KEY] = _serviceProvider; return base.Map(source, sourceType, destinationType); } - public override object Map(object source, object destination, Type sourceType, Type destinationType) + + /// + /// Perform mapping source object from source type to destination type. + /// + /// Source object to map. + /// Destination object to map. + /// Source type to map. + /// Destination type to map. + /// + public override object Map(object source, object destination, Type sourceType, Type destinationType) { using var scope = new MapContextScope(); scope.Context.Parameters[DI_KEY] = _serviceProvider; diff --git a/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs b/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs index 11fa744d..3ab0cb5b 100644 --- a/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs +++ b/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs @@ -80,7 +80,14 @@ public static ITypeAdapterBuilder EntityFromContext(this IType }, context.GetType().FullName); } - public static IQueryable ProjectToType(this IAdapterBuilder source) + + /// + /// Mapping from queryable. + /// + /// Type of destination. + /// Source object to adopt. + /// + public static IQueryable ProjectToType(this IAdapterBuilder source) { var queryable = source.Source.ProjectToType(source.Config); if (!source.HasParameter || source.Parameters.All(it => it.Key.StartsWith("Mapster."))) diff --git a/src/Mapster/Interfaces/IMapper.cs b/src/Mapster/Interfaces/IMapper.cs index f32b4b51..201420b0 100644 --- a/src/Mapster/Interfaces/IMapper.cs +++ b/src/Mapster/Interfaces/IMapper.cs @@ -7,11 +7,65 @@ namespace MapsterMapper public interface IMapper { TypeAdapterConfig Config { get; } - ITypeAdapterBuilder From(TSource source); - TDestination Map(object source); - TDestination Map(TSource source); - TDestination Map(TSource source, TDestination destination); - object Map(object source, Type sourceType, Type destinationType); - object Map(object source, object destination, Type sourceType, Type destinationType); + + + /// + /// Create mapping builder. + /// + /// Source type to create mapping builder. + /// Source object to create mapping builder. + /// Adapter builder type. + ITypeAdapterBuilder From(TSource source); + + + /// + /// Perform mapping from source object to type of destination. + /// + /// Destination type to create mapping builder. + /// Source object to create mapping builder. + /// Type of destination object that mapped. + TDestination Map(object source); + + + /// + /// Perform mapping from type of source to type of destination. + /// + /// Source type to map. + /// Destination type to map. + /// Source object to map. + /// Type of destination object that mapped. + TDestination Map(TSource source); + + + /// + /// Perform mapping from type of source to type of destination. + /// + /// Source type to map. + /// Destination type to map. + /// Source object to map. + /// Destination object to map. + /// Type of destination object that mapped. + TDestination Map(TSource source, TDestination destination); + + + /// + /// Perform mapping source object from source type to destination type. + /// + /// Source object to map. + /// Source type to map. + /// Destination type to map. + /// Mapped object. + object Map(object source, Type sourceType, Type destinationType); + + + /// + /// Perform mapping source object from source type to destination type. + /// + /// Source object to map. + /// Destination object to map. + /// Source type to map. + /// Destination type to map. + /// Mapped object. + object Map(object source, object destination, Type sourceType, Type destinationType); } } diff --git a/src/Mapster/Mapper.cs b/src/Mapster/Mapper.cs index d63f6213..2694c7f3 100644 --- a/src/Mapster/Mapper.cs +++ b/src/Mapster/Mapper.cs @@ -16,12 +16,25 @@ public Mapper(TypeAdapterConfig config) Config = config; } - public virtual ITypeAdapterBuilder From(TSource source) + /// + /// Create mapping builder. + /// + /// Source type to create mapping builder. + /// + /// + public virtual ITypeAdapterBuilder From(TSource source) { return TypeAdapter.BuildAdapter(source, Config); } - public virtual TDestination Map(object source) + + /// + /// Perform mapping source object to type of destination. + /// + /// Destination type to perform mapping + /// Source object to perform mapping. + /// type of destination mapping result. + public virtual TDestination Map(object source) { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (source == null) @@ -31,18 +44,43 @@ public virtual TDestination Map(object source) return fn(source); } - public virtual TDestination Map(TSource source) + + /// + /// Perform mapping from type of source to type of destination. + /// + /// Source type to map. + /// Destination type to map. + /// + /// type of destination mapping result + public virtual TDestination Map(TSource source) { var fn = Config.GetMapFunction(); return fn(source); } - public virtual TDestination Map(TSource source, TDestination destination) + + /// + /// Perform mapping from type of source to type of destination. + /// + /// Source type to map. + /// Destination type to map. + /// Source object to map. + /// Destination type to map. + /// type of destination mapping result + public virtual TDestination Map(TSource source, TDestination destination) { var fn = Config.GetMapToTargetFunction(); return fn(source, destination); } + + /// + /// Perform mapping source object from source type to destination type. + /// + /// Source object to map. + /// Source type to map. + /// Destination type to map. + /// mapped result object public virtual object Map(object source, Type sourceType, Type destinationType) { var del = Config.GetMapFunction(sourceType, destinationType); @@ -59,7 +97,16 @@ public virtual object Map(object source, Type sourceType, Type destinationType) } } - public virtual object Map(object source, object destination, Type sourceType, Type destinationType) + + /// + /// Perform mapping source object to destination object from source type to destination type. + /// + /// Source object to map. + /// Destination object to map. + /// Source type to map. + /// Destination type to map. + /// mapped result object + public virtual object Map(object source, object destination, Type sourceType, Type destinationType) { var del = Config.GetMapToTargetFunction(sourceType, destinationType); if (sourceType.GetTypeInfo().IsVisible && destinationType.GetTypeInfo().IsVisible) diff --git a/src/Mapster/TypeAdapterBuilder.cs b/src/Mapster/TypeAdapterBuilder.cs index 30c76880..26f93efa 100644 --- a/src/Mapster/TypeAdapterBuilder.cs +++ b/src/Mapster/TypeAdapterBuilder.cs @@ -25,7 +25,15 @@ internal TypeAdapterBuilder(TSource source, TypeAdapterConfig config) Config = config; } - [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")] + + /// + /// Allow you to keep config and mapping inline. + /// + /// + /// + /// + /// + [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")] public ITypeAdapterBuilder ForkConfig(Action action, #if !NET40 [CallerFilePath] @@ -40,7 +48,14 @@ public ITypeAdapterBuilder ForkConfig(Action action, return this; } - public ITypeAdapterBuilder AddParameters(string name, object value) + + /// + /// Passing runtime value. + /// + /// Parameter name. + /// Parameter value + /// + public ITypeAdapterBuilder AddParameters(string name, object value) { Parameters.Add(name, value); return this; @@ -58,9 +73,14 @@ private MapContextScope CreateMapContextScope() return scope; } - MapContextScope IAdapterBuilder.CreateMapContextScope() => CreateMapContextScope(); - public TDestination AdaptToType() + MapContextScope IAdapterBuilder.CreateMapContextScope() => CreateMapContextScope(); + /// + /// Mapping to new type using in adapter builder scenario. + /// + /// Destination type to adopt. + /// + public TDestination AdaptToType() { if (_parameters == null) return Map(); @@ -71,13 +91,26 @@ public TDestination AdaptToType() } } - private TDestination Map() + + /// + /// Perform mapping to type of destination in adapter builder scenario. + /// + /// Destination type to map. + /// + private TDestination Map() { var fn = Config.GetMapFunction(); return fn(Source); } - public TDestination AdaptTo(TDestination destination) + + /// + /// Mapping to existing object in adapter builder scenario. + /// + /// Destination type to adopt. + /// + /// + public TDestination AdaptTo(TDestination destination) { if (_parameters == null) return MapToTarget(destination); @@ -94,19 +127,37 @@ private TDestination MapToTarget(TDestination destination) return fn(Source, destination); } - public Expression> CreateMapExpression() + + /// + /// Get mapping expression. + /// + /// Destination type to create map expression. + /// + public Expression> CreateMapExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); return (Expression>) Config.CreateMapExpression(tuple, MapType.Map); } - public Expression> CreateMapToTargetExpression() + + /// + /// Get mapping to existing object expression. + /// + /// Destination type to create map to target expression. + /// + public Expression> CreateMapToTargetExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); return (Expression>) Config.CreateMapExpression(tuple, MapType.MapToTarget); } - public Expression> CreateProjectionExpression() + + /// + /// Get mapping from queryable expression. + /// + /// Destination type to create projection expression. + /// + public Expression> CreateProjectionExpression() { var tuple = new TypeTuple(typeof(TSource), typeof(TDestination)); return (Expression>) Config.CreateMapExpression(tuple, MapType.Projection); diff --git a/src/Mapster/TypeAdapterConfig.cs b/src/Mapster/TypeAdapterConfig.cs index 47de4086..f5ebbb3a 100644 --- a/src/Mapster/TypeAdapterConfig.cs +++ b/src/Mapster/TypeAdapterConfig.cs @@ -103,7 +103,13 @@ public TypeAdapterConfig() }); } - public TypeAdapterSetter When(Func canMap) + + /// + /// allows you to specify conditions for when a mapping should occur based on source and destination types and the mapping type. + /// + /// + /// + public TypeAdapterSetter When(Func canMap) { var rule = new TypeAdapterRule { @@ -114,7 +120,13 @@ public TypeAdapterSetter When(Func canMap) return new TypeAdapterSetter(rule.Settings, this); } - public TypeAdapterSetter When(Func canMap) + + /// + /// allows you to specify conditions for when a mapping should occur based on PreCompileArgument delegate + /// + /// + /// + public TypeAdapterSetter When(Func canMap) { var rule = new TypeAdapterRule { @@ -125,40 +137,80 @@ public TypeAdapterSetter When(Func canMap) return new TypeAdapterSetter(rule.Settings, this); } - public TypeAdapterSetter NewConfig() + + /// + /// Creates a new configuration for mapping between source and destination types. + /// + /// Source type. + /// Destination type. + /// + public TypeAdapterSetter NewConfig() { Remove(typeof(TSource), typeof(TDestination)); return ForType(); } - public TypeAdapterSetter NewConfig(Type sourceType, Type destinationType) + + /// + /// Creates a new configuration for mapping between source and destination types. + /// + /// Source type to create new configuration. + /// Destination type to create new configuration. + /// + public TypeAdapterSetter NewConfig(Type sourceType, Type destinationType) { Remove(sourceType, destinationType); return ForType(sourceType, destinationType); } - public TypeAdapterSetter ForType() + + /// + /// Configures a mapping for a specific source and destination type pair. + /// + /// + /// + /// + public TypeAdapterSetter ForType() { var key = new TypeTuple(typeof(TSource), typeof(TDestination)); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); } - public TypeAdapterSetter ForType(Type sourceType, Type destinationType) + + /// + /// Configures a mapping for a specific source and destination type pair. + /// + /// + /// + /// + public TypeAdapterSetter ForType(Type sourceType, Type destinationType) { var key = new TypeTuple(sourceType, destinationType); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); } - public TypeAdapterSetter ForDestinationType() + + /// + /// Configures a mapping for a specific destination type. + /// + /// Destination type. + /// + public TypeAdapterSetter ForDestinationType() { var key = new TypeTuple(typeof(void), typeof(TDestination)); var settings = GetSettings(key); return new TypeAdapterSetter(settings, this); } - public TypeAdapterSetter ForDestinationType(Type destinationType) + + /// + /// Configures a mapping for a specific destination type. + /// + /// Destination type. + /// + public TypeAdapterSetter ForDestinationType(Type destinationType) { var key = new TypeTuple(typeof(void), destinationType); var settings = GetSettings(key); @@ -582,7 +634,13 @@ private CompileArgument GetCompileArgument(TypeTuple tuple, MapType mapType, Com }; } - public void Compile(bool failFast = true) + + /// + /// Validates and cache mapping instructions. + /// + /// A boolean parameter that determines whether exceptions should be thrown immediately when mapping errors occur or whether to collect and aggregate them. The default value is true. + /// + public void Compile(bool failFast = true) { var exceptions = new List(); var keys = RuleMap.Keys.ToList(); @@ -614,7 +672,13 @@ public void Compile(bool failFast = true) } } - public void Compile(Type sourceType, Type destinationType) + + /// + /// Validates and cache mapping instructions. + /// + /// Source type to compile. + /// Destination type to compile. + public void Compile(Type sourceType, Type destinationType) { var tuple = new TypeTuple(sourceType, destinationType); _mapDict[tuple] = Compiler(CreateMapExpression(tuple, MapType.Map)); @@ -626,7 +690,11 @@ public void Compile(Type sourceType, Type destinationType) } } - public void CompileProjection() + + /// + /// Validates and cache mapping instructions for queryable. + /// + public void CompileProjection() { var keys = RuleMap.Keys.ToList(); foreach (var key in keys) @@ -635,13 +703,25 @@ public void CompileProjection() } } - public void CompileProjection(Type sourceType, Type destinationType) + + /// + /// Validates and cache mapping instructions for queryable. + /// + /// Source type to compile. + /// Destination type to compile. + public void CompileProjection(Type sourceType, Type destinationType) { var tuple = new TypeTuple(sourceType, destinationType); _projectionDict[tuple] = CreateProjectionCallExpression(tuple); } - public IList Scan(params Assembly[] assemblies) + + /// + /// Scans and registers mappings from specified assemblies. + /// + /// assemblies to scan. + /// A list of registered mappings + public IList Scan(params Assembly[] assemblies) { List registers = assemblies.Select(assembly => assembly.GetLoadableTypes() .Where(x => typeof(IRegister).GetTypeInfo().IsAssignableFrom(x.GetTypeInfo()) && x.GetTypeInfo().IsClass && !x.GetTypeInfo().IsAbstract)) @@ -652,12 +732,22 @@ public IList Scan(params Assembly[] assemblies) return registers; } - public void Apply(IEnumerable> registers) + + /// + /// Applies type mappings. + /// + /// collection of IRegister interface to apply mapping. + public void Apply(IEnumerable> registers) { Apply(registers.Select(register => register.Value)); } - public void Apply(IEnumerable registers) + + /// + /// Applies type mappings. + /// + /// collection of IRegister interface to apply mapping. + public void Apply(IEnumerable registers) { foreach (IRegister register in registers) { @@ -665,7 +755,12 @@ public void Apply(IEnumerable registers) } } - public void Apply(params IRegister[] registers) + + /// + /// Applies type mappings. + /// + /// IRegister interface params to apply mapping. + public void Apply(params IRegister[] registers) { foreach (IRegister register in registers) { @@ -673,7 +768,11 @@ public void Apply(params IRegister[] registers) } } - internal void Clear() + + /// + /// Clears all type mapping rules and settings + /// + internal void Clear() { var keys = RuleMap.Keys.ToList(); foreach (var key in keys) @@ -682,7 +781,13 @@ internal void Clear() } } - internal void Remove(Type sourceType, Type destinationType) + + /// + /// Removes a specific type mapping rule. + /// + /// Source type to remove. + /// Destination type to remove. + internal void Remove(Type sourceType, Type destinationType) { var key = new TypeTuple(sourceType, destinationType); Remove(key); @@ -706,7 +811,13 @@ private void Remove(TypeTuple key) .MapWith(src => src.Clone(), true); return config; }); - public TypeAdapterConfig Clone() + + + /// + /// Clones the current TypeAdapterConfig. + /// + /// + public TypeAdapterConfig Clone() { var fn = _cloneConfig.Value.GetMapFunction(); return fn(this); @@ -737,17 +848,30 @@ public TypeAdapterConfig Fork(Action action, public static class TypeAdapterConfig { - public static TypeAdapterSetter NewConfig() + /// + /// Creates a new configuration for mapping between the source and destination types. + /// + /// + public static TypeAdapterSetter NewConfig() { return TypeAdapterConfig.GlobalSettings.NewConfig(); } - public static TypeAdapterSetter ForType() + + /// + /// Creates a configuration for mapping between the source and destination types. + /// + /// + public static TypeAdapterSetter ForType() { return TypeAdapterConfig.GlobalSettings.ForType(); } - public static void Clear() + + /// + /// Clears the type mapping configuration for the specified source and destination types. + /// + public static void Clear() { TypeAdapterConfig.GlobalSettings.Remove(typeof(TSource), typeof(TDestination)); }