问题
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