Hi!
How to fill TODO to make this test pass?
class MyClass { }
[Test]
public void Singleton_by_default_test()
{
var parentContainer = GetUnityContainer();
var container = parentContainer.GetChildContainer();
// TODO: Add magic here (but do NOT explicitly register MyClass in container!)
Assert.AreSame(container.Resolve<MyClass>(), container.Resolve<MyClass>());
}
Update: There is a way that uses inheritance.
public class SingletonContainer : UnityContainer
{
public override object Resolve(Type t, string name)
{
var obj = base.Resolve(t, name);
RegisterInstance(t, name, obj, new ContainerControlledLifetimeManager());
return obj;
}
}
I am using container.GetChildContainer() to get container instance so this method does not suite me.