How do I use autofac to resolve a single instance of a (non-Modal) form until that form is disposed?

I am writing a system tray utility and from the menu there are several different forms that you can open for the application. I am using autofac to resolve the creation of these forms when necessary giving my main form Func and Func dependencies.

If the user selects an option that activates the form, if it is already shown it should receive focus, otherwise autofac should create a new form.

I don't really want these forms sitting in memory while they aren't in use, so I am currently letting the form Dispose of itself when the user closes it.

What I need to know is how I can notify autofac that the form has been disposed so that:

  1. It no longer contains a reference to the disposed form (and can thus be garbage collected)
  2. When I next request autofac for an instance of the form it creates a new one.

I've been reading the Autofac wiki and I'm guessing that I just need to set the LifetimeScope properly.

5
задан Rhys Parry 18 May 2011 в 20:07
поделиться