Linq Datacontext 和“工作单元”
现在 Linq 中的流行词是“工作单元”。 如“仅在一个工作单元中保留数据上下文”,然后销毁它。
嗯,我对此有几个问题。
- 我正在创建一个胖客户端 WPF 应用。因此,我的数据上下文需要跟踪当前屏幕上用户可用的实例化对象的整个网络。我什么时候可以销毁我的数据上下文?
- 我根据用户的操作及其与第一个数据上下文的对象的交互随着时间的推移构建 linq 查询。如何创建新的 DataContext 并在新的 Context 上执行查询?
我希望我说清楚了。 谢谢
The buzword in Linq now days is "unit of work".
as in "Only keep your datacontext in existence for one unit of work" then destroy it.
Well I have a few questions about that.
- I am creating a fat client WPF
application. So my data context needs to track the entire web of instantiated object available for the user on the current screen. when can I destroy my datacontext? - I build a linq query over time based on actions of the user and their interactions with objects of the first datacontext. How can I create a new DataContext and execute the query on new Context?
I hope I was clear.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
工作单元与仅在一个工作单元中保留数据上下文不同。
工作单元是一种设计模式,描述如何以抽象方式表示事务。实际上只有创建、更新和删除 (CUD) 操作才需要它。
一种理念是 UoW 用于所有 CUD 操作,而只读存储库用于读取操作。
无论如何,我建议将对象生命周期与 UoW 或存储库使用解耦。使用依赖注入 (DI) 将两者注入到您的消费服务中,并让 DI 容器管理两者的生命周期。
在 Web 应用程序中,我的经验是对象上下文应该仅在单个请求(每个请求的生命周期)内保持活动状态。另一方面,对于像您所描述的那样的富客户端,使其长时间保持活动状态可能会更有效(单例生命周期)。
通过让 DI 容器管理对象上下文的生命周期,您不必将自己束缚于一种特定的选择。
Unit of Work is not the same as only keep your datacontext in existence for one unit of work.
Unit of Work is a design pattern that describe how to represent transactions in an abstract way. It's really only required for Create, Update and Delete (CUD) operations.
One philosophy is that UoW is used for all CUD operations, while read-only Repositories are used for read operations.
In any case I would recommend decoupling object lifetime from UoW or Repository usage. Use Dependency Injection (DI) to inject both into your consuming services, and let the DI Container manage lifetimes of both.
In web applications, my experience is that the object context should only be kept alive for a single request (per-request lifetime). On the other hand, for a rich client like the one you describe, keeping it alive for a long time may be more efficient (singleton lifetime).
By letting a DI Container manage the lifetime of the object context, you don't tie yourself to one specific choice.
好的。
不。这些类是数据库映射类。它们不是 UI 演示类。
Ok.
No. Those classes are database mapping classes. They are not UI presentation classes.