Skip to content

Commit

Permalink
fix #66 - MapContext should be public
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Jun 22, 2016
1 parent db42b8e commit 0ac3cfa
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 84 deletions.
6 changes: 3 additions & 3 deletions src/Mapster/Adapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Mapster
{
public interface IAdapter
{
TypeAdapter<TSource> BuildAdapter<TSource>(TSource source);
TypeAdapterBuiler<TSource> BuildAdapter<TSource>(TSource source);
TDestination Adapt<TDestination>(object source);
TDestination Adapt<TSource, TDestination>(TSource source);
TDestination Adapt<TSource, TDestination>(TSource source, TDestination destination);
Expand All @@ -23,9 +23,9 @@ public Adapter(TypeAdapterConfig config)
_config = config;
}

public TypeAdapter<TSource> BuildAdapter<TSource>(TSource source)
public TypeAdapterBuiler<TSource> BuildAdapter<TSource>(TSource source)
{
return new TypeAdapter<TSource>(source).UseConfig(_config);
return new TypeAdapterBuiler<TSource>(source, _config);
}

public TDestination Adapt<TDestination>(object source)
Expand Down
4 changes: 2 additions & 2 deletions src/Mapster/MapContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public int GetHashCode(object obj)
/// You can get context by MapContext.Current
/// And all mapping processes will having only one context
/// </remarks>
internal class MapContext
public class MapContext
{
[ThreadStatic]
private static MapContext _current;
Expand All @@ -49,7 +49,7 @@ public Dictionary<string, object> Parameters
get { return _parameters ?? (_parameters = new Dictionary<string, object>(ReferenceComparer.Default)); }
}
}
internal class MapContextScope : IDisposable
public class MapContextScope : IDisposable
{
public MapContext Context { get; }

Expand Down
1 change: 1 addition & 0 deletions src/Mapster/Mapster.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
<Compile Include="IRegister.cs" />
<Compile Include="MapContext.cs" />
<Compile Include="NameMatchingStrategy.cs" />
<Compile Include="TypeAdapterBuiler.cs" />
<Compile Include="ValueAccessingStrategy.cs" />
<Compile Include="Models\AccessModifier.cs" />
<Compile Include="Utils\BlockExpressionDetector.cs" />
Expand Down
6 changes: 6 additions & 0 deletions src/Mapster/Mapster.nuget.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<Project ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Target Name="EmitMSBuildWarning" BeforeTargets="Build">
<Warning Text="Packages containing MSBuild targets and props files cannot be fully installed in projects targeting multiple frameworks. The MSBuild targets and props files have been ignored." />
</Target>
</Project>
2 changes: 1 addition & 1 deletion src/Mapster/QueryableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public static IQueryable<TDestination> ProjectToType<TDestination>(this IQueryab
var mockCall = config.GetProjectionCallExpression(source.ElementType, typeof(TDestination));
var sourceCall = Expression.Call(mockCall.Method, source.Expression, mockCall.Arguments[1]);
return source.Provider.CreateQuery<TDestination>(sourceCall);
}
}
}
}
84 changes: 7 additions & 77 deletions src/Mapster/TypeAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
using System;
using System.Collections.Generic;

namespace Mapster
{
public static class TypeAdapter
{
public static TypeAdapter<TSource> BuildAdapter<TSource>(this TSource source)
public static TypeAdapterBuiler<TSource> BuildAdapter<TSource>(this TSource source)
{
return new TypeAdapter<TSource>(source);
return BuildAdapter(source, TypeAdapterConfig.GlobalSettings);
}

public static TypeAdapterBuiler<TSource> BuildAdapter<TSource>(this TSource source, TypeAdapterConfig config)
{
return new TypeAdapterBuiler<TSource>(source, config);
}

/// <summary>
Expand Down Expand Up @@ -159,78 +163,4 @@ internal static class TypeAdapter<TSource, TDestination>
{
public static Func<TSource, TDestination> Map = TypeAdapterConfig.GlobalSettings.GetMapFunction<TSource, TDestination>();
}

public class TypeAdapter<TSource>
{
TSource Source { get; }
TypeAdapterConfig Config { get; set; }

private Dictionary<string, object> _parameters;
Dictionary<string, object> Parameters
{
get { return _parameters ?? (_parameters = new Dictionary<string, object>(ReferenceComparer.Default)); }
}

public TypeAdapter(TSource source)
{
this.Source = source;
this.Config = TypeAdapterConfig.GlobalSettings;
}

public TypeAdapter<TSource> UseConfig(TypeAdapterConfig config)
{
this.Config = config;
return this;
}

public TypeAdapter<TSource> AddParameters(string name, object value)
{
this.Parameters.Add(name, value);
return this;
}

public TDestination AdaptToType<TDestination>()
{
if (_parameters == null)
return Map<TDestination>();

using (var scope = new MapContextScope())
{
var parameters = scope.Context.Parameters;
foreach (var kvp in _parameters)
{
parameters[kvp.Key] = kvp.Value;
}
return Map<TDestination>();
}
}

private TDestination Map<TDestination>()
{
var fn = this.Config.GetMapFunction<TSource, TDestination>();
return fn(this.Source);
}

public TDestination AdaptTo<TDestination>(TDestination destination)
{
if (_parameters == null)
return MapToTarget(destination);

using (var scope = new MapContextScope())
{
var parameters = scope.Context.Parameters;
foreach (var kvp in _parameters)
{
parameters[kvp.Key] = kvp.Value;
}
return MapToTarget(destination);
}
}

private TDestination MapToTarget<TDestination>(TDestination destination)
{
var fn = this.Config.GetMapToTargetFunction<TSource, TDestination>();
return fn(this.Source, destination);
}
}
}
72 changes: 72 additions & 0 deletions src/Mapster/TypeAdapterBuiler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System.Collections.Generic;

namespace Mapster
{
public class TypeAdapterBuiler<TSource>
{
TSource Source { get; }
TypeAdapterConfig Config { get; }

private Dictionary<string, object> _parameters;
Dictionary<string, object> Parameters
{
get { return _parameters ?? (_parameters = new Dictionary<string, object>(ReferenceComparer.Default)); }
}

internal TypeAdapterBuiler(TSource source, TypeAdapterConfig config)
{
this.Source = source;
this.Config = config;
}

public TypeAdapterBuiler<TSource> AddParameters(string name, object value)
{
this.Parameters.Add(name, value);
return this;
}

public TDestination AdaptToType<TDestination>()
{
if (_parameters == null)
return Map<TDestination>();

using (var scope = new MapContextScope())
{
var parameters = scope.Context.Parameters;
foreach (var kvp in _parameters)
{
parameters[kvp.Key] = kvp.Value;
}
return Map<TDestination>();
}
}

private TDestination Map<TDestination>()
{
var fn = this.Config.GetMapFunction<TSource, TDestination>();
return fn(this.Source);
}

public TDestination AdaptTo<TDestination>(TDestination destination)
{
if (_parameters == null)
return MapToTarget(destination);

using (var scope = new MapContextScope())
{
var parameters = scope.Context.Parameters;
foreach (var kvp in _parameters)
{
parameters[kvp.Key] = kvp.Value;
}
return MapToTarget(destination);
}
}

private TDestination MapToTarget<TDestination>(TDestination destination)
{
var fn = this.Config.GetMapToTargetFunction<TSource, TDestination>();
return fn(this.Source, destination);
}
}
}
2 changes: 1 addition & 1 deletion src/Mapster/project.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.4.0",
"version": "2.4.1",
"description": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster.",
"packOptions": {
"summary": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, but simpler and way faster.",
Expand Down

0 comments on commit 0ac3cfa

Please sign in to comment.