哪种 DataContext 方法会更快?
我使用基本的 DataContext 创建对象,然后将它们提交到数据库中。
我自己编写了一些测试,看看哪种方法更快,但只是想知道哪种方法被认为是以下方法中的最佳实践。
代码迭代循环并实例化要保存到数据库的对象。是否更好:
1.) 创建一个对象列表,然后将每个创建的对象分配到列表,然后在最后使用
MyDataContext.InsertAllOnSubmit(ListOfObjects)
2.) 使用将每个创建的对象直接分配到 DataContext
MyDataContext.InsertOnSubmit(Object)
希望这是有道理的,如果有人需要更多信息,让我知道!
谢谢
I have using a basic DataContext to create objects then submit these into a database.
Have written a couple of tests myself to see which is fast but just wondering which method is considered best practice out of the following.
Code iterates through a loop and instantiates an object which is to be persisted to the database. Is it better to:
1.) Create a list of objects then and assign each created object to the list then at the end use
MyDataContext.InsertAllOnSubmit(ListOfObjects)
2.) Assign each created object directly into the DataContext using
MyDataContext.InsertOnSubmit(Object)
Hope this makes sense, if anyone needs more information let me know!
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我假设我们正在讨论对提交事件的性能影响 - 调用这些方法时没有立即打开数据库连接。
由于每个实现只会在提交时更新数据库,因此它们非常相似。
任何性能差异都是微不足道的(并且将通过您将对象放入列表或枚举列表时所做的任何处理来抵消),因此请选择更适合您的设计的那个。
您可能会发现此有关过早优化的页面很有趣 - http://c2.com/cgi/wiki?PrematureOptimization
I assume we're talking about the performance impact on the submit event - there is no database connection opened immediately when these methods are called.
Since each implementation will only update the database on Submit, they are both very similar.
Any performance difference will be marginal (and will be countered by whatever processing you do to put the objects into the List or enumerate the List), so go with whichever fits better into your design.
You might find this page about premature optimization interesting - http://c2.com/cgi/wiki?PrematureOptimization
我想对于第二个选项,您需要为每个操作重新打开连接。使用列表更干净,也是更好的选择。
I guess for the second option, you'll need to re-open the connection for each operation. Using a list is cleaner and a better option.