mirror of
https://gitee.com/dotnetchina/SqlSugar.git
synced 2025-05-15 20:49:31 +08:00
Synchronization code
This commit is contained in:
parent
697c3ed35d
commit
eb77c3eb49
@ -9,6 +9,7 @@ namespace SqlSugar
|
|||||||
public interface ISimpleClient<T> where T : class, new()
|
public interface ISimpleClient<T> where T : class, new()
|
||||||
{
|
{
|
||||||
SimpleClient<T> CopyNew();
|
SimpleClient<T> CopyNew();
|
||||||
|
RepositoryType CopyNew<RepositoryType>(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository;
|
||||||
RepositoryType CopyNew<RepositoryType>() where RepositoryType : ISugarRepository;
|
RepositoryType CopyNew<RepositoryType>() where RepositoryType : ISugarRepository;
|
||||||
SimpleClient<ChangeType> Change<ChangeType>() where ChangeType : class, new();
|
SimpleClient<ChangeType> Change<ChangeType>() where ChangeType : class, new();
|
||||||
RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository ;
|
RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository ;
|
||||||
|
@ -77,6 +77,78 @@ namespace SqlSugar
|
|||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
public RepositoryType CopyNew<RepositoryType>(IServiceProvider serviceProvider) where RepositoryType : ISugarRepository
|
||||||
|
{
|
||||||
|
var instance = handleDependencies(typeof(RepositoryType), serviceProvider);
|
||||||
|
return (RepositoryType)instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private object handleDependencies(Type type, IServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
ConstructorInfo constructorInfo = null;
|
||||||
|
var newInstanceType = type;
|
||||||
|
if (type.IsInterface && IsAssignableToBaseRepository(type))
|
||||||
|
{
|
||||||
|
var dependencyInstanceType = serviceProvider.GetService(type)?.GetType();
|
||||||
|
newInstanceType = dependencyInstanceType;
|
||||||
|
constructorInfo = dependencyInstanceType.GetConstructors().FirstOrDefault();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
constructorInfo = type.GetConstructors().FirstOrDefault();
|
||||||
|
}
|
||||||
|
var parameters = constructorInfo?.GetParameters();
|
||||||
|
if (parameters == null || parameters.Length == 0)
|
||||||
|
{
|
||||||
|
object dependencyInstance = serviceProvider.GetService(type);
|
||||||
|
if (dependencyInstance is ISugarRepository sugarRepository)
|
||||||
|
{
|
||||||
|
sugarRepository.Context = sugarRepository.Context.CopyNew();
|
||||||
|
return sugarRepository;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return dependencyInstance;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var conParas = new List<object>();
|
||||||
|
foreach (var parameter in parameters)
|
||||||
|
{
|
||||||
|
Type dependencyType = parameter.ParameterType;
|
||||||
|
conParas.Add(handleDependencies(dependencyType, serviceProvider));
|
||||||
|
}
|
||||||
|
|
||||||
|
object instance = null;
|
||||||
|
if (conParas != null && conParas.Count > 0)
|
||||||
|
{
|
||||||
|
instance = Activator.CreateInstance(newInstanceType, conParas.ToArray());
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
instance = Activator.CreateInstance(newInstanceType);
|
||||||
|
}
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
private bool IsAssignableToBaseRepository(Type type)
|
||||||
|
{
|
||||||
|
var baseInterfaces = type.GetInterfaces();
|
||||||
|
foreach (Type interfaceType in baseInterfaces)
|
||||||
|
{
|
||||||
|
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(ISimpleClient<>))
|
||||||
|
{
|
||||||
|
Type genericArgument = interfaceType.GetGenericArguments()[0];
|
||||||
|
Type baseRepositoryGenericType = typeof(ISimpleClient<>).MakeGenericType(genericArgument);
|
||||||
|
|
||||||
|
if (baseRepositoryGenericType.IsAssignableFrom(type))
|
||||||
|
{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
public RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository
|
public RepositoryType ChangeRepository<RepositoryType>() where RepositoryType : ISugarRepository
|
||||||
{
|
{
|
||||||
Type type = typeof(RepositoryType);
|
Type type = typeof(RepositoryType);
|
||||||
|
Loading…
Reference in New Issue
Block a user