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 4 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
25 changes: 24 additions & 1 deletion src/CommandGenerator/CommandGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,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 Down Expand Up @@ -37,6 +39,7 @@ internal class CommandGenerator<T> : ICommandGenerator<T>
private List<ColumnInfo> _columnsInfo = null;
private int _maxParametersForDeleteCommandsInPart = DEFAULT_MAX_PARAMETERS_FOR_DELETE_COMMANDS_IN_PART;
private Lazy<string> _outputStatement;
private delegate object _getColumnValueDelegate(T item);

#endregion

Expand Down Expand Up @@ -280,7 +283,27 @@ private void AddParametersToCommand(DbCommand cmd, IEnumerable<ColumnInfo> colum
/// <inheritdoc/>
public object GetColumnValue(ColumnInfo columnInfo, T item)
{
var value = columnInfo.PropertyInfo.GetValue(item, null);
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);

var invoke = dynamicMethod.CreateDelegate(typeof(_getColumnValueDelegate)) as _getColumnValueDelegate;
var value = invoke(item);
LukasSefcik marked this conversation as resolved.
Show resolved Hide resolved

if (value != null)
{
var converter = ConverterHelper.GetConverter(columnInfo, value.GetType());
Expand Down
24 changes: 23 additions & 1 deletion src/Query/DbSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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 @@ -29,6 +31,7 @@ public class DbSet<T> : IDbSet<T>
private HashSet<T> _editedItems = new HashSet<T>();
private HashSet<T> _deletedItems = new HashSet<T>();
private readonly TableInfo _tableInfo;
private delegate void _setIdentityPrimaryKeyDelegate(T item, object id);

#endregion

Expand Down Expand Up @@ -296,7 +299,26 @@ private async Task CommitChangesAddedItemsAsync(HashSet<T> items, bool useAsync)
if (hasIdentity)
{
var id = await ExecuteScalarAsync(command, useAsync);
_tableInfo.IdentityPrimaryKey.SetValue(item, id);
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 (_tableInfo.IdentityPrimaryKey.PropertyInfo.PropertyType.IsValueType)
{
ilGenerator.Emit(OpCodes.Unbox_Any, _tableInfo.IdentityPrimaryKey.PropertyInfo.PropertyType);
}

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

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

var invoke = dynamicMethod.CreateDelegate(typeof(_setIdentityPrimaryKeyDelegate)) as _setIdentityPrimaryKeyDelegate;
LukasSefcik marked this conversation as resolved.
Show resolved Hide resolved
invoke(item, id);
}
else
{
Expand Down