This commit is contained in:
xuejmnet 2021-02-03 09:53:07 +08:00
parent 690295daf8
commit b723b28d02
7 changed files with 146 additions and 7 deletions

View File

@ -132,9 +132,6 @@ Release | EF Core | .NET Standard | .NET (Core) | Sql Server | Pomelo.EntityFra
} }
} }
``` ```
- `GetAllTails`
现在数据库已存在的尾巴有哪些
`Startup.cs` 下的 `ConfigureServices(IServiceCollection services)` `Startup.cs` 下的 `ConfigureServices(IServiceCollection services)`
```c# ```c#
@ -295,6 +292,32 @@ context.Where(shardingBatchUpdateEntry.Where).Update(shardingBatchUpdateEntry.Up
# 注意事项 # 注意事项
该库的IVirtualDbContext.Set<T>使用asnotracking所以基本不支持跟踪,目前框架采用AppDomain.CurrentDomain.GetAssemblies(); 该库的IVirtualDbContext.Set<T>使用asnotracking所以基本不支持跟踪,目前框架采用AppDomain.CurrentDomain.GetAssemblies();
可能会导致程序集未被加载所以尽可能在api层加载所需要的dll 可能会导致程序集未被加载所以尽可能在api层加载所需要的dll
使用时需要注意
- 实体对象是否继承`IShardingEntity`
- 实体对象是否有`ShardingKey`
- 实体对象是否已经实现了一个虚拟路由
- startup是否已经添加虚拟路由
```c#
services.AddShardingSqlServer(o =>
{
o.ConnectionString = "";
o.AddSharding<SysUserModVirtualRoute>();
o.UseShardingCoreConfig((provider, config) =>
{
//如果是development就判断并且新建数据库如果不存在的话(ishardingentity不会被创建)
config.EnsureCreated = provider.GetService<IHostEnvironment>().IsDevelopment();
//ishardingentity表是否需要在启动时创建(如果已创建可以选择不创建)
config.CreateShardingTableOnStart = true;
});
});
```
- startup
```c#
var shardingBootstrapper = app.ApplicationServices.GetRequiredService<IShardingBootstrapper>();
shardingBootstrapper.Start();
```
# 计划 # 计划
- [提供官网如果该项目比较成功的话] - [提供官网如果该项目比较成功的话]

View File

@ -22,5 +22,23 @@ namespace ShardingCore.Test50.MySql.Domain.Entities
/// 工资 /// 工资
/// </summary> /// </summary>
public int Salary { get; set; } public int Salary { get; set; }
/// <summary>
/// 工资
/// </summary>
public long SalaryLong { get; set; }
/// <summary>
/// 工资
/// </summary>
public decimal SalaryDecimal { get; set; }
/// <summary>
/// 工资
/// </summary>
public double SalaryDouble { get; set; }
/// <summary>
/// 工资
/// </summary>
public float SalaryFloat { get; set; }
} }
} }

View File

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ShardingCore.Core.Internal.RoutingRuleEngines;
using ShardingCore.DbContexts.VirtualDbContexts; using ShardingCore.DbContexts.VirtualDbContexts;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Test50.MySql.Domain.Entities; using ShardingCore.Test50.MySql.Domain.Entities;
@ -16,10 +17,30 @@ namespace ShardingCore.Test50.MySql
public class ShardingTest public class ShardingTest
{ {
private readonly IVirtualDbContext _virtualDbContext; private readonly IVirtualDbContext _virtualDbContext;
private readonly IRoutingRuleEngineFactory _routingRuleEngineFactory;
public ShardingTest(IVirtualDbContext virtualDbContext) public ShardingTest(IVirtualDbContext virtualDbContext,IRoutingRuleEngineFactory routingRuleEngineFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_routingRuleEngineFactory = routingRuleEngineFactory;
}
[Fact]
public async Task Route_TEST()
{
var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
var routeResults1 = _routingRuleEngineFactory.Route(queryable1);
Assert.Equal(1,routeResults1.Count());
Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
var ids = new[] {"339", "124","142"};
var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
var routeResult2s = _routingRuleEngineFactory.Route(queryable2);
Assert.Equal(2,routeResult2s.Count());
Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
Assert.Equal(2,routeResult2s.SelectMany(o=>o.ReplaceTables).Count());
Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
} }
[Fact] [Fact]
@ -290,6 +311,7 @@ namespace ShardingCore.Test50.MySql
Count = g.Count(), Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary), TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary), AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary), MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary) MaxSalary = g.Max(o => o.Salary)
}).ToShardingListAsync(); }).ToShardingListAsync();
@ -297,6 +319,7 @@ namespace ShardingCore.Test50.MySql
Assert.Equal(2, group[0].Count); Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary); Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary); Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary); Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary); Assert.Equal(1140000, group[0].MaxSalary);
} }
@ -314,6 +337,7 @@ namespace ShardingCore.Test50.MySql
Count = g.Count(), Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary), TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary), AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary), MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary) MaxSalary = g.Max(o => o.Salary)
}); });
@ -321,6 +345,7 @@ namespace ShardingCore.Test50.MySql
Assert.Equal(2, group[0].Count); Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary); Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary); Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary); Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary); Assert.Equal(1140000, group[0].MaxSalary);
} }

View File

@ -99,7 +99,11 @@ namespace ShardingCore.Test50.MySql
Id = $@"{id}{dateOfMonth}", Id = $@"{id}{dateOfMonth}",
UserId = id.ToString(), UserId = id.ToString(),
DateOfMonth = int.Parse(dateOfMonth), DateOfMonth = int.Parse(dateOfMonth),
Salary = 700000+id*100*i Salary = 700000+id*100*i,
SalaryLong = 700000+id*100*i,
SalaryDecimal = (700000+id*100*i)/100m,
SalaryDouble = (700000+id*100*i)/100d,
SalaryFloat = (700000+id*100*i)/100f
}); });
tempTime=tempTime.AddMonths(1); tempTime=tempTime.AddMonths(1);
i++; i++;

View File

@ -22,5 +22,22 @@ namespace ShardingCore.Test50.Domain.Entities
/// 工资 /// 工资
/// </summary> /// </summary>
public int Salary { get; set; } public int Salary { get; set; }
/// <summary>
/// 工资
/// </summary>
public long SalaryLong { get; set; }
/// <summary>
/// 工资
/// </summary>
public decimal SalaryDecimal { get; set; }
/// <summary>
/// 工资
/// </summary>
public double SalaryDouble { get; set; }
/// <summary>
/// 工资
/// </summary>
public float SalaryFloat { get; set; }
} }
} }

View File

@ -1,5 +1,6 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using ShardingCore.Core.Internal.RoutingRuleEngines;
using ShardingCore.DbContexts.VirtualDbContexts; using ShardingCore.DbContexts.VirtualDbContexts;
using ShardingCore.Extensions; using ShardingCore.Extensions;
using ShardingCore.Test50.Domain.Entities; using ShardingCore.Test50.Domain.Entities;
@ -16,12 +17,31 @@ namespace ShardingCore.Test50
public class ShardingTest public class ShardingTest
{ {
private readonly IVirtualDbContext _virtualDbContext; private readonly IVirtualDbContext _virtualDbContext;
private readonly IRoutingRuleEngineFactory _routingRuleEngineFactory;
public ShardingTest(IVirtualDbContext virtualDbContext) public ShardingTest(IVirtualDbContext virtualDbContext,IRoutingRuleEngineFactory routingRuleEngineFactory)
{ {
_virtualDbContext = virtualDbContext; _virtualDbContext = virtualDbContext;
_routingRuleEngineFactory = routingRuleEngineFactory;
} }
[Fact]
public async Task Route_TEST()
{
var queryable1 = _virtualDbContext.Set<SysUserMod>().Where(o=>o.Id=="339");
var routeResults1 = _routingRuleEngineFactory.Route(queryable1);
Assert.Equal(1,routeResults1.Count());
Assert.Equal(1,routeResults1.FirstOrDefault().ReplaceTables.Count());
Assert.Equal("0",routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().Tail);
Assert.Equal(nameof(SysUserMod),routeResults1.FirstOrDefault().ReplaceTables.FirstOrDefault().OriginalName);
var ids = new[] {"339", "124","142"};
var queryable2= _virtualDbContext.Set<SysUserMod>().Where(o=>ids.Contains(o.Id));
var routeResult2s = _routingRuleEngineFactory.Route(queryable2);
Assert.Equal(2,routeResult2s.Count());
Assert.Equal(1,routeResult2s.FirstOrDefault().ReplaceTables.Count());
Assert.Equal(2,routeResult2s.SelectMany(o=>o.ReplaceTables).Count());
Assert.Equal(true,routeResult2s.SelectMany(o=>o.ReplaceTables).All(o=>new[]{"0","1"}.Contains(o.Tail)));
}
[Fact] [Fact]
public async Task ToList_All_Test() public async Task ToList_All_Test()
{ {
@ -290,6 +310,7 @@ namespace ShardingCore.Test50
Count = g.Count(), Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary), TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary), AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary), MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary) MaxSalary = g.Max(o => o.Salary)
}).ToShardingListAsync(); }).ToShardingListAsync();
@ -297,6 +318,33 @@ namespace ShardingCore.Test50
Assert.Equal(2, group[0].Count); Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary); Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary); Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary);
}
[Fact]
public async Task Group_API_Test()
{
var ids = new[] {"200", "300"};
var dateOfMonths = new[] {202111, 202110};
var group = await _virtualDbContext.Set<SysUserSalary>()
.Where(o => ids.Contains(o.UserId) && dateOfMonths.Contains(o.DateOfMonth))
.ShardingGroupByAsync(g => new {UId = g.UserId}, g => new
{
GroupUserId = g.Key.UId,
Count = g.Count(),
TotalSalary = g.Sum(o => o.Salary),
AvgSalary = g.Average(o => o.Salary),
AvgSalaryDecimal = g.Average(o => o.SalaryDecimal),
MinSalary = g.Min(o => o.Salary),
MaxSalary = g.Max(o => o.Salary)
});
Assert.Equal(2, group.Count);
Assert.Equal(2, group[0].Count);
Assert.Equal(2260000, group[0].TotalSalary);
Assert.Equal(1130000, group[0].AvgSalary);
Assert.Equal(11300, group[0].AvgSalaryDecimal);
Assert.Equal(1120000, group[0].MinSalary); Assert.Equal(1120000, group[0].MinSalary);
Assert.Equal(1140000, group[0].MaxSalary); Assert.Equal(1140000, group[0].MaxSalary);
} }

View File

@ -103,7 +103,11 @@ namespace ShardingCore.Test50
Id = $@"{id}{dateOfMonth}", Id = $@"{id}{dateOfMonth}",
UserId = id.ToString(), UserId = id.ToString(),
DateOfMonth = int.Parse(dateOfMonth), DateOfMonth = int.Parse(dateOfMonth),
Salary = 700000+id*100*i Salary = 700000+id*100*i,
SalaryLong = 700000+id*100*i,
SalaryDecimal = (700000+id*100*i)/100m,
SalaryDouble = (700000+id*100*i)/100d,
SalaryFloat = (700000+id*100*i)/100f
}); });
tempTime=tempTime.AddMonths(1); tempTime=tempTime.AddMonths(1);
i++; i++;