添加部分注释

This commit is contained in:
xuejmnet 2021-12-04 14:34:45 +08:00
parent da4675d549
commit 584697fc55
5 changed files with 100 additions and 30 deletions

View File

@ -33,11 +33,22 @@ namespace ShardingCore.DIExtensions
_shardingCoreConfigBuilder = shardingCoreConfigBuilder;
}
/// <summary>
/// 替换比较表达式
/// 比较表达式用于将数据库的数据获取到内存后进行排序由于数据库排序和内存排序针对某种类型可能不一致导致结果和预期不符如guid和unique identifier
/// 默认已经将此类型的比较器已经修复如果有后续其他数据库类型和c#类型排序不一致的请自行实现 <see cref="IShardingComparer<TShardingDbContext>"/>
/// </summary>
/// <param name="newShardingComparerFactory"></param>
/// <returns></returns>
public ShardingCoreConfigEndBuilder<TShardingDbContext> ReplaceShardingComparer(Func<IServiceProvider, IShardingComparer<TShardingDbContext>> newShardingComparerFactory)
{
_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparer(newShardingComparerFactory);
return this;
}
/// <summary>
/// 是否启用读写分离
/// </summary>
protected bool UseReadWrite => _shardingCoreConfigBuilder.ShardingConfigOption.UseReadWrite;
public IServiceCollection End()
{
var services = _shardingCoreConfigBuilder.Services;
@ -53,31 +64,43 @@ namespace ShardingCore.DIExtensions
.AddSingleton<IShardingDbContextOptionsBuilderConfig<TShardingDbContext>,
ShardingDbContextOptionsBuilderConfig<TShardingDbContext>>(sp => config);
if (_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory != null)
if (_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory == null)
{
services.AddSingleton<IShardingComparer<TShardingDbContext>>(_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory);
}
else
{
services.AddSingleton<IShardingComparer<TShardingDbContext>, CSharpLanguageShardingComparer<TShardingDbContext>>();
throw new ShardingCoreConfigException($"{nameof(_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory)} is null");
}
services.AddSingleton<IShardingComparer<TShardingDbContext>>(_shardingCoreConfigBuilder.ShardingConfigOption.ReplaceShardingComparerFactory);
if (!_shardingCoreConfigBuilder.ShardingConfigOption.UseReadWrite)
if (!UseReadWrite)
{
services.AddTransient<IConnectionStringManager<TShardingDbContext>, DefaultConnectionStringManager<TShardingDbContext>>();
}
else
{
services.AddTransient<IConnectionStringManager<TShardingDbContext>, ReadWriteConnectionStringManager<TShardingDbContext>>();
RegisterReadWriteConfigure(services);
}
services.AddInternalShardingCore();
return services;
}
/// <summary>
/// 配置读写分离
/// </summary>
/// <param name="services"></param>
/// <exception cref="ShardingCoreInvalidOperationException"></exception>
private void RegisterReadWriteConfigure(IServiceCollection services)
{
services.AddSingleton<IReadWriteOptions<TShardingDbContext>, ReadWriteOptions<TShardingDbContext>>(sp =>
new ReadWriteOptions<TShardingDbContext>(
_shardingCoreConfigBuilder.ShardingConfigOption.ReadWriteDefaultPriority,
_shardingCoreConfigBuilder.ShardingConfigOption.ReadWriteDefaultEnable,
_shardingCoreConfigBuilder.ShardingConfigOption.ReadConnStringGetStrategy));
bool isLoop = false;
var readStrategyEnum = _shardingCoreConfigBuilder.ShardingConfigOption.ReadStrategyEnum;
if (_shardingCoreConfigBuilder.ShardingConfigOption.ReadStrategyEnum == ReadStrategyEnum.Loop)
if ( readStrategyEnum== ReadStrategyEnum.Loop)
{
isLoop = true;
}
@ -105,17 +128,6 @@ namespace ShardingCore.DIExtensions
services.TryAddSingleton<IShardingReadWriteManager, ShardingReadWriteManager>();
services.AddSingleton<IShardingReadWriteAccessor, ShardingReadWriteAccessor<TShardingDbContext>>();
//foreach (var dataSourceKv in dataSources)
//{
// if (dataSourceKv.Key == _shardingCoreConfigBuilder.DefaultDataSourceName)
// throw new ShardingCoreInvalidOperationException($"{nameof(AddShardingDataSource)} include default data source name:{_shardingCoreConfigBuilder.DefaultDataSourceName}");
// _shardingCoreConfigBuilder.AddShardingDataSource.Add(dataSourceKv.Key, dataSourceKv.Value);
//}
}
services.AddInternalShardingCore();
return services;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Diagnostics.CodeAnalysis;
namespace ShardingCore.Exceptions;
/*
* @Author: xjm
* @Description:
* @Date: Saturday, 04 December 2021 11:26:17
* @Email: 326308290@qq.com
*/
[ExcludeFromCodeCoverage]
public class ShardingCoreConfigException:ShardingCoreException
{
public ShardingCoreConfigException(string message) : base(message)
{
}
public ShardingCoreConfigException(string message, Exception innerException) : base(message, innerException)
{
}
}

View File

@ -5,18 +5,34 @@ using System.Text;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions;
/*
* @Author: xjm
* @Description:
* @Date: 2021/8/20 11:34:55
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
namespace ShardingCore
{
/*
* @Author: xjm
* @Description:
* @Date: 2021/8/20 11:34:55
* @Ver: 1.0
* @Email: 326308290@qq.com
*/
/// <summary>
/// 分片db context配置构造配置
/// </summary>
/// <typeparam name="TShardingDbContext"></typeparam>
public interface IShardingDbContextOptionsBuilderConfig<TShardingDbContext> where TShardingDbContext:DbContext,IShardingDbContext
{
/// <summary>
/// 如何根据connectionString 配置 DbContextOptionsBuilder
/// </summary>
/// <param name="connectionString"></param>
/// <param name="dbContextOptionsBuilder"></param>
/// <returns></returns>
DbContextOptionsBuilder UseDbContextOptionsBuilder(string connectionString, DbContextOptionsBuilder dbContextOptionsBuilder);
/// <summary>
/// 如何根据dbConnection 配置DbContextOptionsBuilder
/// </summary>
/// <param name="dbConnection"></param>
/// <param name="dbContextOptionsBuilder"></param>
/// <returns></returns>
DbContextOptionsBuilder UseDbContextOptionsBuilder(DbConnection dbConnection, DbContextOptionsBuilder dbContextOptionsBuilder);
}
}

View File

@ -3,20 +3,38 @@ using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using ShardingCore.Sharding.Abstractions;
namespace ShardingCore.Sharding.ShardingComparision.Abstractions
{
/*
* @Author: xjm
* @Description:
* @Date: Sunday, 31 October 2021 15:07:52
* @Email: 326308290@qq.com
*/
namespace ShardingCore.Sharding.ShardingComparision.Abstractions
{
/// <summary>
/// 分表内存排序比较器
/// </summary>
public interface IShardingComparer
{
/// <summary>
/// 比较 参数
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="asc"></param>
/// <returns></returns>
int Compare(IComparable a, IComparable b,bool asc);
/// <summary>
/// 创建一个比较器
/// </summary>
/// <param name="comparerType"></param>
/// <returns></returns>
object CreateComparer(Type comparerType);
}
/// <summary>
/// 泛型比较器用于依赖注入
/// </summary>
/// <typeparam name="TShardingDbContext"></typeparam>
public interface IShardingComparer<TShardingDbContext> : IShardingComparer where TShardingDbContext:DbContext,IShardingDbContext
{

View File

@ -10,6 +10,7 @@ using System.Linq;
using ShardingCore.Core.VirtualRoutes.DataSourceRoutes;
using ShardingCore.Exceptions;
using ShardingCore.Extensions;
using ShardingCore.Sharding.ShardingComparision;
using ShardingCore.Sharding.ShardingComparision.Abstractions;
namespace ShardingCore
@ -46,7 +47,8 @@ namespace ShardingCore
{
DataSourcesConfigure = dataSourcesConfigure ?? throw new ArgumentNullException(nameof(dataSourcesConfigure));
}
public Func<IServiceProvider, IShardingComparer<TShardingDbContext>> ReplaceShardingComparerFactory { get; private set; }
public Func<IServiceProvider, IShardingComparer<TShardingDbContext>> ReplaceShardingComparerFactory { get; private set; } = sp => new CSharpLanguageShardingComparer<TShardingDbContext>();
/// <summary>
/// 替换默认的比较器
/// </summary>