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

查看:46
本文介绍了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天全站免登陆