如何使用实体+ LINQ不仅仅是基本上硬编码我的疑问? [英] How is using Entity + LINQ not just essentially hard coding my queries?

查看:209
本文介绍了如何使用实体+ LINQ不仅仅是基本上硬编码我的疑问?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我一直在发展与实体+ LINQ了一点,现在我真的开始怀疑的最佳做法。我已经习惯了,如果我需要得到的数据,引用存储过程的模式。存储过程可以根据需要在飞行中改变,而并不需要code重新编译。我发现,我在我的code查询在寻找这样的:

So I've been developing with Entity + LINQ for a bit now and I'm really starting to wonder about best practices. I'm used to the model of "if I need to get data, reference a stored procedure". Stored procedures can be changed on the fly if needed and don't require code recompiling. I'm finding that my queries in my code are looking like this:

List<int> intList = (from query in context.DBTable
                     where query.ForeignKeyId == fkIdToSearchFor
                     select query.ID).ToList();

和我开始怀疑,所不同的是,这之间有什么:

and I'm starting to wonder what the difference is between that and this:

List<int> intList = SomeMgrThatDoesSQLExecute.GetResults(
                                  string.Format("SELECT [ID]
                                                 FROM DBTable
                                                 WHERE ForeignKeyId = {0}",
                                  fkIdToSearchFor));

我担心的是我正在基本上硬编码查询到code。我失去了一些东西?就是实体的地步?如果我需要做任何真正的查询工作,我应该把它放在一个存储过程?

My concern is that that I'm essentially hard coding the query into the code. Am I missing something? Is that the point of Entity? If I need to do any real query work should I put it in a sproc?

推荐答案

我个人避免硬code SQL请求(如你的第二个例子)。写作,而不是实际的SQL LINQ允许:

I personally avoid to hard-code SQL requests (as your second example). Writing LINQ instead of actual SQL allows:

  • 在易用性(智能感知,类型检查...)
  • 的LINQ语言功率(这是大多数的时间比SQL更简单,当有一定的复杂性,多加入...等)
  • 匿名类型的电源
  • 在发现错误,现在在编译时,而不是在运行时两个月过去了......
  • 在更好的重构,如果您要重命名的表/列/ ...(你不会忘记与编译时检查LINQ becaues重命名任何东西)
  • 您的请求和你的数据库(如果你从Oracle迁移到SQL Server的?使用LINQ,你会不会改变你的code,硬codeD要求你必须检查所有之间的松耦合您的要求)
  • 在LINQ VS存储过程:你把逻辑的code,不是在你的数据库。见讨论<一href="http://stackoverflow.com/questions/1473624/business-logic-in-database-versus-$c$c">here.
  • ease of use (Intellisense, type check...)
  • power of LINQ language (which is most of the time more simple than SQL when there is some complexity, multiple joins...etc.)
  • power of anonymous types
  • seeing errors right now at compile-time, not during runtime two months later...
  • better refactoring if your want to rename a table/column/... (you won't forget to rename anything with LINQ becaues of compile-time checks)
  • loose coupling between your requests and your database (what if you move from Oracle to SQL Server? With LINQ you won't change your code, with hardcoded requests you'll have to review all of your requests)
  • LINQ vs stored procedures: you put the logic in your code, not in your database. See discussion here.

如果我需要得到的数据,引用一个存储过程。存储过程   可以根据需要在飞行中改变,而并不需要code重新编译

if I need to get data, reference a stored procedure. Stored procedures can be changed on the fly if needed and don't require code recompiling

- >如果你需要更新你的模型,你可能还需要更新您的code利用数据库​​的更新考虑。因此,我不认为它会帮助你避免重新编译大部分时间。

-> if you need to update your model, you'll probably also have to update your code to take the update of the DB into account. So I don't think it'll help you avoid a recompilation most of the time.

这篇关于如何使用实体+ LINQ不仅仅是基本上硬编码我的疑问?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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