简体   繁体   English

Windows / WPF / Silverlight应用程序中的实体框架ObjectContext

[英]Entity Framework ObjectContext in windows/WPF/Silverlight application

We are writing a WPF application using Entity framework (Silverlight with RIA services to be precise). 我们正在使用Entity框架编写WPF应用程序(准确地说,Silverlight与RIA服务)。 We're using a shared ObjectContext through the application so that we can benefit from sharing data across the modules. 我们通过应用程序使用共享的ObjectContext,这样我们就可以从跨模块共享数据中受益。

The problem is - if user during his work opens let's say historical sales, it gets loaded to the ObjectContext and stays there until the end of the application. 问题是 - 如果用户在工作期间打开让我们说历史销售,它会被加载到ObjectContext并保持到应用程序结束。 So another pattern should be used. 所以应该使用另一种模式。

I know that ObjectContexts should be used as single Unit-of-Work. 我知道ObjectContexts应该用作单个工作单元。 But then, how do you let other parts of the application know that something has changed and they should reload their data? 但是,您如何让应用程序的其他部分知道某些内容已发生变化并且应该重新加载数据?

Edit: Ok, EventAggregator, but then, this would cause all other parts to reload their (probably much of it duplicate) data. 编辑:好的,EventAggregator,但是,这将导致所有其他部分重新加载他们的(可能大部分重复)数据。 Also probably many event would be needed for all the types of entites groups. 也许所有类型的参与团体都需要许多活动。

How do you solve these problems? 你是如何解决这些问题的? My current solution is a kind of compromise - use a shared ObjectContext for the core data used by whole appliaction so that they can be shared and updated automatically. 我当前的解决方案是一种妥协 - 使用共享的ObjectContext作为整个应用程序使用的核心数据,以便它们可以自动共享和更新。 And for the large amount of data, use a new separate ObjectContext. 对于大量数据,请使用新的单独ObjectContext。 Any better ideas? 有更好的想法吗?

Is there a way how to "release" entities from their DataContext so that Garbage collector can do its job and release the memory? 有没有办法如何从DataContext中“释放”实体,以便垃圾收集器可以完成其工作并释放内存?

Wait, is it WPF or Silverlight? 等等,是WPF还是Silverlight? In this case, they are very different and my answer would be different. 在这种情况下,它们是非常不同的,我的答案会有所不同。

WPF Solution WPF解决方案

In WPF I would create a single ObjectContext per form . 在WPF中,我将为每个表单创建一个ObjectContext This way, the context will only last as long as the form itself. 这样,上下文只会持续与表单本身一样长。 You should then implement an event system so that when you save changes to an entity you can alert the other forms that they may need to refresh their data ( INotifyPropertyChanged , for example). 然后,您应该实现一个事件系统,以便在保存对实体的更改时,可以提醒他们刷新数据所需的其他表单(例如, INotifyPropertyChanged )。 Oren Eini wrote a pretty good article on this architecture using NHibernate in MSDN magazine . Oren Eini在MSDN杂志中使用NHibernate在这个架构上写了一篇非常好的文章 You should be able to get the architecture concept from his article. 您应该能够从他的文章中获得架构概念。

Silverlight Solution Silverlight解决方案

Now, Silverlight is different. 现在,Silverlight与众不同。 Silverlight essentially only allows you to have one form in your application. Silverlight基本上只允许您在应用程序中使用一个表单。 Yes, there are some tricks you can do to navigate the root visual of the form to different "pages" but it is still only one form and the user can't open multiple windows in one Silverlight RIA. 是的,您可以使用一些技巧将表单的根视觉导航到不同的“页面”,但它仍然只是一种形式,用户无法在一个Silverlight RIA中打开多个窗口。 Because of this, I would make one .Net RIA Services ObjectContext per Silverlight RIA instance . 因此, 我会根据Silverlight RIA实例制作一个.Net RIA Services ObjectContext Remember, RIA Services is not an actual connection to your database, it is just a caching and change tracking object linked to a web service. 请记住,RIA Services不是与数据库的实际连接,它只是链接到Web服务的缓存和更改跟踪对象。 So, it is perfectly acceptable to leave this object in existance for longer periods of time because it is not tying up any server resources. 因此,将此对象保留较长时间是完全可以接受的,因为它不会占用任何服务器资源。 If your Silverlight RIA opens multiple browser windows or has more than one Silverlight object, then you should have one ObjectContext per Silverlight instance. 如果您的Silverlight RIA打开多个浏览器窗口或具有多个Silverlight对象,那么每个Silverlight实例应该有一个ObjectContext。

On the server, you use an Entity Framework ObjectContext in the web service and it should only live for the duration of one request . 在服务器上,您在Web服务中使用实体框架ObjectContext,它应该仅在一个请求的持续时间内存在 The more stateless you can make your services, the more scalable and performant they will be. 您可以提供服务的无国籍人越多,他们的可扩展性和性能就越高。 You want to open your EF ObjectContext, use it, and close it as soon as possible. 您想要打开EF ObjectContext,使用它,并尽快关闭它。


EDIT: 编辑:

If all you are wanting to do is detach an object from the object context, then you can just use the context.Detach(entity) method. 如果你想要做的就是从对象上下文中分离一个对象,那么你可以使用context.Detach(entity)方法。 You can find an example of how to do this on MSDN . 您可以在MSDN上找到如何执行此操作示例

You could use repository pattern. 您可以使用存储库模式。 An additional layer of abstraction between UI and DAL. UI和DAL之间的另一个抽象层。

Make Data collections in repository static and observable. 使存储库中的数据集合静态和可观察。 Then whenever repository updates any of them, UI layer should catch up. 然后每当存储库更新其中的任何一个时,UI层应该赶上。 JUst an idea. JUst一个想法。

Use ObservableCollections in the ObjectContext. 在ObjectContext中使用ObservableCollections。 Use an event that triggers on NotifyPropertyChange. 使用在NotifyPropertyChange上触发的事件。 Use a publish/subscribe pattern between the view models to inform them of the change and use this to update the other views. 在视图模型之间使用发布/订阅模式以通知它们更改并使用它来更新其他视图。

In our case we have decide to implement a factory pattern and instantiate a new object context whenever we need it (sometimes it does not map one to one to a VM). 在我们的例子中,我们决定实现工厂模式,并在需要时实例化一个新的对象上下文(有时它不会一对一地映射到VM)。

Agreat article about object context life time: http://www.silverlightshow.net/items/Silverlight-WCF-RIA-Services-strategies-for-handling-your-Domain-Context-part-one.aspx 关于对象上下文生命时间的Agreat文章: http//www.silverlightshow.net/items/Silverlight-WCF-RIA-Services-strategies-for-handling-your-Domain-Context-part-one.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM