It’s now the second time that I did this mistake, so I mainly write this down here, to remember myself, not to make the same mistake again.
StructureMap allows to define a plugin family to be singleton. e.g.:
[PluginFamily("ApplicationShell", IsSingleton = true)]
public interface IApplicationShell
{
...
}
[Pluggable("ApplicationShell")]
public class MainForm: Form, IApplicationShell
{
...
}
So when requesting an instance of IApplicationShell from StructureMap, it will return an instance of MainForm. And it will return the same instance every time, so that only one single instance of MainForm exists - a Singleton.
But now add another interface:
[PluginFamily("ApplicationShell", IsSingleton = true)
public interface IDockingManager
{
...
}
[Pluggable("ApplicationShell")]
public class MainForm: Form, IApplicationShell, IDockingManager
{
...
}
Without thinking to much about this, you might assume, that creating an IApplicationShell and an IDockingManager now might return the same singleton MainForm instance. But: Nope! This will create two different instances of MainForm. Good bye Singleton!
StructureMap keeps track of singletons per PluginFamily / interface, not for the implementation of interfaces. In such case it probably would be nice to be able to configure MainForm as a Singleton and not the interface(s) it implements. I think this isn’t possible by configuration, but it can easily be done in code like this:
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
// create single instance of MainForm
MainForm form = ObjectFactory.GetInstance<MainForm>();
// always resolve IApplicationShell to this instance
ObjectFactory.Inject<IApplicationShell>(form);
// always resolve IDockingManager to this instance
ObjectFactory.Inject<IDockingManager>(form);
Application.Run(form);
}




