Why do I need Stored Procedures when I have LINQ to SQL

前端 未结 18 1090
栀梦
栀梦 2021-02-05 15:59

My understanding of Linq to Sql is it will take my Linq statement and convert it into an equivalent SQL statement.

So

var products = from p in db.Product         


        
相关标签:
18条回答
  • 2021-02-05 16:25

    Simple example:

    select * from Products where GetCategoryType(CategoryName)=1
    

    GetCategoryType can run really fast, because it runs on the DB server. There's no Linq to SQL substitute for that as far as I know.

    0 讨论(0)
  • 2021-02-05 16:26

    There are significant associated performance improvements on the SQL Server side of things if you use stored procedures in appropriate circumstances.

    0 讨论(0)
  • 2021-02-05 16:26

    Stored Procedures are useful in many cases, but in General if you are using an ORM you should let the ORM generate the SQL for you. Why should we have to maintain at a minimum of four stored procedures (insert update delete and a single select) for each table.

    With that said as people pointed out there are security benefits to using stored procedures. You won't have to grant users read/write to the tables, which is a good protection against SQL Injection.

    Stored Procedures are also useful when the logic used to retrieve data is fairly complex. You typicaly see this more in Reporting Scenario's and in which case your probally not using Linq2Sql or some other ORM.

    In my opinion if your not generating your SQL but essentially hardcoding it within an app tier, then that should be refactored into stored procedures, and yes there are always exceptions to any rules but in general.

    One use of a stored procedure in Linq2Sql might be if you have multiple servers, and are linking to them, you could use a stored procedure to expose data from that other server and manipulate it. This would hide the multiple servers from your application.

    0 讨论(0)
  • 2021-02-05 16:27
    1. Stored Procedures and Linq to Sql solve different problems.

    2. Linq to Sql is particular to Microsoft SQL Server.

    0 讨论(0)
  • 2021-02-05 16:27

    Sprocs have their uses, just like using LINQ does. IMO if an operation is performed multiple times in multiple places then it's a good candidate for "refactoring" into a Stored Proc, as opposed to a LINQ statement that is repeated in different places.

    Also, and this is probably blasphemy to a lot of people here, sometimes you should put some logic into the database and then a sproc comes in handy. It's a rare occurrence but sometimes the nature of business rules demands it.

    0 讨论(0)
  • 2021-02-05 16:28

    You certainly don't "need" stored procedures. But they can come in handy if your domain model requires a complex aggregate Entity and you don't have the luxury/flexibility to modify your database tables to fit your domain model. In this case using Linq-to-SQL or another ORM might result in a very poorly performing set of database calls to construct your Entity. A stored proc can come to the rescue here.

    Of course, I would advocate using a methodology or process like TDD/BDD that provides you the flexibility to modify your database tables as needed without much pain. That's always the easier, more maintainable path in my opinion.

    0 讨论(0)
提交回复
热议问题