-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #66 - MapContext should be public
- Loading branch information
Showing
8 changed files
with
93 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters