When should ContentManager.Flush() be used?

Topics: Customizing Orchard, Writing modules
Jun 28, 2011 at 5:08 PM

I have been trying to find out a rule on when to call ContentManager.Flush(). I tried to find some hints in the Contents controller (Orchard.Core.Contents.Controllers.AdminController), but I couldn't find a recurring pattern. For example, the Publish and Unpublish actions both call Flush(), whereas Edit and Remove don't. Is there a reason for this discrepancy? Even better, is there a set of rules to follow?

Coordinator
Jun 28, 2011 at 6:27 PM

You might use the IRepository directly, and in a second step, during the same request, want to do a Query which might include the previous changes. If you don't do a Flush, you may miss the changes.

Jun 28, 2011 at 10:44 PM

@sebastienros: Thanks, but it still isn't clear to me. Do you mean that the ContentManager is automatically flushed at the end of each request?

Jun 29, 2011 at 5:25 AM

NHibernates Session object is a Unit of Work, it is initialized at the start of the request and flushed at the end of the request. ContentManager uses IRepository whose implementation in turn relies on Session, so yes, it should be automatically flushed after each request.

Coordinator
Jun 29, 2011 at 5:29 AM

True, it's flushed automatically at the end of each request, and the Flush method might be useful if you need to flush before the end.

Jun 30, 2011 at 10:10 AM

Thanks Sebastien; it's much clearer now. But why does the Contents controller (Orchard.Core.Contents.Controllers.AdminController) do a flush in the Publish and Unpublish actions, but not in the Remove action?

Jun 30, 2011 at 12:01 PM

I don't think it's such a big deal. Let's say you still have some work to do after you've removed a content item. If you flush it, it won't be available to other handles. That's why I think it's the way you're seeing it.