为EasySharding.EFCore提供Dapper相关查询扩展
阅读原文时间:2023年07月09日阅读:2

承接上一篇博文中的中间件基本都是写入性的操作,但对于查询操作实际上是比较鸡肋的,如果单纯的查询,没有分表的情况下基本还能适应,这里为了Dapper提供了扩展

Dapper的扩展查询是需要写表名称的,所以对于开发人员来说需要写表名称,同一个业务的查询就会出现问题,每个业务里面都需要去判断业务上的表名称,不利于开发维护,易出错的问题

所以扩展了 EasySharding.EFCore.DapperExtension 让开发人员不用过于关注表名称,只需要关注那些业务表有分表就行了

例如:

#region GetDbConnection
///

/// 扩展给Dapper调用 支持分库分表 ///
///
///
///
public static ShardingDbConnection GetShardingDbConnection(this TContext context) where TContext : ShardingDbContext
{

        return (ShardingDbConnection)Activator.CreateInstance(typeof(ShardingDbConnection), context.Database.GetDbConnection(), context.ShardingInfo);

    }  
    /// <summary>  
    /// 支持分库 默认支持  
    /// </summary>  
    /// <typeparam name="TContext"></typeparam>  
    /// <param name="shardingConnection"></param>  
    /// <returns></returns>  
    public static DbConnection GetDbConnection<TContext>(this TContext context) where TContext : ShardingDbContext  
    {  
        return context.Database.GetDbConnection();  
    }  
    #endregion

    #region Query  
    /// <summary>  
    /// 格式化分表查询处理 需要分表写 分表业务加上标记 注意sql语句中其他元素冲突{0}  
    /// 如:Select \* from {TableName} as a  join TB1 on a.id=b.id  
    /// </summary>  
    /// <typeparam name="T"></typeparam>  
    /// <param name="shardingcon"></param>  
    /// <param name="sql"></param>  
    /// <returns></returns>  
    public static IEnumerable<T> QuerySharding<T>(this ShardingDbConnection shardingcon, string sql, object\[\] param = null) where T : class  
    {

        return shardingcon.\_dbConnection.Query<T>(sql.SqlSharding(shardingcon.\_ShardingInfo.GetName()), param);

    }

    public static IEnumerable<dynamic> QuerySharding(this ShardingDbConnection shardingcon, string sql, object\[\] param = null)  
    {

        return shardingcon.\_dbConnection.Query(sql.SqlSharding(shardingcon.\_ShardingInfo.GetName()), param);

    }  
    /// <summary>  
    /// 异步方法  格式化分表查询处理 需要分表写 分表业务加上标记 注意sql语句中其他元素冲突{0}  
    /// 如:Select \* from {TableName} as a  join TB1 on a.id=b.id  
    /// </summary>  
    /// <typeparam name="T"></typeparam>  
    /// <param name="shardingcon"></param>  
    /// <param name="sql"></param>  
    /// <param name="param"></param>  
    /// <returns></returns>  
    public static async Task<IEnumerable<T>> QueryShardingAsync<T>(this ShardingDbConnection shardingcon, string sql, object\[\] param = null) where T : class  
    {

        return await shardingcon.\_dbConnection.QueryAsync<T>(sql.SqlSharding(shardingcon.\_ShardingInfo.GetName()), param);

    }

    public static async Task<IEnumerable<dynamic>> QueryShardingAsync(this ShardingDbConnection shardingcon, string sql, object\[\] param = null)  
    {

        return await shardingcon.\_dbConnection.QueryAsync(sql.SqlSharding(shardingcon.\_ShardingInfo.GetName()), param);

    }  
    #endregion

扩展了对分库连接的Dapper支持方便

using var conquery = context.GetContext().GetDbConnection();
var testlst = conquery.Query("select * from Test");

        using var conquery2 = context.GetShardingContext(new ShardingInfo  
        {  
            DatabaseTagName = $"easysharding",  
            StufixTableName = $"1",  
            ConStr = $"dbstr1",  
        });  
        var conquerysharding = conquery2.GetShardingDbConnection();  
        var testlst2 = conquerysharding.QuerySharding<MySql.Test>("select \* from {Test}");

针对Sql中的表名称不需要关注具体表名称,知道业务表分表主题名称即可通过{TableName}加上花括号的方式 ,对应分库分表连接的上下文确定具体表操作。

GitHub:https://github.com/woshilangdanger/easysharding.efcore