惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Engineering at Meta
Engineering at Meta
月光博客
月光博客
WordPress大学
WordPress大学
C
Cisco Blogs
Recent Commits to openclaw:main
Recent Commits to openclaw:main
博客园 - 【当耐特】
大猫的无限游戏
大猫的无限游戏
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
The Cloudflare Blog
有赞技术团队
有赞技术团队
Microsoft Azure Blog
Microsoft Azure Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
小众软件
小众软件
H
Heimdal Security Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
W
WeLiveSecurity
量子位
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
F
Fortinet All Blogs
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
P
Privacy & Cybersecurity Law Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
L
Lohrmann on Cybersecurity
C
CERT Recently Published Vulnerability Notes
L
LINUX DO - 热门话题
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
V2EX - 技术
V2EX - 技术
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
N
News | PayPal Newsroom
S
Security @ Cisco Blogs
宝玉的分享
宝玉的分享
Project Zero
Project Zero
The Hacker News
The Hacker News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
PCI Perspectives
PCI Perspectives
G
GRAHAM CLULEY
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Y
Y Combinator Blog
N
Netflix TechBlog - Medium
S
Schneier on Security
Application and Cybersecurity Blog
Application and Cybersecurity Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
博客园 - 聂微东

博客园 - 开发手游啦啦啦

用TypeScript开发爬虫程序 ABP导航源码分析 ABP的语言切换 AngularJs自定义指令详解(10) - 执行次序 AngularJs自定义指令详解(9) - terminal AngularJs自定义指令详解(8) - priority AngularJs自定义指令详解(7) - multiElement AngularJs自定义指令详解(6) - controller、require AngularJs自定义指令详解(5) - link AngularJs自定义指令详解(4) - transclude AngularJs自定义指令详解(3) - scope AngularJs自定义指令详解(2) - template AngularJs自定义指令详解(1) - restrict ABP的Zero Sample ABP的工作单元 ABP的事件总线和领域事件(EventBus & Domain Events) 初入Cocos2d-x 2.2 quick-cocos2d-x之testlua之VisibleRect.lua quick-cocos2d-x之testlua之mainMenu.lua
ABP的数据过滤器(Data Filters)
开发手游啦啦啦 · 2015-06-22 · via 博客园 - 开发手游啦啦啦

我们在数据库开发中,一般会运用软删除 (soft delete)模式 ,即不直接从数据库删除数据 ,而是标记这笔数据为已删除。因此 ,如果实体被软删除了,那么它就应该不会在应用程序中被检索到。要达到这种效果 ,我们需要在每次检索实体的查询语句上添加 SQL的 Where条件 IsDeleted = false 。这是个乏味的工作 。但它是个容易被忘掉的事情。因此 ,我们应该要有个自动的机制来处理这些问题 。

ABP 提供数据过滤器 (Data filters),它使用 自动化的,基于规则的过滤查询。

ABP已做好的过滤器

ISoftDelete

publicclass Person : Entity, ISoftDelete
{
    publicvirtualstring Name { get; set; }

    publicvirtualbool IsDeleted { get; set; }
}

如上面的Person类,实现了ISoftDelete接口,当我们使用IRepository.Delete方法删除一个Person时,该Person并不真的从数据库删除,仅仅是IsDeleted属性被设置为true。

publicclass MyService
{
    privatereadonly IRepository<Person> _personRepository;

    publicMyService(IRepository<Person> personRepository)
    {
        _personRepository = personRepository;
    }

    public List<Person> GetPeople()
    {
        return _personRepository.GetAllList();
    }
}

GetPeople method only gets Person entities which has IsDeleted = false (not deleted). All repository methods and also navigation properties properly works. We could add some other Where conditions, joins.. etc. It will automatically add IsDeleted = false condition properly to the generated SQL query.

如上面代码,如果某个Person已经被软删除,那么使用IRepository获取所有Person数据时,过滤器会自动过滤掉已经软删除的Person,也就是说,GetAll不是获取实际上数据库还存有的所用Person数据,而是排除了之前被软删除的数据了。

A side note: If you implement IDeletionAudited (which extends ISoftDelete) then deletion time and deleter user id are also automatically set by ASP.NET Boilerplate.

IMustHaveTenant

publicclass Product : IMustHaveTenant
{
    publicvirtualint TenantId { get; set; }
        
    publicvirtualstring Name { get; set; }
}

如果你创建了一个多租户的应用程序(储存所有租户的数据于单一一个数据库中),你肯定不会希望某个租户看到其他租户的资料,此时你可以实现IMustHaveTenant接口。

ABP会使用IABPSession来取得当前TenantId并且自动地替当前租户进行过滤查询处理。

If current user is not logged in to the system or current user is a host user (Host user is an upper level user that can manage tenants and tenant datas), ASP.NET Boilerplate automatically disables IMustHaveTenant filter. Thus, all data of all tenant's can be retrieved to the application. Notice that this is not about security, you should always authorize sensitive data.

IMayHaveTenant

publicclass Product : IMayHaveTenant
{
    publicvirtualint? TenantId { get; set; }
        
    publicvirtualstring Name { get; set; }
}

If an entity class shared by tenants and the host (that means an entity object may be owned by a tenant or the host), you can use IMayHaveTenant filter.

IMayHaveTenant接口定义了TenantId,但是注意它是int?类型,可为空。

null value means this is a host entity, a non-null value means this entity owned by a tenant which's Id is the TenantId. ASP.NET Boilerplate uses IAbpSession to get current TenantId. IMayHaveTenant filter is not common as much as IMustHaveTenant. But you may need it for common structures used by host and tenants.

禁用过滤器

var people1 = _personRepository.GetAllList();

using (_unitOfWorkManager.Current.DisableFilter(AbpDataFilters.SoftDelete))
{
    var people2 = _personRepository.GetAllList();                
}

var people3 = _personRepository.GetAllList();

启用过滤器

也就是使用_unitOfWorkManager.Current.EnableFilter方法

设定过滤器参数

CurrentUnitOfWork.SetFilterParameter("PersonFilter", "personId", 42);
CurrentUnitOfWork.SetFilterParameter(AbpDataFilters.MayHaveTenant, AbpDataFilters.Parameters.TenantId, 42);

自定义过滤器

首先定义一个接口:

publicinterface IHasPerson
{
    int PersonId { get; set; }
}

实现接口:

publicclass Phone : Entity, IHasPerson
{
    [ForeignKey("PersonId")]
    publicvirtual Person Person { get; set; }
    publicvirtualint PersonId { get; set; }

    publicvirtualstring Number { get; set; }
}

重写DbContext类中的OnModelCreating 方法(用的EntityFramework.DynamicFilters,参考https://github.com/jcachat/EntityFramework.DynamicFilters):

protectedoverridevoidOnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Filter("PersonFilter", (IHasPerson entity, int personId) => entity.PersonId == personId, 0);
}

"PersonFilter" is the unique name of the filter here. Second parameter defines filter interface and personId filter parameter (not needed if filter is not parametric), last parameter is the default value of the personId.

最后,我们要把过滤器注册到ABP的工作单元系统中,以下代码需要写在模块的Preinitialize方法里:

Configuration.UnitOfWork.RegisterFilter("PersonFilter", false);

First parameter is same unique name we defined before. Second parameter indicates whether this filter is enabled or disabled by default. After declaring such a parametric filter, we can use it by supplying it's value on runtime.

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