使用linq2sql为存储过程生成类好,还是直接调用存储过程好?
我看到很多关于 LINQ to SQL 与存储过程的问题。我更好奇在对象映射方面串联使用它们的好处。
我定义了业务对象,并且为所有 CRUD 事务存储了过程。
是否最好将所有存储过程放入 DBML 文件中并从那里调用它们,然后将结果映射到我的业务对象,或者最好只使用 DataReader 并从那里映射它?
这对我来说很烦人,因为我想要我定义的对象,而不是使用 linq2sql 生成的 MyStoredProcResult 对象,所以我觉得我正在通过字段映射执行与数据读取器相同的字段。
性能在这里不一定是关键(除非它慢得离谱)。我希望为所有开发人员创建一种标准方法,以最简单的方式使用最少的代码将数据从数据库加载到对象中。
I see tons of questions on LINQ to SQL vs Stored Procs. I'm more curious about the benefits of using them in tandem as relates to object mapping.
I have my business objects defined, and I have stored procedures for all of my CRUD transactions.
Is it better to plop all the stored procs into a DBML file and call them from there, and then map the results to my business objects, or is it better to just use a DataReader and map it from there?
It's annoying to me because I want my objects as I define them, rather than use MyStoredProcResult objects as linq2sql generates, so I feel I'm doing the same field by field mapping as I would with a data reader.
Performance isn't necessarily key here (unless it's ridiculously slow). I'm looking to create a standard way for all our developers to load data from a database into an object in the simplest fashion with the least amount of code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
映射到 LINQ2SQL 在类型安全方面具有很大的优势 - 您实际上不必担心解析结果或添加命令参数。它为您完成这一切。
另一方面,事实证明,直接使用 SQLcommand 调用存储过程和 DataReader 具有更好的性能(特别是在读取/更改大量数据时)。
无论您选择哪一个,最好构建一个单独的数据访问层,因为它可以提供更大的灵活性。访问/更改数据库的逻辑不应内置到您的业务对象中,因为如果您被迫更改存储数据的方式,那么更新软件将会很痛苦。
Mapping to LINQ2SQL has a serious advantage in being type-safe - you don't really have to worry about parsing the results or adding command parameters. It does it all for you.
On the other hand with calling stored procedures directly with SQLcommand and DataReader proves to have better performance (especially when reading/changing a lot of data).
Regardless of which you choose it is better to build a separate Data Access Layer as it allows more flexibility. The logic of accessing/changing database should not be built into your business objects cos if you are forced to change means of storing you data it updating you software will be painful.
不是直接回答您的问题,但如果您希望对象作为查询结果,您可能必须考虑代码优先模式。 Linq2SQL 不支持此功能,但 Entity Framework 和 NHibernate 确实如此。
直接的答案是,DataReader 显然会具有更少的开销,但同时它会有更多的魔术字符串。就性能而言,开销很糟糕(在你的情况下没有那么大)。魔术字符串在维护代码方面很糟糕。所以这绝对是您个人的选择。
Not direct answer to your question, but if you want your objects as result of query, you probably have to consider code first schemas. Linq2SQL does not support this, but Entity Framework and NHibernate does.
Direct answer is that DataReader will obviously has less overhead, but at the same time it will have much more magic strings. Overhead is bad in terms of perfomance(in your case not that big). Magic strings are bad in terms maintaining code. So definetly this will be your personal choise.
LINQ2SQL 可以提供填充有查询结果的对象。您必须以支持 List(Of T) 或 List 的方式构建子对象,具体取决于您的语言选择。
假设您有一个表,其中包含 ID、公司名称和电话号码字段。在 LINQ 或存储过程中查询该表将是直接的。 LINQ 带来的优点是能够将结果映射到匿名类型或您自己的类。因此查询:
将返回匿名类型。但是,如果您也有这样的类:
将查询更改为此将在数据中移动时填充 Company 对象:
我的 C# 语法可能不是 100% 正确,因为我主要用 VB 编写代码,但它足够接近带你去那里。
LINQ2SQL can provide your objects populated with the results of the query. You will have to build child objects in such a way as to support either a List(Of T) or List depending on your language choice.
Suppose you have a table with an ID, a Company Name, and a Phone Number for fields. Querying that table would be straight-forward in either LINQ or a stored procedure. The advantage that LINQ brings is the ability to map the results to either anonymous types or your own classes. So a query of:
would return an anonymous type. However, if you also have a class like this:
changing your query to this will populate Company objects as it moves through the data:
My C# syntax may not be 100% correct as I mainly code in VB, but it will be close enough to get you there.