添加过滤器模块及种子数据
This commit is contained in:
@@ -25,10 +25,11 @@ namespace Yi.Framework.Core.Sqlsugar
|
||||
|
||||
services.AddSingleton<IUnitOfWorkManager, UnitOfWorkManager>();
|
||||
|
||||
//这里替换过滤器实现
|
||||
|
||||
services.Configure<DbConnOptions>(Appsettings.appConfiguration("DbConnOptions"));
|
||||
services.AddSqlsugarServer();
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Yi.Framework.Data
|
||||
{
|
||||
public class Class1
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
//using System;
|
||||
//using System.Collections.Concurrent;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Yi.Framework.Data
|
||||
//{
|
||||
// public class DataFilter : IDataFilter
|
||||
// {
|
||||
// private readonly ConcurrentDictionary<Type, object> _filters;
|
||||
|
||||
// private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
// public DataFilter(IServiceProvider serviceProvider)
|
||||
// {
|
||||
// _serviceProvider = serviceProvider;
|
||||
// _filters = new ConcurrentDictionary<Type, object>();
|
||||
// }
|
||||
|
||||
// public IDisposable Enable<TFilter>()
|
||||
// where TFilter : class
|
||||
// {
|
||||
// return GetFilter<TFilter>().Enable();
|
||||
// }
|
||||
|
||||
// public IDisposable Disable<TFilter>()
|
||||
// where TFilter : class
|
||||
// {
|
||||
// return GetFilter<TFilter>().Disable();
|
||||
// }
|
||||
|
||||
// public bool IsEnabled<TFilter>()
|
||||
// where TFilter : class
|
||||
// {
|
||||
// return GetFilter<TFilter>().IsEnabled;
|
||||
// }
|
||||
|
||||
// private IDataFilter<TFilter> GetFilter<TFilter>()
|
||||
// where TFilter : class
|
||||
// {
|
||||
// return _filters.GetOrAdd(
|
||||
// typeof(TFilter),
|
||||
// valueFactory: (k) => _serviceProvider.GetRequiredService<IDataFilter<TFilter>>()
|
||||
// ) as IDataFilter<TFilter>;
|
||||
// }
|
||||
// }
|
||||
|
||||
// public class DataFilter<TFilter> : IDataFilter<TFilter>
|
||||
// where TFilter : class
|
||||
// {
|
||||
// public bool IsEnabled
|
||||
// {
|
||||
// get
|
||||
// {
|
||||
// EnsureInitialized();
|
||||
// return _filter.Value.IsEnabled;
|
||||
// }
|
||||
// }
|
||||
|
||||
// private readonly AbpDataFilterOptions _options;
|
||||
|
||||
// private readonly AsyncLocal<DataFilterState> _filter;
|
||||
|
||||
// public DataFilter(IOptions<AbpDataFilterOptions> options)
|
||||
// {
|
||||
// _options = options.Value;
|
||||
// _filter = new AsyncLocal<DataFilterState>();
|
||||
// }
|
||||
|
||||
// public IDisposable Enable()
|
||||
// {
|
||||
// if (IsEnabled)
|
||||
// {
|
||||
// return NullDisposable.Instance;
|
||||
// }
|
||||
|
||||
// _filter.Value.IsEnabled = true;
|
||||
|
||||
// return new DisposeAction(() => Disable());
|
||||
// }
|
||||
|
||||
// public IDisposable Disable()
|
||||
// {
|
||||
// if (!IsEnabled)
|
||||
// {
|
||||
// return NullDisposable.Instance;
|
||||
// }
|
||||
|
||||
// _filter.Value.IsEnabled = false;
|
||||
|
||||
// return new DisposeAction(() => Enable());
|
||||
// }
|
||||
|
||||
// private void EnsureInitialized()
|
||||
// {
|
||||
// if (_filter.Value != null)
|
||||
// {
|
||||
// return;
|
||||
// }
|
||||
|
||||
// _filter.Value = _options.DefaultStates.GetOrDefault(typeof(TFilter))?.Clone() ?? new DataFilterState(true);
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
@@ -0,0 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Ddd.Repositories;
|
||||
|
||||
namespace Yi.Framework.Data.DataSeeds
|
||||
{
|
||||
public abstract class AbstractDataSeed<TEntity> : IDataSeed<TEntity>
|
||||
{
|
||||
private readonly IRepository<TEntity> _repository;
|
||||
public AbstractDataSeed(IRepository<TEntity> repository)
|
||||
{
|
||||
_repository = repository;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 简单种子数据,重写该方法即可
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public abstract List<TEntity> GetSeedData();
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 复杂数据,重写该方法即可
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async virtual Task<bool> DataHandlerAsync()
|
||||
{
|
||||
return await _repository.InsertRangeAsync(GetSeedData());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 这个用来处理判断是否数据库还存在数据
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public virtual bool IsInvoker()
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 完全自定义数据,处理该方法即可
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async virtual Task<bool> InvokerAsync()
|
||||
{
|
||||
bool res = true;
|
||||
if (IsInvoker())
|
||||
{
|
||||
return await DataHandlerAsync();
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Data.DataSeeds
|
||||
{
|
||||
public interface IDataSeed<TEntity>
|
||||
{
|
||||
bool IsInvoker();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Data.Entities
|
||||
{
|
||||
public interface IMultiTenant
|
||||
{
|
||||
public Guid TenantId { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Data.Entities
|
||||
{
|
||||
public interface ISoftDelete
|
||||
{
|
||||
public bool IsDeleted { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Data.Filters
|
||||
{
|
||||
public class DefaultDataFilter : IDataFilter
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
public DefaultDataFilter(IServiceProvider serviceProvider)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public IDisposable Disable<TFilter>() where TFilter : class
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public IDisposable Enable<TFilter>() where TFilter : class
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public bool IsEnabled<TFilter>() where TFilter : class
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public void RemoveFilter<TFilter>() where TFilter : class
|
||||
{
|
||||
}
|
||||
public void RemoveAndBackup<TFilter>() where TFilter : class
|
||||
{
|
||||
}
|
||||
|
||||
public void AddFilter<TFilter>(Expression<Func<TFilter, bool>> expression) where TFilter : class
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Yi.Framework.Data.Filters
|
||||
{
|
||||
public interface IDataFilter
|
||||
{
|
||||
IDisposable Enable<TFilter>() where TFilter :class;
|
||||
|
||||
IDisposable Disable<TFilter>() where TFilter : class;
|
||||
|
||||
bool IsEnabled<TFilter>() where TFilter : class;
|
||||
|
||||
void AddFilter<TFilter>(Expression<Func<TFilter, bool>> expression) where TFilter : class;
|
||||
|
||||
void RemoveFilter<TFilter>() where TFilter : class;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.Linq;
|
||||
//using System.Text;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
//namespace Yi.Framework.Data
|
||||
//{
|
||||
// public interface IDataFilter<TFilter>
|
||||
// where TFilter : class
|
||||
// {
|
||||
// IDisposable Enable();
|
||||
|
||||
// IDisposable Disable();
|
||||
|
||||
// bool IsEnabled { get; }
|
||||
// }
|
||||
|
||||
// public interface IDataFilter
|
||||
// {
|
||||
// IDisposable Enable<TFilter>()
|
||||
// where TFilter : class;
|
||||
|
||||
// IDisposable Disable<TFilter>()
|
||||
// where TFilter : class;
|
||||
|
||||
// bool IsEnabled<TFilter>()
|
||||
// where TFilter : class;
|
||||
// }
|
||||
|
||||
//}
|
||||
@@ -0,0 +1,31 @@
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using StartupModules;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.Data.Entities;
|
||||
using Yi.Framework.Data.Filters;
|
||||
|
||||
namespace Yi.Framework.Data
|
||||
{
|
||||
public class YiFrameworkDataModule : IStartupModule
|
||||
{
|
||||
public void Configure(IApplicationBuilder app, ConfigureMiddlewareContext context)
|
||||
{
|
||||
var dataFilter = app.ApplicationServices.GetRequiredService<IDataFilter>();
|
||||
//内置多租户与软删除过滤
|
||||
dataFilter.AddFilter<ISoftDelete>(u => u.IsDeleted == false);
|
||||
|
||||
//租户id从租户管理中获取
|
||||
dataFilter.AddFilter<IMultiTenant>(u => u.TenantId == Guid.Empty);
|
||||
}
|
||||
|
||||
public void ConfigureServices(IServiceCollection services, ConfigureServicesContext context)
|
||||
{
|
||||
services.AddTransient<IDataFilter, DefaultDataFilter>();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user