Stored procedure or LINQ? [closed]

六月ゝ 毕业季﹏ 提交于 2019-12-06 16:37:46

I would only use stored procedure if you really want to optimize your sql. Otherwise if you want to keep it flexible and make it possible to plugin filters easily I would recommend a type pipes and filters pattern. It would work like this:

public class ProductRepository
{
    public IQueryable<Prodcut> GetAll() 
    {
        return yourContext.Products;
    }
}

public static class ProductFilters
{
    public static IQueryable<Product> ByCategory(this IQueryable<Product> query, string category)
    {
        return query.Where(p => p.Category == category);
    }
}

The name ProductRepository is probably wrong in this case since it is not truly a repository, more some kind of "bridge". But this pattern allow you to easily add additional filters like extension methods. It is important that you return IQueryable from the extension methods and your "repository", this make the query to be evaluated only once and you can chain your filters.

I am using the following in similar cases:

  1. Devart LinqConnect
  2. Creating stored procedures (for SQL optimizing)
  3. Include these procedures to the model and work with complex types
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!