LINQ to SQL 的优点是什么? [英] What are the advantages of LINQ to SQL?

查看:28
本文介绍了LINQ to SQL 的优点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始在一个中型项目中使用 LINQ to SQL,并希望加深我对 L2S 提供的优势的理解.

I've just started using LINQ to SQL on a mid-sized project, and would like to increase my understanding of what advantages L2S offers.

我看到的一个缺点是它增加了另一层代码,我的理解是它的性能比使用存储过程和 ADO.Net 慢.调试似乎也是一个挑战,尤其是对于更复杂的查询,而且这些查询无论如何最终可能会被移动到存储过程中.

One disadvantage I see is that it adds another layer of code, and my understanding is that it has slower performance than using stored procedures and ADO.Net. It also seems that debugging could be a challenge, especially for more complex queries, and that these might end up being moved to a stored proc anyway.

我一直想要一种在更好的开发环境中编写查询的方法,L2S 查询是我一直在寻找的解决方案吗?或者我们刚刚在 SQL 之上创建了另一个层,现在有两倍的担心?

I've always wanted a way to write queries in a better development environment, are L2S queries the solution I've been looking for? Or have we just created another layer on top of SQL, and now have twice as much to worry about?

推荐答案

L2S 提供的优势:

  • 没有魔法字符串,就像在 SQL 查询中一样
  • 智能感知
  • 在数据库更改时编译检查
  • 更快的开发
  • 工作单元模式(上下文)
  • 自动生成的域对象是可用的小项目
  • 延迟加载.
  • 学习编写 linq 查询/lambdas 是 .NET 开发人员必须学习的内容.

关于性能:

  • 在大多数解决方案中,性能很可能不会成为问题.预先优化是一种反模式.如果您稍后发现应用程序的某些区域变慢,您可以分析这些部分,在某些情况下甚至将一些 linq 查询与存储过程或 ADO.NET 交换.
  • 在许多情况下,延迟加载功能可以提高性能,或者至少可以大大简化代码.

关于调试:

  • 在我看来,调试 Linq2Sql 比存储过程和 ADO.NET 容易得多.我建议你看看 Linq2Sql Debug Visualizer,它使您能够查看查询,甚至在调试时触发执行以查看结果.
  • 您还可以配置上下文以将所有 sql 查询写入控制台窗口,更多信息 这里
  • In my opinion debuging Linq2Sql is much easier than both stored procedures and ADO.NET. I recommend that you take a look at Linq2Sql Debug Visualizer, which enables you to see the query, and even trigger an execute to see the result when debugging.
  • You can also configure the context to write all sql queries to the console window, more information here

关于另一层:

  • Linq2Sql 可以看作是另一层,但它是一个纯粹的数据访问层.存储过程也是另外一层代码,我见过很多将部分业务逻辑实现到存储过程中的案例.这在我看来要糟糕得多,因为您随后将业务层拆分为两个地方,开发人员将更难以清楚地了解业务领域.

这篇关于LINQ to SQL 的优点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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