sharding/samples/Samples.AbpSharding/AbstractShardingAbpDbContex...

147 lines
5.0 KiB
C#
Raw Normal View History

2021-10-22 17:26:02 +08:00
using Microsoft.EntityFrameworkCore;
using ShardingCore.Extensions;
using ShardingCore.Sharding.Abstractions;
using System;
2021-10-22 23:19:43 +08:00
using System.ComponentModel.DataAnnotations.Schema;
2021-10-22 17:26:02 +08:00
using System.Threading.Tasks;
2023-02-10 15:29:44 +08:00
using JetBrains.Annotations;
using Microsoft.EntityFrameworkCore.ChangeTracking;
2023-02-10 15:29:44 +08:00
using ShardingCore.EFCores;
using ShardingCore.Sharding.ShardingDbContextExecutors;
2021-10-22 23:19:43 +08:00
using Volo.Abp.Domain.Entities;
using Volo.Abp.Domain.Entities.Events;
2021-10-22 17:26:02 +08:00
using Volo.Abp.EntityFrameworkCore;
2021-10-22 23:19:43 +08:00
using Volo.Abp.Reflection;
2021-10-22 17:26:02 +08:00
namespace Samples.AbpSharding
{
2022-04-01 10:01:39 +08:00
public abstract class AbstractShardingAbpDbContext<TDbContext> : AbpDbContext<TDbContext>, IShardingDbContext
2021-10-22 23:19:43 +08:00
where TDbContext : DbContext
2021-10-22 17:26:02 +08:00
{
2023-02-10 15:29:44 +08:00
private bool _createExecutor = false;
2021-10-22 17:52:02 +08:00
protected AbstractShardingAbpDbContext(DbContextOptions<TDbContext> options) : base(options)
2021-10-22 17:26:02 +08:00
{
}
2021-10-22 17:26:02 +08:00
private IShardingDbContextExecutor _shardingDbContextExecutor;
public IShardingDbContextExecutor GetShardingExecutor()
{
2023-02-10 15:29:44 +08:00
if (!_createExecutor)
{
_shardingDbContextExecutor=this.DoCreateShardingDbContextExecutor();
_createExecutor = true;
}
return _shardingDbContextExecutor;
2021-10-22 17:26:02 +08:00
}
2021-10-25 15:10:57 +08:00
private IShardingDbContextExecutor DoCreateShardingDbContextExecutor()
2022-10-20 22:50:07 +08:00
{
2023-02-10 15:29:44 +08:00
var shardingDbContextExecutor = this.CreateShardingDbContextExecutor();
if (shardingDbContextExecutor != null)
{
2023-02-10 15:29:44 +08:00
shardingDbContextExecutor.EntityCreateDbContextBefore += (sender, args) =>
{
CheckAndSetShardingKeyThatSupportAutoCreate(args.Entity);
};
shardingDbContextExecutor.CreateDbContextAfter += (sender, args) =>
{
2023-02-10 15:29:44 +08:00
var dbContext = args.DbContext;
if (dbContext is AbpDbContext<TDbContext> abpDbContext && abpDbContext.LazyServiceProvider == null)
{
abpDbContext.LazyServiceProvider = this.LazyServiceProvider;
2023-02-14 18:01:55 +08:00
if (dbContext is IAbpEfCoreDbContext abpEfCoreDbContext&&this.UnitOfWorkManager.Current!=null)
2023-02-10 15:29:44 +08:00
{
abpEfCoreDbContext.Initialize(
new AbpEfCoreDbContextInitializationContext(
this.UnitOfWorkManager.Current
)
);
}
}
};
}
return shardingDbContextExecutor;
2022-10-20 22:50:07 +08:00
}
2021-10-22 17:26:02 +08:00
2021-10-22 23:19:43 +08:00
private void CheckAndSetShardingKeyThatSupportAutoCreate<TEntity>(TEntity entity) where TEntity : class
{
if (entity is IShardingKeyIsGuId)
{
if (entity is IEntity<Guid> guidEntity)
{
if (guidEntity.Id != default)
{
return;
}
var idProperty = entity.GetObjectProperty(nameof(IEntity<Guid>.Id));
2021-10-22 23:19:43 +08:00
var dbGeneratedAttr = ReflectionHelper
.GetSingleAttributeOrDefault<DatabaseGeneratedAttribute>(
idProperty
);
if (dbGeneratedAttr != null && dbGeneratedAttr.DatabaseGeneratedOption != DatabaseGeneratedOption.None)
{
return;
}
EntityHelper.TrySetId(
guidEntity,
() => GuidGenerator.Create(),
true
);
}
2021-10-25 15:10:57 +08:00
}
else if (entity is IShardingKeyIsCreationTime)
2021-10-22 23:19:43 +08:00
{
AuditPropertySetter?.SetCreationProperties(entity);
}
}
// /// <summary>
// /// abp 5.x+ 如果存在并发字段那么需要添加这段代码
// /// </summary>
// protected override void HandlePropertiesBeforeSave()
// {
// if (GetShardingExecutor() == null)
// {
// base.HandlePropertiesBeforeSave();
// }
// }
// /// <summary>
// /// abp 4.x+ 如果存在并发字段那么需要添加这段代码
// /// </summary>
// /// <returns></returns>
//
// protected override void ApplyAbpConcepts(EntityEntry entry, EntityChangeReport changeReport)
// {
// if (GetShardingExecutor() == null)
// {
// base.ApplyAbpConcepts(entry, changeReport);
// }
// }
2021-10-22 17:26:02 +08:00
public override void Dispose()
{
2022-12-23 09:10:01 +08:00
_shardingDbContextExecutor?.Dispose();
base.Dispose();
2021-10-22 17:26:02 +08:00
}
public override async ValueTask DisposeAsync()
{
2022-12-23 09:10:01 +08:00
if (_shardingDbContextExecutor != null)
2021-10-22 17:26:02 +08:00
{
await _shardingDbContextExecutor.DisposeAsync();
}
2022-12-23 09:10:01 +08:00
await base.DisposeAsync();
2021-10-22 17:26:02 +08:00
}
}
}