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

WIP Use dynamic method in DbSet. #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
49 changes: 43 additions & 6 deletions src/CommandGenerator/CommandGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kros.KORM.Converter;
using Kros.Caching;
using Kros.KORM.Converter;
using Kros.KORM.Metadata;
using Kros.KORM.Properties;
using Kros.KORM.Query;
Expand All @@ -8,6 +9,8 @@
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace Kros.KORM.CommandGenerator
Expand All @@ -31,12 +34,14 @@ internal class CommandGenerator<T> : ICommandGenerator<T>

#region Private Fields

private TableInfo _tableInfo;
private KORM.Query.IQueryProvider _provider;
private IQueryBase<T> _query;
private readonly TableInfo _tableInfo;
private readonly KORM.Query.IQueryProvider _provider;
private readonly IQueryBase<T> _query;
private List<ColumnInfo> _columnsInfo = null;
private int _maxParametersForDeleteCommandsInPart = DEFAULT_MAX_PARAMETERS_FOR_DELETE_COMMANDS_IN_PART;
private Lazy<string> _outputStatement;
private readonly Lazy<string> _outputStatement;
private readonly ICache<int, Delegate> _delegatesCache = new Cache<int, Delegate>();
private delegate object GetColumnValueDelegate(T item);

#endregion

Expand Down Expand Up @@ -280,7 +285,9 @@ private void AddParametersToCommand(DbCommand cmd, IEnumerable<ColumnInfo> colum
/// <inheritdoc/>
public object GetColumnValue(ColumnInfo columnInfo, T item)
{
var value = columnInfo.PropertyInfo.GetValue(item, null);
GetColumnValueDelegate invokeDelegate = GetDelegate(item, columnInfo);
var value = invokeDelegate(item);

if (value != null)
{
var converter = ConverterHelper.GetConverter(columnInfo, value.GetType());
Expand Down Expand Up @@ -368,6 +375,36 @@ private string GetDeleteCommandText(IEnumerable<ColumnInfo> columns)
return string.Format(DELETE_QUERY_BASE, _tableInfo.Name, paramWherePart.ToString());
}

private GetColumnValueDelegate GetDelegate(T item, ColumnInfo columnInfo)
{
var key = $"{columnInfo.Name}-{typeof(T).FullName}".GetHashCode();

return _delegatesCache.Get(key, () => CreateDelegate(columnInfo)) as GetColumnValueDelegate;
}

private GetColumnValueDelegate CreateDelegate(ColumnInfo columnInfo)
{
var dynamicMethodArgs = new Type[] { typeof(T) };
var dynamicMethod = new DynamicMethod("GetColumnValue", typeof(object), dynamicMethodArgs);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

ilGenerator.Emit(OpCodes.Ldarg_0);

MethodInfo fnGetValue = typeof(T).GetProperty(
columnInfo.PropertyInfo.Name, BindingFlags.Public | BindingFlags.Instance).GetGetMethod();

ilGenerator.Emit(OpCodes.Callvirt, fnGetValue);

if (columnInfo.PropertyInfo.PropertyType.IsValueType)
{
ilGenerator.Emit(OpCodes.Box, columnInfo.PropertyInfo.PropertyType);
}

ilGenerator.Emit(OpCodes.Ret);

return dynamicMethod.CreateDelegate(typeof(GetColumnValueDelegate)) as GetColumnValueDelegate;
}

#endregion
}
}
52 changes: 44 additions & 8 deletions src/Query/DbSet.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Kros.KORM.CommandGenerator;
using Kros.Caching;
using Kros.KORM.CommandGenerator;
using Kros.KORM.Data;
using Kros.KORM.Exceptions;
using Kros.KORM.Metadata;
Expand All @@ -10,6 +11,8 @@
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Threading.Tasks;

namespace Kros.KORM.Query
Expand All @@ -22,13 +25,15 @@ public class DbSet<T> : IDbSet<T>
{
#region Private fields

private ICommandGenerator<T> _commandGenerator;
private IQueryProvider _provider;
private IQueryBase<T> _query;
private HashSet<T> _addedItems = new HashSet<T>();
private HashSet<T> _editedItems = new HashSet<T>();
private HashSet<T> _deletedItems = new HashSet<T>();
private readonly ICommandGenerator<T> _commandGenerator;
private readonly IQueryProvider _provider;
private readonly IQueryBase<T> _query;
private readonly HashSet<T> _addedItems = new HashSet<T>();
private readonly HashSet<T> _editedItems = new HashSet<T>();
private readonly HashSet<T> _deletedItems = new HashSet<T>();
private readonly TableInfo _tableInfo;
private readonly ICache<int, Delegate> _delegatesCache = new Cache<int, Delegate>();
private delegate void SetIdentityPrimaryKeyDelegate(T item, object id);

#endregion

Expand Down Expand Up @@ -296,7 +301,8 @@ private async Task CommitChangesAddedItemsAsync(HashSet<T> items, bool useAsync)
if (hasIdentity)
{
var id = await ExecuteScalarAsync(command, useAsync);
_tableInfo.IdentityPrimaryKey.SetValue(item, id);
SetIdentityPrimaryKeyDelegate invokeDelegate = GetDelegate(item, _tableInfo.IdentityPrimaryKey);
invokeDelegate(item, id);
}
else
{
Expand Down Expand Up @@ -468,6 +474,36 @@ private async Task BulkUpdateCoreAsync(
}
}

private SetIdentityPrimaryKeyDelegate GetDelegate(T item, ColumnInfo columnInfo)
{
var key = $"{columnInfo.Name}-{typeof(T).FullName}".GetHashCode();

return _delegatesCache.Get(key, () => CreateDelegate(columnInfo)) as SetIdentityPrimaryKeyDelegate;
}

private SetIdentityPrimaryKeyDelegate CreateDelegate(ColumnInfo columnInfo)
{
var dynamicMethodArgs = new Type[] { typeof(T), typeof(object) };
var dynamicMethod = new DynamicMethod("IdentityPrimaryKey_SetValue", typeof(void), dynamicMethodArgs);
ILGenerator ilGenerator = dynamicMethod.GetILGenerator();

ilGenerator.Emit(OpCodes.Ldarg_0);
ilGenerator.Emit(OpCodes.Ldarg_1);

if (columnInfo.PropertyInfo.PropertyType.IsValueType)
{
ilGenerator.Emit(OpCodes.Unbox_Any, columnInfo.PropertyInfo.PropertyType);
}

MethodInfo fnGetIdentity = typeof(T).GetProperty(
columnInfo.Name, BindingFlags.Public | BindingFlags.Instance).GetSetMethod();

ilGenerator.Emit(OpCodes.Callvirt, fnGetIdentity);
ilGenerator.Emit(OpCodes.Ret);

return dynamicMethod.CreateDelegate(typeof(SetIdentityPrimaryKeyDelegate)) as SetIdentityPrimaryKeyDelegate;
}

#endregion

#region IEnumerator
Expand Down