Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor IMapper.From to return interface #454

Merged
merged 4 commits into from
Sep 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Mapster.DependencyInjection/ServiceMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ServiceMapper(IServiceProvider serviceProvider, TypeAdapterConfig config)
_serviceProvider = serviceProvider;
}

public override TypeAdapterBuilder<TSource> From<TSource>(TSource source)
public override ITypeAdapterBuilder<TSource> From<TSource>(TSource source)
{
return base.From(source)
.AddParameters(DI_KEY, _serviceProvider);
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster.EF6/TypeAdapterBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mapster
{
public static class TypeAdapterBuilderExtensions
{
public static TypeAdapterBuilder<TSource> EntityFromContext<TSource>(this TypeAdapterBuilder<TSource> builder, IObjectContextAdapter context)
public static ITypeAdapterBuilder<TSource> EntityFromContext<TSource>(this ITypeAdapterBuilder<TSource> builder, IObjectContextAdapter context)
{
const string DB_KEY = "Mapster.EF6.db";
return builder
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster.EFCore/TypeAdapterBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Mapster
{
public static class TypeAdapterBuilderExtensions
{
public static TypeAdapterBuilder<TSource> EntityFromContext<TSource>(this TypeAdapterBuilder<TSource> builder, DbContext context)
public static ITypeAdapterBuilder<TSource> EntityFromContext<TSource>(this ITypeAdapterBuilder<TSource> builder, DbContext context)
{
const string dbKey = "Mapster.EFCore.db";
return builder
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Interfaces/IMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace MapsterMapper
public interface IMapper
{
TypeAdapterConfig Config { get; }
TypeAdapterBuilder<TSource> From<TSource>(TSource source);
ITypeAdapterBuilder<TSource> From<TSource>(TSource source);
TDestination Map<TDestination>(object source);
TDestination Map<TSource, TDestination>(TSource source);
TDestination Map<TSource, TDestination>(TSource source, TDestination destination);
Expand Down
25 changes: 25 additions & 0 deletions src/Mapster/Interfaces/ITypeAdapterBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;

namespace Mapster
{
public interface ITypeAdapterBuilder<T> : IAdapterBuilder<T>
{
[SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
ITypeAdapterBuilder<T> ForkConfig(Action<TypeAdapterConfig> action,
#if !NET40
[CallerFilePath]
#endif
string key1 = "",
#if !NET40
[CallerLineNumber]
#endif
int key2 = 0);
ITypeAdapterBuilder<T> AddParameters(string name, object value);
Expression<Func<T, TDestination>> CreateMapExpression<TDestination>();
Expression<Func<T, TDestination, TDestination>> CreateMapToTargetExpression<TDestination>();
Expression<Func<T, TDestination>> CreateProjectionExpression<TDestination>();
}
}
6 changes: 3 additions & 3 deletions src/Mapster/Mapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public Mapper(TypeAdapterConfig config)
Config = config;
}

public virtual TypeAdapterBuilder<TSource> From<TSource>(TSource source)
public virtual ITypeAdapterBuilder<TSource> From<TSource>(TSource source)
{
return new TypeAdapterBuilder<TSource>(source, Config);
return TypeAdapter.BuildAdapter(source, Config);
}

public virtual TDestination Map<TDestination>(object source)
Expand Down Expand Up @@ -76,4 +76,4 @@ public virtual object Map(object source, object destination, Type sourceType, Ty
}
}

}
}
4 changes: 2 additions & 2 deletions src/Mapster/TypeAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace Mapster
{
public static class TypeAdapter
{
public static TypeAdapterBuilder<TSource> BuildAdapter<TSource>(this TSource source)
public static ITypeAdapterBuilder<TSource> BuildAdapter<TSource>(this TSource source)
{
return new TypeAdapterBuilder<TSource>(source, TypeAdapterConfig.GlobalSettings);
}

public static TypeAdapterBuilder<TSource> BuildAdapter<TSource>(this TSource source, TypeAdapterConfig config)
public static ITypeAdapterBuilder<TSource> BuildAdapter<TSource>(this TSource source, TypeAdapterConfig config)
{
return new TypeAdapterBuilder<TSource>(source, config);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Mapster/TypeAdapterBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace Mapster
{
public class TypeAdapterBuilder<TSource> : IAdapterBuilder<TSource>
public class TypeAdapterBuilder<TSource> : ITypeAdapterBuilder<TSource>
{
TSource Source { get; }
TSource IAdapterBuilder<TSource>.Source => this.Source;
Expand All @@ -26,7 +26,7 @@ internal TypeAdapterBuilder(TSource source, TypeAdapterConfig config)
}

[SuppressMessage("ReSharper", "ExplicitCallerInfoArgument")]
public TypeAdapterBuilder<TSource> ForkConfig(Action<TypeAdapterConfig> action,
public ITypeAdapterBuilder<TSource> ForkConfig(Action<TypeAdapterConfig> action,
#if !NET40
[CallerFilePath]
#endif
Expand All @@ -40,7 +40,7 @@ public TypeAdapterBuilder<TSource> ForkConfig(Action<TypeAdapterConfig> action,
return this;
}

public TypeAdapterBuilder<TSource> AddParameters(string name, object value)
public ITypeAdapterBuilder<TSource> AddParameters(string name, object value)
{
this.Parameters.Add(name, value);
return this;
Expand Down Expand Up @@ -112,4 +112,4 @@ public Expression<Func<TSource, TDestination>> CreateProjectionExpression<TDesti
return (Expression<Func<TSource, TDestination>>) this.Config.CreateMapExpression(tuple, MapType.Projection);
}
}
}
}