public void TestSingleton() { var container = new CryoContainer(); container.RegisterSingleton <MainClass>(); container.RegisterAlias <IFirstInterface, MainClass>(); container.RegisterAlias <ISecondInterface, MainClass>(); var firstInterface = container.Resolve <IFirstInterface>(); var secondInterface = container.Resolve <ISecondInterface>(); var mainClass = container.Resolve <MainClass>(); Assert.AreSame(mainClass, firstInterface); Assert.AreSame(mainClass, secondInterface); }
public void TestInstance() { var instance = new MainClass(); var container = new CryoContainer(); container.RegisterInstance(instance); container.RegisterAlias <IFirstInterface, MainClass>(); container.RegisterAlias <ISecondInterface, MainClass>(); var firstInterface = container.Resolve <IFirstInterface>(); var secondInterface = container.Resolve <ISecondInterface>(); Assert.AreSame(instance, firstInterface); Assert.AreSame(instance, secondInterface); }
public void TestNamedSingleton() { var container = new CryoContainer(); container.RegisterSingleton <MainClass>("MainClass"); container.RegisterAlias <IFirstInterface, MainClass>(null, "MainClass"); container.RegisterAlias <ISecondInterface, MainClass>(); var firstInterface = container.Resolve <IFirstInterface>(); var mainClass = container.ResolveByName <MainClass>("MainClass"); Assert.AreSame(mainClass, firstInterface); try { container.Resolve <ISecondInterface>(); Assert.Fail("Expected CryoDI.ContainerException : Can't resolve type CryoDI.Tests.MainClass"); } catch (ContainerException ex) { // expected CryoDI.ContainerException : Can't resolve type CryoDI.Tests.MainClass } }