实体框架的ObjectContext在windows / WPF / Silverlight应用程序应用程序、实体、框架、windows

2023-09-04 02:15:12 作者:分镜头.

我们正在编写使用实体框架WPF应用程序(与Silverlight的RIA服务是precise)。我们使用一个共享的ObjectContext通过应用使我们可以从各个模块之间共享的数据中受益。

We are writing a WPF application using Entity framework (Silverlight with RIA services to be precise). 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. So another pattern should be used.

我知道ObjectContexts应作为单位-的-工作单。但后来,你如何让应用程序的其他部分知道的东西已经改变,他们应该重新载入他们的数据?

I know that ObjectContexts should be used as single Unit-of-Work. But then, how do you let other parts of the application know that something has changed and they should reload their data?

编辑:好了,EventAggregator,但随后,这将导致所有其他部分重新加载它们的(可能大部分是重复的)数据。另外可能很多情况下,将需要的entites组的所有类型。

Ok, EventAggregator, but then, this would cause all other parts to reload their (probably much of it duplicate) data. Also probably many event would be needed for all the types of entites groups.

你怎么解决这些问题?我的当前的解决方案是一种妥协 - 使用一个共享的ObjectContext所使用的整个appliaction核心数据,以便它们可以共享和自动更新。而对于大量数据,使用一个新的独立的ObjectContext。更好的想法?

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. And for the large amount of data, use a new separate ObjectContext. Any better ideas?

有没有办法如何释放,从他们的DataContext的实体,从而使垃圾收集器完成其工作,并释放内存?

Is there a way how to "release" entities from their DataContext so that Garbage collector can do its job and release the memory?

推荐答案

等等,它是WPF或Silverlight?在这种情况下,他们是非常不同的,我的答案是不同的。

Wait, is it WPF or Silverlight? In this case, they are very different and my answer would be different.

WPF的解决方案

在WPF中我将创建每个表单一个单一的ObjectContext 。这种方式,上下文只会持续只要形式本身。然后,您应该实现一个事件系统,以便当您将更改保存到一个实体可以提醒其他形式,他们可能需要刷新自己的数据(的 INotifyPropertyChanged的,例如)。 奥伦Eini写了pretty的好文章在MSDN杂志上使用NHibernate这种架构。你应该能够从他的文章的架构概念。

In WPF I would create a single ObjectContext per form. 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). Oren Eini wrote a pretty good article on this architecture using NHibernate in MSDN magazine. You should be able to get the architecture concept from his article.

的Silverlight解决方案

现在,Silverlight是不同的。 Silverlight的本质只允许你在你的应用程序的一种形式。是的,有一些技巧,你可以做导航根的视觉形式,以不同的页面,但它仍然只是一种形式,用户无法在一个Silverlight的RIA打开多个窗口。正因为如此,我会做一个净RIA服务ObjectContext的每Silverlight的RIA实例。请记住,RIA服务是不是你的数据库的实际连接,它只是一个缓存和变更跟踪连接到Web服务对象。因此,它是完全可以接受的离开中实存此对象为更长的时间,因为它不占用任何服务器资源。如果您的Silverlight RIA打开多个浏览器窗口或有多个Silverlight的对象,那么你应该有每个Silverlight的实例一个ObjectContext的。

Now, Silverlight is different. Silverlight essentially only allows you to have one form in your application. 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. Because of this, I would make one .Net RIA Services ObjectContext per Silverlight RIA instance. 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. 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.

在服务器上,您使用的实体框架的ObjectContext中的Web服务,它应该只能活一个请求的持续时间。更无状态的,你可以让你的服务,更多的可扩展性和高性能的他们将。要打开您的EF 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. 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.

编辑:

如果所有你想要做的是从对象上下文分离的对象,那么你可以使用 context.Detach(实体)方法。你可以找到如何在执行此操作的例子MSDN 。

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. You can find an example of how to do this on MSDN.