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 54862393..530de3d6 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 b4059942..8d4f5de6 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 bbbe1731..132ef0a5 100644
--- a/src/Mapster.Async/Mapster.Async.csproj
+++ b/src/Mapster.Async/Mapster.Async.csproj
@@ -1,13 +1,13 @@
- net6.0
+ net7.0;net6.0
Async supports for Mapster
true
Mapster;Async
true
Mapster.Async.snk
- 2.0.0
+ 2.0.1-pre02
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/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
{
diff --git a/src/Mapster.Core/Mapster.Core.csproj b/src/Mapster.Core/Mapster.Core.csproj
index 77ca9e26..a15e23cc 100644
--- a/src/Mapster.Core/Mapster.Core.csproj
+++ b/src/Mapster.Core/Mapster.Core.csproj
@@ -1,10 +1,10 @@
Lightweight library for Mapster and Mapster CodeGen
- net6.0
+ net7.0;net6.0
Mapster.Core
mapster
- 1.2.1-pre02
+ 1.2.1-pre04
enable
true
true
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.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.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 b56893ad..4723750a 100644
--- a/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj
+++ b/src/Mapster.DependencyInjection/Mapster.DependencyInjection.csproj
@@ -1,13 +1,13 @@
- net6.0
+ net7.0;net6.0
Dependency Injection supports for Mapster
true
Mapster;DependencyInjection
true
Mapster.DependencyInjection.snk
- 1.0.1
+ 1.0.1-pre02
diff --git a/src/Mapster.DependencyInjection/ServiceMapper.cs b/src/Mapster.DependencyInjection/ServiceMapper.cs
index bfaddb3d..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 TypeAdapterBuilder 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.EF6/Mapster.EF6.csproj b/src/Mapster.EF6/Mapster.EF6.csproj
index f722df5b..e7b1489b 100644
--- a/src/Mapster.EF6/Mapster.EF6.csproj
+++ b/src/Mapster.EF6/Mapster.EF6.csproj
@@ -1,14 +1,14 @@
- net6.0
+ net7.0;net6.0
EF6 plugin for Mapster
true
Mapster;EF6
True
true
Mapster.EF6.snk
- 2.0.0
+ 2.0.1-pre02
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.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 8eef0933..40c4294e 100644
--- a/src/Mapster.EFCore/Mapster.EFCore.csproj
+++ b/src/Mapster.EFCore/Mapster.EFCore.csproj
@@ -1,14 +1,14 @@
- net6.0
+ net7.0;net6.0
EFCore plugin for Mapster
true
Mapster;EFCore
True
true
Mapster.EFCore.snk
- 5.1.0
+ 5.1.1-pre02
diff --git a/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs b/src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs
index 33ede59b..f417651c 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
@@ -88,7 +88,14 @@ public static TypeAdapterBuilder EntityFromContext(this TypeAd
}, 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.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 5d03ec76..0d6addb3 100644
--- a/src/Mapster.Immutable/Mapster.Immutable.csproj
+++ b/src/Mapster.Immutable/Mapster.Immutable.csproj
@@ -1,13 +1,13 @@
- net6.0
+ net7.0;net6.0
Immutable collection supports for Mapster
true
Mapster;Immutable
true
Mapster.Immutable.snk
- 1.0.0
+ 1.0.1-pre02
enable
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 d73c2058..0a22e89b 100644
--- a/src/Mapster.JsonNet/Mapster.JsonNet.csproj
+++ b/src/Mapster.JsonNet/Mapster.JsonNet.csproj
@@ -1,13 +1,13 @@
- net6.0
+ net7.0;net6.0
Json.net conversion supports for Mapster
true
Mapster;Json.net
true
Mapster.JsonNet.snk
- 1.1.1-pre01
+ 1.1.1-pre03
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 78768508..1ea56308 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.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
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
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
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/Mapster.Tool.csproj b/src/Mapster.Tool/Mapster.Tool.csproj
index c6ea4c66..b04a51c5 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
@@ -10,7 +10,7 @@
Mapster;Tool
true
Mapster.Tool.snk
- 8.4.0-pre03
+ 8.4.0-pre06
enable
diff --git a/src/Mapster.Tool/Program.cs b/src/Mapster.Tool/Program.cs
index 0f03634b..4066ef52 100644
--- a/src/Mapster.Tool/Program.cs
+++ b/src/Mapster.Tool/Program.cs
@@ -8,6 +8,7 @@
using CommandLine;
using ExpressionDebugger;
using Mapster.Models;
+using Mapster.Utils;
namespace Mapster.Tool
{
@@ -59,13 +60,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;
@@ -150,12 +150,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)
@@ -382,8 +381,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);
@@ -398,11 +396,10 @@ 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();
// assemblies defines open generic only, so we have to add specialised types used in mappings
foreach (var (key, _) in config.RuleMap) types.Add(key.Source);
-
var configDict = new Dictionary();
foreach (var builder in codeGenConfig.AdaptAttributeBuilders)
{
diff --git a/src/Mapster/Interfaces/IMapper.cs b/src/Mapster/Interfaces/IMapper.cs
index 4ef3d2b0..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; }
- TypeAdapterBuilder 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/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..2694c7f3 100644
--- a/src/Mapster/Mapper.cs
+++ b/src/Mapster/Mapper.cs
@@ -16,12 +16,25 @@ public Mapper(TypeAdapterConfig config)
Config = config;
}
- public virtual TypeAdapterBuilder From(TSource source)
+ ///
+ /// Create mapping builder.
+ ///
+ /// Source type to create mapping builder.
+ ///
+ ///
+ public virtual ITypeAdapterBuilder From(TSource source)
{
- return new TypeAdapterBuilder(source, Config);
+ 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)
@@ -76,4 +123,4 @@ public virtual object Map(object source, object destination, Type sourceType, Ty
}
}
-}
\ No newline at end of file
+}
diff --git a/src/Mapster/Mapster.csproj b/src/Mapster/Mapster.csproj
index f3aae63c..81283f31 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
@@ -16,7 +16,7 @@
true
Mapster
- 7.4.0-pre03
+ 7.4.0-pre06
enable
1701;1702;8618
diff --git a/src/Mapster/TypeAdapter.cs b/src/Mapster/TypeAdapter.cs
index 794a7b57..2e1db2f1 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 13005760..26f93efa 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 => Source;
@@ -25,8 +25,16 @@ internal TypeAdapterBuilder(TSource source, TypeAdapterConfig config)
Config = config;
}
- [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
- public TypeAdapterBuilder ForkConfig(Action action,
+
+ ///
+ /// Allow you to keep config and mapping inline.
+ ///
+ ///
+ ///
+ ///
+ ///
+ [SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
+ public ITypeAdapterBuilder ForkConfig(Action action,
#if !NET40
[CallerFilePath]
#endif
@@ -40,7 +48,14 @@ public TypeAdapterBuilder ForkConfig(Action action,
return this;
}
- public TypeAdapterBuilder 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,22 +127,40 @@ 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);
}
}
-}
\ No newline at end of file
+}
diff --git a/src/Mapster/TypeAdapterConfig.cs b/src/Mapster/TypeAdapterConfig.cs
index 73efdcd3..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,15 +703,27 @@ 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.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();
@@ -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));
}
diff --git a/src/Pack.bat b/src/Pack.bat
index cfb24046..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 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 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
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