Extend SubSonic's IQueryable Structure (via LINQ?)
I'm working on a project that groups data by a "customer id". When the user logs in, they're limited to that customer and that customer only.
I'm working with SubSonic3, and what I've got looks something like this:
public IEnumerable<Models.Foo> FindFoo(int userID, string searchText, int pageIndex, int pageSize)
{
return from item in Foo.GetPaged(pageIndex, pageSize)
where (item.Name.Contains(searchText) ||
item.Description.Contains(searchText)) &&
item.CustomerID == CurrentCustomerID()
select new Models.Foo(item);
}
What I'd like to do is abstract away the item.CustomerID line, because that's going to happen for every query, without fail, so it would be simpler (and more secure) to do it in one place and guarantee it'll happen everywhere.
So, my question is: can this be done, and if开发者_开发知识库 so, how?
As far as subsonic generated classes are partial you can add some interface to them... Just create you're interface with CustomerID property (for example name it ICusomerEntity
) and make partial for any of generated classes that will apply this interface to them... then just use the generic subsonic methods to rerieve ICustomerEntity
, not the specific class..
For example you can make generic method called GetCustomerEntity with constraint T:ICustomerEntity
which will return IQueriable with a bascic query comparing CustomerId with the one of logged user..
Hope it helps...
精彩评论