开发者

Stored procedure or LINQ? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 8 years ago.

Improve this开发者_JAVA百科 question

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?


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜