存储过程还是LINQ? [英] Stored procedure or LINQ?

查看:60
本文介绍了存储过程还是LINQ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用数据库SQL Server在ASP.net MVC 2中创建的网上商店.

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. 通过存储过程进行过滤吗?

  1. Make the filtration via stored procedure?

使用LINQ作为C-sharp编码吗?

Using LINQ as a C-sharp coding?

另一种解决方案?

推荐答案

如果您确实想优化sql,我只会使用存储过程.否则,如果您想使其灵活并且可以轻松地插入过滤器,我建议您使用类型管道和过滤器模式.它将像这样工作:

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);
    }
}

在这种情况下,名称ProductRepository可能是错误的,因为它不是真正的存储库,更像是某种桥梁".但是,此模式可让您轻松添加其他过滤器,例如扩展方法.重要的是,您必须从扩展方法和存储库"中返回IQueryable,这使得查询仅被评估一次,并且可以链接过滤器.

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.

这篇关于存储过程还是LINQ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆