ServiceStack auto query global filter

青春壹個敷衍的年華 提交于 2019-12-25 04:44:09

问题


I'm looking at using ServiceStack's AutoQuery feature and I have some basic queries working. However I'd like to implement a global filter since I have a multi-tenanted database, e.g.,

All queries should be appended with the criteria CustomerId = Session.CustomerId

What would be the best way to do this?


回答1:


You could potentially use a custom AutoQuery base class for this to append the custom filter to each query, e.g:

public abstract class MyAutoQueryServiceBase : AutoQueryServiceBase
{
    public override object Exec<From>(IQuery<From> dto)
    {
        var q = AutoQuery.CreateQuery(dto, Request);
        var session = base.SessionAs<CustomUserSession>();
        q.And("CustomerId = {0}", session.CustomerId);

        return AutoQuery.Execute(dto, q);
    }

    public override object Exec<From, Into>(IQuery<From, Into> dto)
    {
        var q = AutoQuery.CreateQuery(dto, Request);
        var session = base.SessionAs<CustomUserSession>();
        q.And("CustomerId = {0}", session.CustomerId);

        return AutoQuery.Execute(dto, q);
    }
}

Then tell AutoQuery to use your base class instead, e.g:

Plugins.Add(new AutoQueryFeature { 
    AutoQueryServiceBaseType = typeof(MyAutoQueryServiceBase)
});


来源:https://stackoverflow.com/questions/30637599/servicestack-auto-query-global-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!