Skip to content

Commit

Permalink
Methods Documention
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseinHabibiyan committed Sep 3, 2023
1 parent 31bc530 commit 1e1d1f9
Show file tree
Hide file tree
Showing 9 changed files with 572 additions and 75 deletions.
42 changes: 38 additions & 4 deletions src/Mapster.Async/TypeAdapterExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,16 @@ internal static U GetValueOrDefault<T, U>(this IDictionary<T, U> dict, T key)
return dict.TryGetValue(key, out var value) ? value : default;
}

public static TypeAdapterSetter<TDestination> AfterMappingAsync<TDestination>(

/// <summary>
/// Setup async operation
/// </summary>
/// <typeparam name="TDestination"></typeparam>
/// <param name="setter"></param>
/// <param name="action"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static TypeAdapterSetter<TDestination> AfterMappingAsync<TDestination>(
this TypeAdapterSetter<TDestination> setter, Func<TDestination, Task> action)
{
setter.AfterMapping(dest =>
Expand All @@ -27,7 +36,17 @@ public static TypeAdapterSetter<TDestination> AfterMappingAsync<TDestination>(
return setter;
}

public static TypeAdapterSetter<TSource, TDestination> AfterMappingAsync<TSource, TDestination>(

/// <summary>
/// Setup async operation
/// </summary>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TDestination"></typeparam>
/// <param name="setter"></param>
/// <param name="action"></param>
/// <returns></returns>
/// <exception cref="InvalidOperationException"></exception>
public static TypeAdapterSetter<TSource, TDestination> AfterMappingAsync<TSource, TDestination>(
this TypeAdapterSetter<TSource, TDestination> setter, Func<TSource, TDestination, Task> action)
{
setter.AfterMapping((src, dest) =>
Expand All @@ -41,7 +60,14 @@ public static TypeAdapterSetter<TSource, TDestination> AfterMappingAsync<TSource
return setter;
}

public static async Task<TDestination> AdaptToTypeAsync<TDestination>(this IAdapterBuilder builder)

/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> AdaptToTypeAsync<TDestination>(this IAdapterBuilder builder)
{
var tasks = new List<Task>();
builder.Parameters[ASYNC_KEY] = tasks;
Expand All @@ -54,7 +80,15 @@ public static async Task<TDestination> AdaptToTypeAsync<TDestination>(this IAdap
}
}

public static async Task<TDestination> AdaptToAsync<TDestination>(this IAdapterBuilder builder, TDestination destination)

/// <summary>
/// Map asynchronously to destination type.
/// </summary>
/// <typeparam name="TDestination">Destination type to map.</typeparam>
/// <param name="builder"></param>
/// <param name="destination">Destination object to map.</param>
/// <returns>Type of destination object that mapped.</returns>
public static async Task<TDestination> AdaptToAsync<TDestination>(this IAdapterBuilder builder, TDestination destination)
{
var tasks = new List<Task>();
builder.Parameters[ASYNC_KEY] = tasks;
Expand Down
135 changes: 117 additions & 18 deletions src/Mapster.Core/Register/AdaptAttributeBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ public AdaptAttributeBuilder(BaseAdaptAttribute attribute)
this.Attribute = attribute;
}

public AdaptAttributeBuilder ForTypes(params Type[] types)
/// <summary>
/// Configures the builder for specific types.
/// </summary>
/// <param name="types">Types to configure.</param>
/// <returns></returns>
public AdaptAttributeBuilder ForTypes(params Type[] types)
{
foreach (var type in types)
{
Expand All @@ -27,7 +32,14 @@ public AdaptAttributeBuilder ForTypes(params Type[] types)
return this;
}

public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace)

/// <summary>
/// Configures the builder for all types in a given namespace within an assembly.
/// </summary>
/// <param name="assembly">The assembly containing the types.</param>
/// <param name="namespace">The namespace of the types to include.</param>
/// <returns></returns>
public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @namespace)
{
foreach (var type in assembly.GetTypes())
{
Expand All @@ -40,7 +52,14 @@ public AdaptAttributeBuilder ForAllTypesInNamespace(Assembly assembly, string @n
return this;
}

public AdaptAttributeBuilder ForType<T>(Action<PropertySettingBuilder<T>>? propertyConfig = null)

/// <summary>
/// Configures the builder for a specific type and allows for property-specific configuration.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="propertyConfig">An optional action for configuring properties of the specified type.</param>
/// <returns></returns>
public AdaptAttributeBuilder ForType<T>(Action<PropertySettingBuilder<T>>? propertyConfig = null)
{
if (!this.TypeSettings.TryGetValue(typeof(T), out var settings))
{
Expand All @@ -52,7 +71,13 @@ public AdaptAttributeBuilder ForType<T>(Action<PropertySettingBuilder<T>>? prope
return this;
}

public AdaptAttributeBuilder ExcludeTypes(params Type[] types)

/// <summary>
/// Excludes specific types from the configuration.
/// </summary>
/// <param name="types">An array of types to exclude.</param>
/// <returns></returns>
public AdaptAttributeBuilder ExcludeTypes(params Type[] types)
{
foreach (var type in types)
{
Expand All @@ -62,7 +87,13 @@ public AdaptAttributeBuilder ExcludeTypes(params Type[] types)
return this;
}

public AdaptAttributeBuilder ExcludeTypes(Func<Type, bool> predicate)

/// <summary>
/// Exclude certain types from the adaptation process based on a provided predicate.
/// </summary>
/// <param name="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.</param>
/// <returns></returns>
public AdaptAttributeBuilder ExcludeTypes(Func<Type, bool> predicate)
{
foreach (var type in this.TypeSettings.Keys.ToList())
{
Expand All @@ -73,67 +104,135 @@ public AdaptAttributeBuilder ExcludeTypes(Func<Type, bool> predicate)
return this;
}

public AdaptAttributeBuilder IgnoreAttributes(params Type[] attributes)

/// <summary>
/// Specifies attributes to ignore during mapping.
/// </summary>
/// <param name="attributes">An array of attributes to ignore.</param>
/// <returns></returns>
public AdaptAttributeBuilder IgnoreAttributes(params Type[] attributes)
{
this.Attribute.IgnoreAttributes = attributes;
return this;
}

public AdaptAttributeBuilder IgnoreNoAttributes(params Type[] attributes)

/// <summary>
/// Specifies attributes that should not be ignored during mapping.
/// </summary>
/// <param name="attributes">An array of attributes that should not be ignored.</param>
/// <returns></returns>
public AdaptAttributeBuilder IgnoreNoAttributes(params Type[] attributes)
{
this.Attribute.IgnoreNoAttributes = attributes;
return this;
}

public AdaptAttributeBuilder IgnoreNamespaces(params string[] namespaces)

/// <summary>
/// Specifies namespaces to ignore during mapping.
/// </summary>
/// <param name="namespaces">An array of namespaces to ignore.</param>
/// <returns></returns>
public AdaptAttributeBuilder IgnoreNamespaces(params string[] namespaces)
{
this.Attribute.IgnoreNamespaces = namespaces;
return this;
}

public AdaptAttributeBuilder IgnoreNullValues(bool value)

/// <summary>
/// Configures whether null values should be ignored during mapping.
/// </summary>
/// <param name="value">A boolean value indicating whether to ignore null values.</param>
/// <returns></returns>
public AdaptAttributeBuilder IgnoreNullValues(bool value)
{
this.Attribute.IgnoreNullValues = value;
return this;
}

public AdaptAttributeBuilder RequireDestinationMemberSource(bool value)

/// <summary>
/// Configures whether a destination member source is required during.
/// </summary>
/// <param name="value">A boolean value indicating whether a destination member source is required.</param>
/// <returns></returns>
public AdaptAttributeBuilder RequireDestinationMemberSource(bool value)
{
this.Attribute.RequireDestinationMemberSource = value;
return this;
}

public AdaptAttributeBuilder MapToConstructor(bool value)

/// <summary>
/// Configures whether mapping should be performed to constructors.
/// </summary>
/// <param name="value">A boolean value indicating whether mapping to constructors is enabled.</param>
/// <returns></returns>
public AdaptAttributeBuilder MapToConstructor(bool value)
{
this.Attribute.MapToConstructor = value;
return this;
}

public AdaptAttributeBuilder MaxDepth(int depth)

/// <summary>
/// Sets the maximum depth for mapping.
/// </summary>
/// <param name="depth">The maximum depth for mapping.</param>
/// <returns></returns>
public AdaptAttributeBuilder MaxDepth(int depth)
{
this.Attribute.MaxDepth = depth;
return this;
}

public AdaptAttributeBuilder PreserveReference(bool value)


/// <summary>
/// Configures whether to preserve object references during mapping.
/// </summary>
/// <param name="value">A boolean value indicating whether to preserve object references.</param>
/// <returns></returns>
public AdaptAttributeBuilder PreserveReference(bool value)
{
this.Attribute.PreserveReference = value;
return this;
}

public AdaptAttributeBuilder ShallowCopyForSameType(bool value)


/// <summary>
/// Configures whether to perform a shallow copy for the same source and destination type.
/// </summary>
/// <param name="value">A boolean value indicating whether to perform a shallow copy.</param>
/// <returns></returns>
public AdaptAttributeBuilder ShallowCopyForSameType(bool value)
{
this.Attribute.ShallowCopyForSameType = value;
return this;
}

public AdaptAttributeBuilder AlterType<TFrom, TTo>()

/// <summary>
/// Forward property types.
/// </summary>
/// <typeparam name="TFrom">Forward property from type.</typeparam>
/// <typeparam name="TTo">Forward property to type.</typeparam>
/// <returns></returns>
public AdaptAttributeBuilder AlterType<TFrom, TTo>()
{
this.AlterTypes.Add(type => type == typeof(TFrom) ? typeof(TTo) : null);
return this;
}

public AdaptAttributeBuilder AlterType(Func<Type, bool> predicate, Type toType)

/// <summary>
/// Forward property types for Code generation.
/// </summary>
/// <param name="predicate">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.</param>
/// <param name="toType">Type of destination to forward property type.</param>
/// <returns></returns>
public AdaptAttributeBuilder AlterType(Func<Type, bool> predicate, Type toType)
{
this.AlterTypes.Add(type => predicate(type) ? toType : null);
return this;
Expand Down
42 changes: 38 additions & 4 deletions src/Mapster.Core/Register/PropertySettingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,63 @@ private PropertySetting ForProperty(string name)
return setting;
}

public PropertySettingBuilder<T> Ignore<TReturn>(Expression<Func<T, TReturn>> member)

/// <summary>
/// Ignore a specific property during mapping.
/// </summary>
/// <typeparam name="TReturn"></typeparam>
/// <param name="member">A lambda expression that identifies the property to be ignored during mapping.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Ignore<TReturn>(Expression<Func<T, TReturn>> member)
{
var setting = ForProperty(member.GetMemberName());
setting.Ignore = true;
return this;
}

public PropertySettingBuilder<T> Map<TReturn>(Expression<Func<T, TReturn>> member, string targetPropertyName)

/// <summary>
/// Map a specific property of the source type to a target property with a different name during mapping.
/// </summary>
/// <typeparam name="TReturn"></typeparam>
/// <param name="member">A lambda expression that identifies the source property to be mapped.</param>
/// <param name="targetPropertyName">The name of the target property to which the source property should be mapped during the mapping process.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Map<TReturn>(Expression<Func<T, TReturn>> member, string targetPropertyName)
{
var setting = ForProperty(member.GetMemberName());
setting.TargetPropertyName = targetPropertyName;
return this;
}

public PropertySettingBuilder<T> Map<TReturn>(Expression<Func<T, TReturn>> member, Type targetPropertyType, string? targetPropertyName = null)

/// <summary>
/// Map a specific property of the source type to a target property with a different type and name during mapping.
/// </summary>
/// <typeparam name="TReturn"></typeparam>
/// <param name="member">A lambda expression that identifies the source property to be mapped.</param>
/// <param name="targetPropertyType">The type of the target property to which the source property should be mapped during the mapping process.</param>
/// <param name="targetPropertyName">The name of the target property to which the source property should be mapped.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Map<TReturn>(Expression<Func<T, TReturn>> member, Type targetPropertyType, string? targetPropertyName = null)
{
var setting = ForProperty(member.GetMemberName());
setting.TargetPropertyType = targetPropertyType;
setting.TargetPropertyName = targetPropertyName;
return this;
}

public PropertySettingBuilder<T> Map<TReturn, TReturn2>(Expression<Func<T, TReturn>> member, Expression<Func<T, TReturn2>> mapFunc, string? targetPropertyName = null)

/// <summary>
/// Map a specific property of the source type to a target property using a custom mapping function.
/// </summary>
/// <typeparam name="TReturn">Type of source property.</typeparam>
/// <typeparam name="TReturn2">Type of target property type.</typeparam>
/// <param name="member">A lambda expression that identifies the source property to be mapped.</param>
/// <param name="mapFunc">A lambda expression that defines the custom mapping function.</param>
/// <param name="targetPropertyName">The name of the target property to which the source property should be mapped.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Map<TReturn, TReturn2>(Expression<Func<T, TReturn>> member, Expression<Func<T, TReturn2>> mapFunc, string? targetPropertyName = null)
{
var setting = ForProperty(member.GetMemberName());
setting.MapFunc = mapFunc;
Expand Down
Loading

0 comments on commit 1e1d1f9

Please sign in to comment.