星空网 > 软件开发 > ASP.net

[Architect] Abp 框架原理解析 DynamicFilters

本节目录

  • 介绍
  • 定义Filter
  • 设置Filter

 

这是Abp中多租户、软删除、激活\禁用等如此方便的原因

Install-Package EntityFramework.DynamicFilters

 

定义数据

  class DemoDb : DbContext  {    public DemoDb() : base("Default")    {    }    public IDbSet<Blog> Blogs { get; set; }  }  interface ISoftDelete  {    bool IsDeleted { get; set; }  }  class Blog : ISoftDelete  {    public int Id { get; set; }    public string Name { get; set; }    public bool IsDeleted { get; set; }  }

 

初始化数据

    static void Main(string[] args)    {      using (var db = new DemoDb())      {        for (int i = 0; i < 5; i++)        {          db.Blogs.Add(new Blog { Name = i.ToString() });        }        for (int i = 0; i < 5; i++)        {          db.Blogs.Add(new Blog { Name = i.ToString(), IsDeleted = true });        }        db.SaveChanges();      }      Console.WriteLine("Done");      Console.ReadLine();    }

 

查询数据

    private static void Query()    {      using (var db = new DemoDb())      {        Console.WriteLine(db.Blogs.Count());      }    }

 

 

 

定义Filter

    protected override void OnModelCreating(DbModelBuilder modelBuilder)    {      //以下几种效果均一样      modelBuilder.Filter("IsDeleted", (ISoftDelete entity) => entity.IsDeleted, false);      //modelBuilder.Filter("IsDeleted", (ISoftDelete entity) => entity.IsDeleted == false);      //modelBuilder.Filter("IsDeleted", (Blog entity) => entity.IsDeleted, () =>      //{      //  //todo other      //  return false;      //});      //多参数情况      modelBuilder.Filter("BlogFilter",          (Blog b, int id, bool isDeleted) => (b.Id == id) && (b.IsDeleted == isDeleted),          () => 1,          () => false);    }

 

设置Filter

修改参数值

    private static void Query()    {      using (var db = new DemoDb())      {        db.SetFilterScopedParameterValue("IsDeleted", true);        db.SetFilterScopedParameterValue("BlogFilter", "id", 2);        Console.WriteLine(db.Blogs.Count());      }    }

 

启用/禁用过滤

禁用代码:

context.DisableFilter("IsDeleted");

context.DisableAllFilter();

modelBuilder.DisableFilterGlobally("IsDeleted");

 

启用代码:

context.EnableFilter();

context.EnableAllFilter();

 

 

参考:

https://github.com/jcachat/EntityFramework.DynamicFilters

 

本文地址:http://www.cnblogs.com/neverc/p/5258184.html




原标题:[Architect] Abp 框架原理解析 DynamicFilters

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

谷歌google广告:https://www.goluckyvip.com/tag/31794.html
谷歌google竞价:https://www.goluckyvip.com/tag/31795.html
谷歌google推广:https://www.goluckyvip.com/tag/31796.html
谷歌google引流:https://www.goluckyvip.com/tag/31797.html
谷歌google营销:https://www.goluckyvip.com/tag/31798.html
谷歌MING先生:https://www.goluckyvip.com/tag/31799.html
泰国签证有出生地 泰国出生入籍护照:https://www.vstour.cn/a/411245.html
宠物梳专利查询分析:https://www.kjdsnews.com/a/1842293.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流