Skip to content

Commit

Permalink
Use default mapping
Browse files Browse the repository at this point in the history
Use default mapping
  • Loading branch information
zzzprojects committed Oct 18, 2016
1 parent 1e0c379 commit 480aa3e
Show file tree
Hide file tree
Showing 10 changed files with 229 additions and 33 deletions.
65 changes: 62 additions & 3 deletions src/Z.Dapper.Plus.NET40/DapperPlusAction/Method/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using Z.BulkOperations;

namespace Z.Dapper.Plus
Expand Down Expand Up @@ -106,11 +107,69 @@ public void Execute()

if (!DapperPlusManager.MapperCache.TryGetValue(mapperKey, out config))
{
var elementType = DataSource.GetType().GetElementType();
var constructor = typeof (DapperPlusEntityMapper<>).MakeGenericType(elementType).GetConstructor(new Type[0]);
config = (DapperPlusEntityMapper) constructor.Invoke(new object[0]);
// CHECK for Entity Framework Proxy Type
if (mapperKey.StartsWith("zzz_proxy;"))
{
var mapperProxy = new List<DapperPlusEntityMapper>();
// Try to find if one mapping could correspond
foreach (var keyValue in DapperPlusManager.MapperCache)
{
var key = keyValue.Key;
var prefix = string.IsNullOrEmpty(Key) ? "zzz_null" : Key;

// MUST start with the same suffix
if (!key.StartsWith(prefix)) continue;

var suffix = key.Split('.').Last().Split('+').Last();
var mapperSuffix = mapperKey.Split(';').Last();

if (suffix.Length < 20)
{
// MUST BE Equal
if (suffix != mapperSuffix) continue;
}
else
{
// MUST START with same name but only one!
if (!suffix.StartsWith(mapperSuffix)) continue;
}

mapperProxy.Add(keyValue.Value);
}

if (mapperProxy.Count == 1)
{
config = mapperProxy[0];
}
}

if (config == null)
{
if (DapperPlusManager.ThrowErrorIfNotMapped)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Mapping Not Found!");
sb.AppendLine("Current MapperKey: " + mapperKey);
sb.AppendLine("Possible Mapping:");
foreach (var keyValue in DapperPlusManager.MapperCache)
{
sb.AppendLine(" - " + keyValue.Key);
}

throw new Exception(sb.ToString());
}
else
{
var type = DataSource.GetType();
var elementType = type.GetGenericArguments()[0];
var constructor = typeof(DapperPlusEntityMapper<>).MakeGenericType(elementType).GetConstructor(new Type[0]);
config = (DapperPlusEntityMapper)constructor.Invoke(new object[0]);
}

}
}

bulkOperation._isDapperPlus = true;
bulkOperation.DataSource = DataSource;
bulkOperation.AllowDuplicateKeys = true;
bulkOperation.CaseSensitive = CaseSensitiveType.DestinationInsensitive;
Expand Down
37 changes: 33 additions & 4 deletions src/Z.Dapper.Plus.NET40/DapperPlusAction/Method/Map.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Z.BulkOperations;

namespace Z.Dapper.Plus
Expand All @@ -16,7 +17,8 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
var isIdentityModified = config.IsIdentityModified();
var isIgnoreModified = config.IsIgnoreModified();

var isModified = isMapModified.HasValue && isMapModified.Value
var isModified = config._columnMappings == null
|| isMapModified.HasValue && isMapModified.Value
|| isKeyModified.HasValue && isKeyModified.Value
|| isOutputModified.HasValue && isOutputModified.Value
|| isIdentityModified.HasValue && isIdentityModified.Value
Expand Down Expand Up @@ -57,7 +59,16 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, Output = true});
// CHECK smart mapping
if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1)
{
columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString());
columnMapping.Output = true;
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, Output = true});
}
}
}

Expand All @@ -70,7 +81,16 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsPrimaryKey = true});
// CHECK smart mapping
if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1)
{
columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString());
columnMapping.IsPrimaryKey = true;
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsPrimaryKey = true});
}
}
}

Expand All @@ -83,7 +103,16 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsIdentity = true});
// CHECK smart mapping
if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1)
{
columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString());
columnMapping.IsIdentity = true;
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsIdentity = true});
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/Z.Dapper.Plus.NET40/DapperPlusManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Z.Dapper.Plus
/// <summary>Manager for dapper plus.</summary>
public class DapperPlusManager
{
public static bool ThrowErrorIfNotMapped { get; set; }
/// <summary>The mapper cache.</summary>
private static ConcurrentDictionary<string, DapperPlusEntityMapper> _mapperCache;

Expand Down Expand Up @@ -65,7 +66,20 @@ public static string GetFullMapperKey<T>(string mapperKey)

public static string GetFullMapperKey(Type type, string mapperKey)
{
var fullKey = mapperKey ?? "zzz_null" + ";" + type.FullName;
var fullKey = mapperKey ?? "zzz_null" + ";";

if (type.FullName.StartsWith("System.Data.Entity.DynamicProxies"))
{
// Oops! Entity Framework Proxy Type
var name = type.FullName.Remove(0, "System.Data.Entity.DynamicProxies.".Length).Split('_')[0];
fullKey = "zzz_proxy;" + fullKey;
fullKey += name;
}
else
{
fullKey += type.FullName;
}

return fullKey;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Z.Dapper.Plus.NET40/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("dcb2a0f8-0619-4c45-870f-fa2676fbb9b5")]
[assembly: AssemblyVersion("0.0.1")]
[assembly: AssemblyFileVersion("0.0.1")]
[assembly: AssemblyVersion("1.0.6")]
[assembly: AssemblyFileVersion("1.0.6")]
7 changes: 0 additions & 7 deletions src/Z.Dapper.Plus.NET40/Z.Dapper.Plus.NET40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Z.BulkOperations, Version=2.9.25.0, Culture=neutral, PublicKeyToken=59b66d028979105b, processorArchitecture=MSIL">
<HintPath>..\packages\Z.BulkOperations.2.9.25\lib\net40\Z.BulkOperations.dll</HintPath>
<Private>True</Private>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="BaseDapperPlusActionSet.cs" />
Expand Down Expand Up @@ -125,9 +121,6 @@
<Name>Z.BulkOperations.NET40</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
65 changes: 62 additions & 3 deletions src/Z.Dapper.Plus/DapperPlusAction/Method/Execute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using Z.BulkOperations;

namespace Z.Dapper.Plus
Expand Down Expand Up @@ -106,11 +107,69 @@ public void Execute()

if (!DapperPlusManager.MapperCache.TryGetValue(mapperKey, out config))
{
var elementType = DataSource.GetType().GetElementType();
var constructor = typeof (DapperPlusEntityMapper<>).MakeGenericType(elementType).GetConstructor(new Type[0]);
config = (DapperPlusEntityMapper) constructor.Invoke(new object[0]);
// CHECK for Entity Framework Proxy Type
if (mapperKey.StartsWith("zzz_proxy;"))
{
var mapperProxy = new List<DapperPlusEntityMapper>();
// Try to find if one mapping could correspond
foreach (var keyValue in DapperPlusManager.MapperCache)
{
var key = keyValue.Key;
var prefix = string.IsNullOrEmpty(Key) ? "zzz_null" : Key;

// MUST start with the same suffix
if (!key.StartsWith(prefix)) continue;

var suffix = key.Split('.').Last().Split('+').Last();
var mapperSuffix = mapperKey.Split(';').Last();

if (suffix.Length < 20)
{
// MUST BE Equal
if (suffix != mapperSuffix) continue;
}
else
{
// MUST START with same name but only one!
if (!suffix.StartsWith(mapperSuffix)) continue;
}

mapperProxy.Add(keyValue.Value);
}

if (mapperProxy.Count == 1)
{
config = mapperProxy[0];
}
}

if (config == null)
{
if (DapperPlusManager.ThrowErrorIfNotMapped)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Mapping Not Found!");
sb.AppendLine("Current MapperKey: " + mapperKey);
sb.AppendLine("Possible Mapping:");
foreach (var keyValue in DapperPlusManager.MapperCache)
{
sb.AppendLine(" - " + keyValue.Key);
}

throw new Exception(sb.ToString());
}
else
{
var type = DataSource.GetType();
var elementType = type.GetGenericArguments()[0];
var constructor = typeof(DapperPlusEntityMapper<>).MakeGenericType(elementType).GetConstructor(new Type[0]);
config = (DapperPlusEntityMapper)constructor.Invoke(new object[0]);
}

}
}

bulkOperation._isDapperPlus = true;
bulkOperation.DataSource = DataSource;
bulkOperation.AllowDuplicateKeys = true;
bulkOperation.CaseSensitive = CaseSensitiveType.DestinationInsensitive;
Expand Down
37 changes: 33 additions & 4 deletions src/Z.Dapper.Plus/DapperPlusAction/Method/Map.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Linq;
using Z.BulkOperations;

namespace Z.Dapper.Plus
Expand All @@ -16,7 +17,8 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
var isIdentityModified = config.IsIdentityModified();
var isIgnoreModified = config.IsIgnoreModified();

var isModified = isMapModified.HasValue && isMapModified.Value
var isModified = config._columnMappings == null
|| isMapModified.HasValue && isMapModified.Value
|| isKeyModified.HasValue && isKeyModified.Value
|| isOutputModified.HasValue && isOutputModified.Value
|| isIdentityModified.HasValue && isIdentityModified.Value
Expand Down Expand Up @@ -57,7 +59,16 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, Output = true});
// CHECK smart mapping
if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1)
{
columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString());
columnMapping.Output = true;
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, Output = true});
}
}
}

Expand All @@ -70,7 +81,16 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsPrimaryKey = true});
// CHECK smart mapping
if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1)
{
columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString());
columnMapping.IsPrimaryKey = true;
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsPrimaryKey = true});
}
}
}

Expand All @@ -83,7 +103,16 @@ public void Map(BulkOperation bulkOperation, DapperPlusEntityMapper config)
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsIdentity = true});
// CHECK smart mapping
if (string.IsNullOrEmpty(accessor.Member) && columnMappings.Count(x => x.SourceName == accessor.ToString()) == 1)
{
columnMapping = columnMappings.Find(x => x.SourceName == accessor.ToString());
columnMapping.IsIdentity = true;
}
else
{
columnMappings.Add(new DapperPlusColumnMapping {SourceName = accessor.ToString(), DestinationName = accessor.Member, IsIdentity = true});
}
}
}

Expand Down
16 changes: 15 additions & 1 deletion src/Z.Dapper.Plus/DapperPlusManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Z.Dapper.Plus
/// <summary>Manager for dapper plus.</summary>
public class DapperPlusManager
{
public static bool ThrowErrorIfNotMapped { get; set; }
/// <summary>The mapper cache.</summary>
private static ConcurrentDictionary<string, DapperPlusEntityMapper> _mapperCache;

Expand Down Expand Up @@ -65,7 +66,20 @@ public static string GetFullMapperKey<T>(string mapperKey)

public static string GetFullMapperKey(Type type, string mapperKey)
{
var fullKey = mapperKey ?? "zzz_null" + ";" + type.FullName;
var fullKey = mapperKey ?? "zzz_null" + ";";

if (type.FullName.StartsWith("System.Data.Entity.DynamicProxies"))
{
// Oops! Entity Framework Proxy Type
var name = type.FullName.Remove(0, "System.Data.Entity.DynamicProxies.".Length).Split('_')[0];
fullKey = "zzz_proxy;" + fullKey;
fullKey += name;
}
else
{
fullKey += type.FullName;
}

return fullKey;
}
}
Expand Down
Loading

0 comments on commit 480aa3e

Please sign in to comment.