扩展 SubSonic 的 IQueryable 结构(通过 LINQ?)
我正在开发一个按“客户 ID”对数据进行分组的项目。当用户登录时,他们仅限于该客户并且仅限于该客户。
我正在使用 SubSonic3,我得到的看起来像这样:
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);
}
我想做的是抽象掉 item.CustomerID 行,因为每个查询都会发生这种情况,不会失败,因此在一个地方执行此操作会更简单(也更安全),并保证它会在任何地方发生。
所以,我的问题是:这可以做到吗?如果可以,怎么做?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
至于亚音速生成的类是部分的,您可以向它们添加一些接口...只需创建具有 CustomerID 属性的接口(例如将其命名为
ICusomerEntity
),并对任何生成的类进行部分会将此接口应用于它们...然后只需使用通用的亚音速方法来检索ICustomerEntity
,而不是特定的类。例如,您可以使用约束
T:ICustomerEntity 创建名为 GetCustomerEntity 的通用方法
它将返回 IQueriable,并带有一个基本查询,将 CustomerId 与登录用户的 ID 进行比较。希望它有帮助...
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 rerieveICustomerEntity
, 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...