添加额外便捷扩展方法,还未实现

This commit is contained in:
xuejmnet 2022-01-31 00:27:22 +08:00
parent ed64badf90
commit b9d7e88408
8 changed files with 152 additions and 43 deletions

View File

@ -7,9 +7,12 @@ using ShardingCore.Sharding.ShardingExecutors.Abstractions;
namespace ShardingCore.Core.NotSupportShardingProviders
{
[Obsolete("plz use NotSupport() eg. dbcontext.Set<User>().NotSupport().Where(...).ToList()")]
public interface INotSupportShardingProvider
{
[Obsolete("plz use NotSupport() eg. dbcontext.Set<User>().NotSupport().Where(...).ToList()")]
void CheckNotSupportSharding(IQueryCompilerContext queryCompilerContext);
[Obsolete("plz use NotSupport() eg. dbcontext.Set<User>().NotSupport().Where(...).ToList()")]
bool IsNotSupportSharding(IQueryCompilerContext queryCompilerContext);
}
}

View File

@ -1,37 +0,0 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Query.Internal;
using ShardingCore.Core;
namespace ShardingCore.Extensions
{
/*
* @Author: xjm
* @Description:
* @Date: Sunday, 30 January 2022 00:12:37
* @Email: 326308290@qq.com
*/
public static class EntityFrameworkShardingQueryableExtension
{
internal static readonly MethodInfo NotSupportMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethod(nameof(NotSupport)) ?? throw new InvalidOperationException($"not found method {nameof(NotSupport)}");
public static IQueryable<TEntity> NotSupport<TEntity>(this IQueryable<TEntity> source)
{
Check.NotNull(source, nameof(source));
return
source.Provider is EntityQueryProvider
? source.Provider.CreateQuery<TEntity>(
Expression.Call(
(Expression)null,
NotSupportMethodInfo.MakeGenericMethod(typeof(TEntity)),
source.Expression))
// source.Provider.CreateQuery<TEntity>(
// (Expression) Expression.Call((Expression) null, NotSupportMethodInfo.MakeGenericMethod(typeof (TEntity)), source.Expression))
: source;
}
}
}

View File

@ -0,0 +1,123 @@
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.EntityFrameworkCore.Query.Internal;
using ShardingCore.Core;
namespace ShardingCore.Extensions.ShardingQueryableExtensions
{
/*
* @Author: xjm
* @Description:
* @Date: Sunday, 30 January 2022 00:12:37
* @Email: 326308290@qq.com
*/
public static class EntityFrameworkShardingQueryableExtension
{
internal static readonly MethodInfo NotSupportMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(NotSupport)).Single();
internal static readonly MethodInfo AsRouteMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(AsRoute)).Single();
internal static readonly MethodInfo UseConfigMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(UseConfig)).Single();
internal static readonly MethodInfo UseConnectionModeMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(UseConnectionMode)).Single();
internal static readonly MethodInfo ReadWriteSeparationMethodInfo
= typeof(EntityFrameworkShardingQueryableExtension).GetTypeInfo().GetDeclaredMethods(nameof(ReadWriteSeparation)).Single();
/// <summary>
/// 标记当前操作是不支持分片的可以自行才用union all
/// </summary>
/// <param name="source"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static IQueryable<TEntity> NotSupport<TEntity>(this IQueryable<TEntity> source)
{
Check.NotNull(source, nameof(source));
return
source.Provider is EntityQueryProvider
? source.Provider.CreateQuery<TEntity>(
Expression.Call(
(Expression)null,
NotSupportMethodInfo.MakeGenericMethod(typeof(TEntity)),
source.Expression))
: source;
}
/// <summary>
/// 开启提示路由的前提下手动指定表、手动指定数据源
/// </summary>
/// <param name="source"></param>
/// <param name="routeConfigure"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static IQueryable<TEntity> AsRoute<TEntity>(this IQueryable<TEntity> source,Action<ShardingQueryableAsRouteOptions> routeConfigure)
{
Check.NotNull(source, nameof(source));
Check.NotNull(routeConfigure, nameof(routeConfigure));
return source;
}
/// <summary>
/// 使用哪个配置多配置下有效
/// </summary>
/// <param name="source"></param>
/// <param name="configId"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static IQueryable<TEntity> UseConfig<TEntity>(this IQueryable<TEntity> source,string configId)
{
Check.NotNull(source, nameof(source));
Check.NotNull(configId, nameof(configId));
return source;
}
/// <summary>
/// 设置连接而模式
/// </summary>
/// <param name="source"></param>
/// <param name="maxQueryConnectionsLimit"></param>
/// <param name="connectionMode"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
public static IQueryable<TEntity> UseConnectionMode<TEntity>(this IQueryable<TEntity> source,int maxQueryConnectionsLimit,ConnectionModeEnum connectionMode)
{
Check.NotNull(source, nameof(source));
if (maxQueryConnectionsLimit <= 0)
throw new ArgumentException($"{nameof(UseConnectionMode)} {nameof(maxQueryConnectionsLimit)} should >=1");
return source;
}
/// <summary>
/// 走读库
/// </summary>
/// <param name="source"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static IQueryable<TEntity> ReadOnly<TEntity>(this IQueryable<TEntity> source)
{
return source.ReadWriteSeparation(true);
}
/// <summary>
/// 走写库
/// </summary>
/// <param name="source"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static IQueryable<TEntity> WriteOnly<TEntity>(this IQueryable<TEntity> source)
{
return source.ReadWriteSeparation(false);
}
/// <summary>
/// 自定义读写分离走什么库
/// </summary>
/// <param name="source"></param>
/// <param name="routeReadConnect"></param>
/// <typeparam name="TEntity"></typeparam>
/// <returns></returns>
public static IQueryable<TEntity> ReadWriteSeparation<TEntity>(this IQueryable<TEntity> source,bool routeReadConnect)
{
Check.NotNull(source, nameof(source));
return source;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using ShardingCore.Core.QueryRouteManagers;
namespace ShardingCore.Extensions.ShardingQueryableExtensions
{
/*
* @Author: xjm
* @Description:
* @Date: Monday, 31 January 2022 00:15:37
* @Email: 326308290@qq.com
*/
public class ShardingQueryableAsRouteOptions
{
private readonly Action<ShardingRouteContext> _routeConfigure;
public ShardingQueryableAsRouteOptions(Action<ShardingRouteContext> routeConfigure)
{
_routeConfigure = routeConfigure;
}
}
}

View File

@ -29,6 +29,8 @@ namespace ShardingCore.Sharding.StreamMergeEngines
var result = await base.ExecuteAsync( queryable => ((IQueryable<TEntity>)queryable).SingleOrDefaultAsync(cancellationToken), cancellationToken);
var notNullResult = result.Where(o => o != null&&o.QueryResult!=null).Select(o=>o.QueryResult).ToList();
if (notNullResult.Count==0)
throw new InvalidOperationException("Sequence on element.");
if (notNullResult.Count!=1)
throw new InvalidOperationException("Sequence contains more than one element.");

View File

@ -30,10 +30,6 @@ namespace ShardingCore.Sharding.StreamMergeEngines
var result = await base.ExecuteAsync( queryable => ((IQueryable<TEntity>)queryable).SingleOrDefaultAsync(cancellationToken), cancellationToken);
var notNullResult = result.Where(o => o != null&&o.QueryResult!=null).Select(o=>o.QueryResult).ToList();
if (notNullResult.IsEmpty())
return default;
if (notNullResult.Count > 1)
throw new InvalidOperationException("Sequence contains more than one element.");
return notNullResult.SingleOrDefault();

View File

@ -31,7 +31,7 @@ namespace ShardingCore.Sharding.ShardingExecutors.Abstractions
/// </summary>
/// <returns></returns>
bool IsQueryTrack();
[Obsolete("plz use NotSupport() eg. dbcontext.Set<User>().NotSupport().Where(...).ToList()")]
bool IsUnion();
bool IsNotSupport();
}

View File

@ -1,6 +1,6 @@
using System;
using System.Linq.Expressions;
using ShardingCore.Extensions;
using ShardingCore.Extensions.ShardingQueryableExtensions;
namespace ShardingCore.Sharding.Visitors
{