支持abp.vnext

This commit is contained in:
xuejiaming 2021-10-22 23:19:43 +08:00
parent f45f76e4e5
commit 229f6b2d8c
4 changed files with 70 additions and 3 deletions

View File

@ -6,11 +6,16 @@ using ShardingCore.Sharding.Abstractions;
using ShardingCore.Sharding.ShardingDbContextExecutors;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ShardingCore.EFCores.OptionsExtensions;
using ShardingCore.Helpers;
using ShardingCore.Utils;
using Volo.Abp.Domain.Entities;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.Reflection;
namespace Samples.AbpSharding
{
@ -77,9 +82,45 @@ namespace Samples.AbpSharding
/// <returns></returns>
public DbContext CreateGenericDbContext<TEntity>(TEntity entity) where TEntity : class
{
CheckAndSetShardingKeyThatSupportAutoCreate(entity);
return _shardingDbContextExecutor.CreateGenericDbContext(entity);
}
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.GetProperty(nameof(IEntity<Guid>.Id));
var dbGeneratedAttr = ReflectionHelper
.GetSingleAttributeOrDefault<DatabaseGeneratedAttribute>(
idProperty
);
if (dbGeneratedAttr != null && dbGeneratedAttr.DatabaseGeneratedOption != DatabaseGeneratedOption.None)
{
return;
}
EntityHelper.TrySetId(
guidEntity,
() => GuidGenerator.Create(),
true
);
}
}else if (entity is IShardingKeyIsCreationTime)
{
AuditPropertySetter?.SetCreationProperties(entity);
}
}
public override EntityEntry Add(object entity)
{

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Samples.AbpSharding
{
public interface IShardingKeyIsGuId
{
}
}

View File

@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Samples.AbpSharding
{
public interface IShardingKeyIsCreationTime
{
}
}

View File

@ -72,6 +72,10 @@ namespace ShardingCore.Extensions
return null;
}
}
public static PropertyInfo GetProperty(this object obj, string propertyName)
{
return obj.GetType().GetProperty(propertyName, _bindingFlags);
}
/// <summary>
/// 类型X是否包含某个属性
/// </summary>