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

前端 未结 18 1086
栀梦
栀梦 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:40

    I tend to prefer using stored procedures for several reasons:

    1. it makes the security configuration easier (as mentioned by other posters).
    2. It provides a clearly defined interface for DB access (although responsibility for this could be shifted into other areas, such as a DAL written in C#
    3. I find that the Query Optimizer, in Oracle at least, is able to make more intelligent decisions the more information you give it. This really requires testing with both methods though for your specific scenarios though.
    4. Depending on the developers available, you may have some very good SQL coders who will be better at producing efficient queries if they use sprocs.

    The downside is that it can be a pain to keep the code that invokes the sprocs in sync with the database if things are evolving rapidly. The points about producing efficient queries could count as premature optimization. At the end of the day, there is no substitute for benchmarking performance under realistic conditions.

提交回复
热议问题