可以不看下面内容,直接看这:https://www.cnblogs.com/MindSharing/p/11283914.html

================================

ABP官方数据过滤的地址:https://aspnetboilerplate.com/Pages/Documents/Data-Filters

中文可以看这个:https://aspnetboilerplate.com/Pages/Documents/Data-Filters

看完后最大问题就是,自定义的数据过滤和UOW中事务是在一起的

using (CurrentUnitOfWork.EnableFilter("PersonFilter"))
{
using(CurrentUnitOfWork.SetFilterParameter("PersonFilter", "personId", 42))
{
var phones = _phoneRepository.GetAllList();
//...
}
}

总不可能每次我写服务的时候,都在方法里面写一串这个代码吧,那这样也没方便多少

所以可以考虑使用我方法来给过滤器全局设置一个值。

我的应用场景是这样的,我的项目登录的时候会让用户选择一个专业,所有数据都是这个专业下的,什么建筑啊,结构啊

为了实现这个筛选,先还是需要建立过滤器,在Core项目添加一个接口

    public interface IHasMajor
{
string Major { get; set; }
}

 在EF项目中的DbContext.cs 中注册 过滤器

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); modelBuilder.Filter("MajorFilter", (IHasMajor entity, string major) => entity.Major == major, ""); }

 在你需要筛选专业的实体里面实现这个接口

  [Table("DictionaryInfo")]
public class DictionaryInfo : FullAuditedEntity, IHasMajor
{
public virtual int? ParentId { get; set; } public virtual string Value { get; set; } public virtual string Description { get; set; } public virtual DictionaryInfo ParentDictionaryInfo { get; set; } public virtual ICollection<DictionaryInfo> ChildDictionaryInfos { get; set; } public virtual string Major { get; set; }
}

  好了下面就是关键的了,传统做法,我们就是在服务里面设置数据过滤的参数

        public async Task<GetDictionaryInfoOutput> GetAllDictionary()
{
using (CurrentUnitOfWork.SetFilterParameter(SysConsts.DataFilter.MajorFilter, SysConsts.DataFilter.MajorParameter, AbpSession.GetMajor()))
{
List<DictionaryInfo> dictionaries = await _dictionaryInfoRepository.GetAllListAsync();
var result = new GetDictionaryInfoOutput
{
Dictionary = dictionaries.MapTo<List<DictionaryInfoDto>>()
};
return result;
} }

  这种方式感觉很不优雅,改用拦截器来自动为每个服务注入 过滤器参数,

ABP 官方对拦截器他也给了个例子,可以看这个

https://www.codeproject.com/Articles/1080517/Aspect-Oriented-Programming-using-Interceptors-wit

添加个一个拦截器,在应用层

    public class FilterInterceptor : IInterceptor
{
private readonly IAbpSession _abpSession; private readonly IUnitOfWork _unitOfWork; private readonly IUnitOfWorkManager _unitOfWorkManager; public FilterInterceptor(
IUnitOfWork unitOfWork
, IAbpSession abpSession
, IUnitOfWorkManager unitOfWorkManager)
{
Logger = NullLogger.Instance;
_unitOfWork = unitOfWork;
_abpSession = abpSession;
_unitOfWorkManager = unitOfWorkManager;
} public ILogger Logger { get; set; } public void Intercept(IInvocation invocation)
{
string major = _abpSession.GetMajor(); _unitOfWorkManager.Current.SetFilterParameter(SysConsts.DataFilter.MajorFilter, SysConsts.DataFilter.MajorParameter, major); //Executing the actual method
invocation.Proceed();
} }

  

这里面有一个主意地方,拦截器请注入IUnitOfWorkManager ,而不是注入IUnitOfWork ,后者设置参数了,没效果

然后再注册拦截器,在应用层添加一个文件

 public static class InterceptorRegistrar
{
public static void Initialize(IKernel kernel)
{
kernel.ComponentRegistered += Kernel_ComponentRegistered;
} private static void Kernel_ComponentRegistered(string key, IHandler handler)
{
if (typeof(IApplicationService).IsAssignableFrom(handler.ComponentModel.Implementation))
{
handler.ComponentModel.Interceptors.Add(new InterceptorReference(typeof(FilterInterceptor)));
}
}
}

  在应用层的ApplicationModule.cs 文件里面的PreInitialize方法里面

public override void PreInitialize()
{
InterceptorRegistrar.Initialize(IocManager.IocContainer.Kernel);
}

  好这样就注册完了,可以试试服务层每个方法都能自动判断专业了,把服务里面手动设置筛选参数的地方都可以去掉了

     public async Task<GetDictionaryInfoOutput> GetAllDictionary()
{
//using (CurrentUnitOfWork.SetFilterParameter(SysConsts.DataFilter.MajorFilter, SysConsts.DataFilter.MajorParameter, AbpSession.GetMajor()))
//{
List<DictionaryInfo> dictionaries = await _dictionaryInfoRepository.GetAllListAsync();
var result = new GetDictionaryInfoOutput
{
Dictionary = dictionaries.MapTo<List<DictionaryInfoDto>>()
};
return result;
//} }

  

专业的值是存在session里面的,如何把自己想要的值存到session 里面,可以参照这篇文章

http://www.jianshu.com/p/930c10287e2a

最新文章

  1. Go build constraints
  2. Enterprise Solution 开源项目资源汇总 Visual Studio Online 源代码托管 企业管理软件开发框架
  3. OC与Swift单例
  4. ExtJS自制表格Grid分页条
  5. 复制选中的listbox内容
  6. C Shell 中的特殊变量
  7. MIT Scheme 的基本使用
  8. Python 类型
  9. jQuery.merge 源码阅读
  10. Got a packet bigger than &#39;max_allowed_packet&#39; bytes With statement Mysql终端数据同步不成功解决办法
  11. PRINCE2的好处是什么?使用PRINCE2受益非浅
  12. LeetCode 322. Coin Change
  13. Unity 游戏框架搭建 (二十) 更安全的对象池
  14. 直观理解神经网络最后一层全连接+Softmax
  15. LeetCode算法题-Find Mode in Binary Search Tree(Java实现)
  16. Win10系列:C#应用控件基础23
  17. [PHP]算法-归并排序的PHP实现
  18. Python缩进与if语句 空格的魅力
  19. python3 验证码图片切割
  20. 14-HTML-CSS案例

热门文章

  1. Spring Boot 2.x (十八):邮件服务一文打尽
  2. Go语言设计模式汇总
  3. python函数知识二 动态参数、函数的注释、名称空间、函数的嵌套、global,nonlocal
  4. 默认文档接卸--手机web app开发笔记(二)
  5. Spring cloud搭建Eureka高可用注册中心
  6. “朕赐给你,才是你的;朕不给,你不能抢”--custome role在Azure权限管理中的简单实践
  7. tomcat启动成功但是没有监听8080端口
  8. JDK(Windows)
  9. Java中Random随机数
  10. 前端js性能优化的要点