Stored procedure or LINQ? [closed]

左心房为你撑大大i 提交于 2020-01-03 03:24:08

问题


I have a webshop created in ASP.net MVC 2 with a database SQL server.

If I need to make filtration for instance specfic products from the stock in database via webshop.

Which options should I choose and why?

  1. Make the filtration via stored procedure?

  2. Using LINQ as a C-sharp coding?

  3. Another solution?


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/4583936/stored-procedure-or-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!