Extend SubSonic IQueryable Structure (via LINQ?)

I am working on a project that groups data by "customer id". When a user logs in, they are limited to only this client and this client.

I am working with SubSonic3 and what I have is 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 would like to do is abstract the item.CustomerID line, because this will happen for every request, without fail, so it would be easier (and more secure) to do it in one place and guarantee it "Happens everywhere".

So my question is, can this be done, and if so, how?

+2


a source to share


1 answer


Since the subsonic generated classes are partial, you can add some interface to them ... Just create an interface with CustomerID (like its name ICusomerEntity

) and make it partial for any of the generated classes that this interface will apply to them ... then just use general subsonic methods for re-execution ICustomerEntity

, not class specific.

For example, you can create a generic method GetCustomerEntity with a constraint T:ICustomerEntity

that will return an IQueriable with a basic query comparing the CustomerId to the logged in user.



Hope it helps ...

+1


a source







All Articles