views:

125

answers:

6

I want to convert all of my db stored procedures to linq to sql expressions, is there any limitation for this work? you must notice that there is some complicated queries in my db.

+1  A: 

What does this mean? Does this mean you want to use L2S to call your stored procedures, or do you want to convert all the T-SQL statements in your stored procs to L2S? If it's the later, you should not have too many problems doing this. Most T-SQL statements can be represented in Linq without problem.

I might suggest you investigate a tool like Linqer to help you with your T-SQL conversion. It will convert most any T-SQL statement into Linq. It has saved my quite a bit of time in converting some of my queries.

Randy Minder
A: 

If you already have tested and working stored procedures, why convert them at all? That's just making work for no reason.

If you were starting a new product from scratch and were wondering whether to use stored procedures or not, that would be an entirely different question.

Christian Hayter
@Christian - Why convert them? One very good reason. By converting the queries into Linq queries, you gain the benefit of having strongly typed queries in your application. This is very powerful when you start refactoring your DB. You can then let the compiler tell you which queries need rework.
Randy Minder
+1: If it isn't broken, don't fix it. (Albeit in this case it is not clear that it is or isn't broken, or what value of "broken" is broken enough.)
Richard
@Richard -- that's true, but there are more definitions of "broken" than "not working". Moving to a "code only" solution may increase maintainability, reduce technical debt, or better fit the mix of developer skills available. A "working" solution can still be broken from another perspective.
tvanfosson
There's a couple of other reasons to keep sprocs. Security-wise, you can lock access to an entire table and only allow access to the data via sprocs. With LINQ you'll be executing regular DML. Also, sprocs have the benefit of their execution plans being cached after the first call. Also, also, depending on your architecture, its often easier to fix a database query in a sproc instead of recompiling and shipping a new binary every time.
Chris Haas
You know what this reminds me of, actually? Back in ASP classic days I would see people every once in a while decide to run everything through "index.asp" in root and just have a bunch of query string parsers that kicked out different content. Sounds great - one file that houses all of the logic but really its a maintenance nightmare.
Chris Haas
A: 

There are many constructs in T-SQL which have no parallel in LINQ to SQL. Starting with flow control, ability to return multiple row sets, recursive queries.

You will need to approach this on a case by case basis. Remembering any times the SP does significant filtering work on the database much of that filtering may end up on the client, so needing to move far more data from server to client.

Richard
The point of LINQ to SQL is that, if done properly, the work still gets done in the database because LINQ takes the expression you've built and converts it to a query. It's not always the case because some C# constructs have no translation to SQL (at least, not yet), but in most cases it is.
tvanfosson
@tvanfosson: More a case of many T-SQL constructs not accessible from LINQ. If the SPs are largely wrappers around single DML statements, then the translation is almost certainly possible, but if more advanced T-SQL is used (e.g. recursive queries) there is on LINQ equivalent. Without knowing more about the nature of the SPs). I would stick with the general statement of not changing it if it works.
Richard
Yes, recursive queries are a good reason to stick with stored procedures.
Steven
+4  A: 

I've found that in almost all cases where I've done a new project with L2S, I've completely removed the need for stored procedures. In fact, many of the cases where I would have been forced to use a stored proc, multivariable filters for instance, I've found that by building the query dynamically in LINQ, I've actually gotten better queries in the vast majority of cases since I don't need to include those parts of the query that get translated to "don't care" in the stored proc. So, from my perspective, yes -- you should be able to translate your stored procs to LINQ.

A better question, thought, might be should you translate your stored procs to LINQ? The answer to that, I think, depends on the state of the project, your relative expertise with C#/VB and LINQ vs SQL, the size of the conversion, etc. On an existing project I'd only make the effort if it improves the maintainability or extensibility of the code base, or if I was making significant changes and the new code would benefit. In the latter case you may choose to incrementally move your code to pure LINQ as you touch it to make changes. You can use stored procs with LINQ so you may not need to change it to make use of LINQ.

tvanfosson
+2  A: 

I'm not a fan of this approach. This is a major architectural change, because you are now removing a major interface layer you previously put in place to gain a decoupling advantage.

With stored procedures, you have already chosen the interface your database exposes. You will now need to grant users SELECT privileges on all the underlying tables/views instead of EXECUTE on just the application stored procedures and potentially you will need to restrict column read rights at the column level in the tables/views. Now you will need to re-implement at a lower level every explicit underlying table/view/column rights which your stored procedure was previously implementing with a single implicit EXECUTE right.

Whereas before the services expected from the database could be enumerated by an appropriate inventory of stored procedures, now the potential database operations are limited to the exposed tables/views/columns, vastly increasing the coupling and potential for difficulty in estimating scope changes for database refactorings and feature implementations.

Unless there are specific cases where the stored procedure interface is difficult to create/maintain, I see little benefit of changing a working SP-based architecture en masse. In cases where LINQ generates a better implementation because of application-level data coupling (for instance joining native collections to database), it can be appropriate. Even then, you might want to LINQ to the stored procedure on the database side.

If you chose LINQ from the start, you would obviously have done a certain amount of work up front in determining column/view/table permissions and limiting the scope of application code affecting database implementation details.

Cade Roux
Important considerations, however if security is an issue you can use sps for insert, updates, deletes, (using LINQ to SQL through the SP methods is simple anyways), but still allow readonly queries. (Yes, sometimes even select priviledges is too much, YMMV.)
Richard Hein
+6  A: 

Several features of SQL Server are not supported by Linq to SQL:

  • Batch updates (unless you use non-standard extensions);
  • Table-Valued Parameters;
  • CLR types, including spatial types and hierarchyid;
  • DML statements (I'm thinking specifically of table variables and temporary tables);
  • The OUTPUT INTO clause;
  • The MERGE statement;
  • Recursive Common Table Expressions, i.e. hierarchical queries on a nested set;
  • Optimized paging queries using SET ROWCOUNT (ROW_NUMBER is not the most efficient);
  • Certain windowing functions like DENSE_RANK and NTILE;
  • Cursors - although these should obviously be avoided, sometimes you really do need them;
  • Analytical queries using ROLLUP, CUBE, COMPUTE, etc.
  • Statistical aggregates such as STDEV, VAR, etc.
  • PIVOT and UNPIVOT queries;
  • XML columns and integrated XPath;
  • ...and so on...

With some of these things you could technically write your own extension methods, parse the expression trees and actually generate the correct SQL, but that won't work for all of the above, and even when it is a viable option, it will often simply be easier to write the SQL and invoke the command or stored procedure. There's a reason that the DataContext gives you the ExecuteCommand, ExecuteQuery and ExecuteMethodCall methods.

As I've stated in the past, ORMs such as Linq to SQL are great tools, but they are not silver bullets. I've found that for larger, database-heavy projects, L2S can typically handle about 95% of the tasks, but for that other 5% you need to write UDFs or Stored Procedures, and sometimes even bypass the DataContext altogether (object tracking does not play nice with server triggers).

For smaller/simpler projects it is highly probable that you could do everything in Linq to SQL. Whether or not you should is a different question entirely, and one that I'm not going to try to answer here.

Aaronaught
+1 I would also add PIVOT queries, often used when dealing with EAV tables.
Remus Rusanu
Indeed, and the EAV comment also reminded me of XML columns which you can't use effectively in L2S either. Although some might say that if you're using an EAV model then you've already lost the battle. :P
Aaronaught
@Aaronaught: thanks for your useful answer.
masoud ramezani