MVC - 域服务负责过滤还是存储库层负责?

发布于 2024-10-12 18:42:34 字数 828 浏览 1 评论 0原文

我应该从域服务中过滤我的 IQueryable 结果吗?

例如......我的3个门户(网站)访问相同的域服务层,根据用户的类型,我调用特定的存储库方法并返回结果,

当前存储库层:

    IQueryable<Products> GetAllProductsForCrazyUserNow(CrazyUser id);
    Products GetAProductForCrazyUserNow(CrazyUser id,product id);

    IQueryable<Products> GetProductsForNiceUserNow(NiceUser id);
    Products GetProductsForNiceUserNow(NiceUser id,product id);

最好这样做在存储库层中:

    IQueryable<Products> GetAllProducts();
    Products GetAProduct(product id);

然后在域服务中,我简单地进行过滤,即

var Niceman = IQueryable<Products> GetAllProducts().Where(u=> u.Name == "Nice");

注意:我有一个只读会话和在存储库层中包含 CRUD 的会话,因此请在回答时记住这一点。

第二个问题:我应该在域服务层进行任何过滤吗?这一层是唯一可以修改实体的层,即 Product.Price == 25.00;这不会委托给存储库层。

Should I be filtering my IQueryable results from the Domain Service?

For example... My 3 portals (Websites) access the same Domain Service Layer, depending on the type of user it is, I call a specific repository method and return the result,

Current Repository Layer:

    IQueryable<Products> GetAllProductsForCrazyUserNow(CrazyUser id);
    Products GetAProductForCrazyUserNow(CrazyUser id,product id);

    IQueryable<Products> GetProductsForNiceUserNow(NiceUser id);
    Products GetProductsForNiceUserNow(NiceUser id,product id);

Would it be best just to do this in the Repository Layer:

    IQueryable<Products> GetAllProducts();
    Products GetAProduct(product id);

Then within the Domain Service, I simple do the filter i.e.

var Niceman = IQueryable<Products> GetAllProducts().Where(u=> u.Name == "Nice");

NOTE: I have a read only session and session which includes CRUD within the Repository Layer, so please keep that in mind when answering.

Second question: Should I do any filtering at all in the domain service layer? This very layer is the only layer than can amend the Entity i.e. Product.Price == 25.00; This is not delegated to the repository layer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴意少年 2024-10-19 18:42:34

我通常使用存储库层执行简单的 CRUD 工作,并让域层执行任何业务逻辑,或者在您的情况下执行将数据传递回 UI 层之前所需的任何过滤。

将业务/过滤逻辑与存储库层分离将有助于保持事物的整洁。此外,将来如果您转向不同类型的数据访问模式,那么您不必更改该代码的工作方式,因为它将在域层中分离。

I normally use the repository layer to do simple CRUD work and have the domain layer perform any business logic or in your case any filtering that is needed before passing that data back to the UI layer.

Separating out the business/filtering logic from the repository layer will help keep things clean. Also, in the future if you move to a different type of data access pattern, then you won't have to change how that code works since it will be separated in your domain layer.

旧话新听 2024-10-19 18:42:34

Haroon,

我在存储库外部的 IQueryable 上使用扩展方法。事实上,我有一组称为“过滤器”的类,它们通常是这样的:

public static class ProductFilters
{
    public static IQueryable<Products> NiceMan(
        this IQueryable<Products> customQuery, string filterName)
    {
        if (!string.IsNullOrEmpty(filterName))
           customQuery = customQuery.Where(u => u.Name == filterName);
        return customQuery;
    }
   // create lots of other Products based filters here
   // and repeat with seperate IQueryable<Classes> per type
}

用法:

var Niceman = IQueryable<Products> GetAllProducts().NiceMan("Nice");

我发现这是一个很好的逻辑分离并保持存储库干净。对于第二个问题,是的,在服务层内使用此过滤器/扩展逻辑,而不是在存储库中。

Haroon,

I use extension methods on IQueryable<Classes> OUTSIDE of the repo. in fact, i have a set of classes that i call 'Filters' and they are usually something along these lines:

public static class ProductFilters
{
    public static IQueryable<Products> NiceMan(
        this IQueryable<Products> customQuery, string filterName)
    {
        if (!string.IsNullOrEmpty(filterName))
           customQuery = customQuery.Where(u => u.Name == filterName);
        return customQuery;
    }
   // create lots of other Products based filters here
   // and repeat with seperate IQueryable<Classes> per type
}

usage:

var Niceman = IQueryable<Products> GetAllProducts().NiceMan("Nice");

I find this a good separation of logic and keeps the repo clean. And in answer to the second question, yes, use this filter/extension logic inside the service layer, rather than the repo as well.

小巷里的女流氓 2024-10-19 18:42:34

我自己也有过同样的问题。我对将其放入服务层的犹豫是,在某些情况下,过滤过程可能会删除从存储库返回的大量记录,并且我不想从数据库中提取超出必要的数据。

我最终转向 NHibernate,并让我的存储库方法接受 DetachedCriteria 参数。然后,我将用户信息传递到服务层,并让它执行过滤,不是通过操作 IQueryable,而是通过构造一个 DetachedCriteria 对象并将其传递到存储库,从而修改 SQL 并限制数据库工作。

到目前为止,它似乎工作得很好,并且“感觉”正确,因为我的逻辑在服务层中非常牢固,而存储库只执行基本的 CRUD。

I've had this same question myself. My hesitation to putting it in the service layer was that in some cases the filtering process could remove the bulk of the records returned from the repo, and I didn't want to pull more data than necessary from the database.

I ended up moving to NHibernate, and had my repository methods accept DetachedCriteria arguments. I then passed the user information to the service layer and had it perform the filtering not by manipulating an IQueryable but by constructing a DetachedCriteria object and passing it along to the repo, thus modifying the SQL and limiting the database work.

So far it seems to work pretty well, and "feels" right since I have my logic pretty solidly in the service layer with the repo only doing basic CRUD.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文