Rx 扩展是否适合读取文件并存储到数据库
我有一个很长的 Excel 文件,我使用 EPPlus 读取它。对于每一行,我测试它是否满足特定条件,如果满足,我将行(代表行的对象)添加到集合中。读取文件时,我将这些对象存储到数据库中。是否可以同时做这两件事?我的想法是拥有一个对象集合,这些对象会以某种方式被线程消耗,从而将对象保存到数据库中。同时 excel reader 方法将填充集合...这可以使用 Rx 来完成还是有更好的方法?
谢谢。
I have a really long Excel file wich I read using EPPlus. For each line I test if it meets certain criteria and if so I add the line (an object representing the line) to a collection. When the file is read, I store those objects to the database. Would it be possible to do both things at the same time? My idea is to have a collection of objects that somehow would be consumed by thread that would save the objects to the DB. At the same time the excel reader method would populate the collection... Could this be done using Rx or is there a better method?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
另一个答案 - 基于我的第一个答案。
创建一个从 EPPlus/Xls 返回
IEnumberable
的函数 - 使用yield return
然后将序列转换为线程池上的可观察对象,您就获得了拥有生产者/消费者和 BlockingCollection 的 Rx 方式。
An alternate answer - based on comments to my first.
Create a function returning an
IEnumberable<Records>
from EPPlus/Xls - useyield return
then convert the seqence to an observable on the threadpool and you've got the Rx way of having a producer/consumer and BlockingCollection.
您的情况似乎是提取数据(
IEnumerable
)而不是推送数据(IObservable/Rx
)。因此,我建议可以使用 LINQ to object 来对解决方案进行建模。
类似于下面的代码所示。
注意:在使用 IEnumerable 时,在这种情况下您不会创建中间集合,整个过程看起来像一个管道。
Your case seems to be of pulling data (
IEnumerable
) and not pushing data (IObservable/Rx
).Hence I would suggest LINQ to objects is something that can be used to model the solution.
Something like shown in below code.
NOTE: In using IEnumerable you don't create intermediate collections in this case and the whole process looks like a pipeline.
这似乎不太合适,因为用例中没有固有的并发性、计时或事件。
也就是说,这可能是 plinq 的一个用例。如果 EEPlus 支持并发读取。像这样的东西
It doesn't seem like a good fit, as there's no inherent concurrency, timing, or eventing in the use case.
That said, it may be a use case for plinq. If EEPlus supports concurrent reads. Something like