Skip to content

Instantly share code, notes, and snippets.

@mcliment
Last active August 29, 2015 14:16
Show Gist options
  • Save mcliment/0fb5106a3ae2dfc60f86 to your computer and use it in GitHub Desktop.
Save mcliment/0fb5106a3ae2dfc60f86 to your computer and use it in GitHub Desktop.
Redefine singletons in StructureMap nested containers

This simple program just demonstrates that you can even redefine singletons in nested containers and they are destroyed when the nested container is disposed.

It just prints the HashCode of the instances twice -to ensure that they are defined as singletons-.

But this only works in StructureMap 2.6.x -actually because of a design flaw-, in 3.x branch throws a InvalidOperationException with the message:

Additional information: Only registrations of the default Transient, UniquePerRequest, and prebuilt objects are valid for nested containers. Remember that 'Transient' instances will be built once per nested container. If you need this functionality, try using a     Child/Profile container instead

NestedContainerSingletons.Singleton or plugin type NestedContainerSingletons.ISingleton has lifecycle Singleton

For this reason, the version for StructureMap 3.x uses profiles and redefines the singleton instance as a transient for a particular profile.

using System;
using StructureMap;
namespace NestedContainerSingletons
{
class Program
{
static void Main(string[] args)
{
var c = new Container(_ =>
_.For<ISingleton>().Singleton().Use<Singleton>());
c.GetInstance<ISingleton>().Look();
c.GetInstance<ISingleton>().Look();
using (var c2 = c.GetNestedContainer())
{
c2.Configure(_ => _.For<ISingleton>().Singleton().Use<Singleton>());
c2.GetInstance<ISingleton>().Look();
c2.GetInstance<ISingleton>().Look();
}
c.GetInstance<ISingleton>().Look();
c.GetInstance<ISingleton>().Look();
Console.ReadKey();
}
}
public interface ISingleton
{
void Look();
}
public class Singleton : ISingleton
{
public void Look()
{
Console.WriteLine("This is " + this.GetHashCode());
}
}
}
using System;
using StructureMap;
namespace NestedContainerSingletons
{
class Program
{
static void Main(string[] args)
{
var c = new Container(_ =>
{
_.For<ISingleton>().Singleton().Use<Singleton>();
_.Profile("TestProfile", r =>
{
r.For<ISingleton>().Transient().Use<Singleton>();
});
});
c.GetInstance<ISingleton>().Look();
c.GetInstance<ISingleton>().Look();
using (var c2 = c.GetNestedContainer("TestProfile"))
{
c2.GetInstance<ISingleton>().Look();
c2.GetInstance<ISingleton>().Look();
}
using (var c3 = c.GetNestedContainer("TestProfile"))
{
c3.GetInstance<ISingleton>().Look();
c3.GetInstance<ISingleton>().Look();
}
c.GetInstance<ISingleton>().Look();
c.GetInstance<ISingleton>().Look();
Console.ReadKey();
}
}
public interface ISingleton
{
void Look();
}
public class Singleton : ISingleton
{
public void Look()
{
Console.WriteLine("This is " + this.GetHashCode());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment