Synchronization code

This commit is contained in:
sunkaixuan 2023-09-11 01:24:49 +08:00
parent 929a0605a4
commit 612ee9e794
5 changed files with 96 additions and 1 deletions

View File

@ -59,6 +59,23 @@ namespace SqlSugar
result.NavContext = UpdateNavProvider.NavContext;
return result;
}
public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions=null)
{
UpdateNavMethodInfo result = new UpdateNavMethodInfo();
result.Context = UpdateNavProvider._Context;
var entityInfo = result.Context.EntityMaintenance.GetEntityInfo<T>();
Type properyItemType;
bool isList;
Expression exp =UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType,out isList);
var method = this.GetType().GetMyMethod("Include", 2,isList)
.MakeGenericMethod(properyItemType);
var obj = method.Invoke(this, new object[] { exp, updateNavOptions });
result.MethodInfos = obj;
return result;
}
}
public class UpdateNavTask<Root, T> where T : class, new() where Root : class, new()
{

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class UpdateNavMethodInfo
{
internal object MethodInfos { get; set; }
internal SqlSugarProvider Context { get; set; }
public UpdateNavMethodInfo IncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null)
{
var type = MethodInfos.GetType().GetGenericArguments()[0];
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);
Type properyItemType;
bool isList;
Expression exp = UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType, out isList);
var method = this.MethodInfos.GetType().GetMyMethod("Include", 2, isList)
.MakeGenericMethod(properyItemType);
var obj = method.Invoke(this.MethodInfos, new object[] { exp, updateNavOptions });
this.MethodInfos = obj;
return this;
}
public UpdateNavMethodInfo ThenIncludeByNameString(string navMemberName, UpdateNavOptions updateNavOptions = null)
{
var type = MethodInfos.GetType().GetGenericArguments()[0];
var entityInfo = this.Context.EntityMaintenance.GetEntityInfo(type);
Type properyItemType;
bool isList;
Expression exp = UtilMethods.GetIncludeExpression(navMemberName, entityInfo, out properyItemType, out isList);
var method = this.MethodInfos.GetType().GetMyMethod("ThenInclude", 2, isList)
.MakeGenericMethod(properyItemType);
var obj = method.Invoke(this.MethodInfos, new object[] { exp, updateNavOptions });
this.MethodInfos = obj;
return this;
}
public async Task<bool> ExecuteCommandAsync()
{
if (Context == null) return false;
var result = MethodInfos.GetType().GetMethod("ExecuteCommandAsync").Invoke(MethodInfos, new object[] { });
return await (Task<bool>)result;
}
public bool ExecuteCommand()
{
if (Context == null) return false;
var result = MethodInfos.GetType().GetMethod("ExecuteCommand").Invoke(MethodInfos, new object[] { });
return (bool)result;
}
}
}

View File

@ -158,6 +158,7 @@
<Compile Include="Abstract\UpdateProvider\UpdateMethodInfo.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateableHelper.cs" />
<Compile Include="Abstract\UpdateProvider\SplitTableUpdateByObjectProvider.cs" />
<Compile Include="Abstract\UpdateProvider\UpdateNavMethodInfo.cs" />
<Compile Include="Entities\DiscriminatorObject .cs" />
<Compile Include="Entities\PropertyMetadata.cs" />
<Compile Include="Entities\DbFastestProperties.cs" />

View File

@ -32,6 +32,14 @@ namespace SqlSugar
it.GetParameters().Length == argCount&&
it.GetParameters().First().ParameterType==parameterType);
}
public static MethodInfo GetMyMethod(this Type type, string name, int argCount, bool isList)
{
var methods= type.GetMethods().Where(it => it.Name == name).Where(it =>
it.GetParameters().Length == argCount&&
it.GetParameters()[0].ToString().Contains("List`") == isList).ToList();
return methods.First();
}
public static MethodInfo GetMyMethod(this Type type, string name, int argCount, Type parameterType,Type parameterType2)
{
return type.GetMethods().Where(it=>it.Name == name).FirstOrDefault(it =>

View File

@ -18,7 +18,22 @@ namespace SqlSugar
{
public class UtilMethods
{
internal static Expression GetIncludeExpression(string navMemberName, EntityInfo entityInfo, out Type properyItemType,out bool isList)
{
var navInfo = entityInfo.Columns.Where(it => it.Navigat != null && it.PropertyName.EqualCase(navMemberName)).FirstOrDefault();
var properyType = navInfo.PropertyInfo.PropertyType;
properyItemType = properyType;
if (properyType.FullName.IsCollectionsList())
{
properyItemType = properyType.GetGenericArguments()[0];
isList = true;
}
else
{
isList = false;
}
return ExpressionBuilderHelper.CreateExpressionSelectField(entityInfo.Type, navInfo.PropertyName, properyType);
}
public static string RemoveEqualOne(string value)
{
value = value.TrimEnd(' ').TrimEnd('1').TrimEnd('=');