public void RegisterInstanceCanRegisterAStruct() { // Struct / ValueType ? using (var container = new IocContainer()) { int number = 32; var reg1 = container.RegisterInstance(typeof(int), number); var reg2 = container.RegisterInstance(number, "key"); // Check registrations Assert.IsInstanceOfType(reg1, typeof(InstanceRegistration<int>)); Assert.IsInstanceOfType(reg2, typeof(InstanceRegistration<int>)); Assert.AreSame(reg1.ReturnType, typeof(int)); Assert.AreSame(reg2.ReturnType, typeof(int)); // Check index Assert.IsTrue(container.Index.Contains(typeof(int))); Assert.IsTrue(container.Index.Contains(typeof(int), "key")); // Try to resolve var result1 = (int)container.Resolve(typeof(int)); var result2 = container.Resolve<int>("key"); Assert.AreEqual(number, result1); Assert.AreEqual(number, result2); Assert.AreEqual(result1, result2); } }
public void TryResolveAllReturnsExpectedInstances() { using (var container = new IocContainer()) { // Arrange var foo1 = new Foo1(); var foo2 = new Foo2(); var foo3 = new Foo2(); var bar1 = new Bar1(); container.RegisterInstance<IFoo>(foo1); container.RegisterInstance<IFoo>("Foo1", foo2); container.RegisterInstance<IFoo>("Foo2", foo3); container.RegisterInstance<IBar>(bar1); // Act var results = container.TryResolveAll<IFoo>(); var resultList = results.ToList(); // Assert Assert.IsTrue(results.Count() == 3); CollectionAssert.Contains(resultList, foo1); CollectionAssert.Contains(resultList, foo2); CollectionAssert.Contains(resultList, foo3); } }
public void RegisterInstanceGenericUsingKeyThatAlreadyExistsThrowsException() { using (var container = new IocContainer()) { container.RegisterInstance<IFoo>("Bar", new Foo1()); container.RegisterInstance<IFoo>("Bar", new Foo2()); } }
public void RegisterInstanceByTypeThrowsExceptionIfInstanceNotOfExpectedType() { using (var container = new IocContainer()) { var fooInstance = new Foo1(); var result = container.RegisterInstance(typeof(IBar), fooInstance); } }
private void RegisterDatabase(IocContainer container) { SterlingEngine engine = new SterlingEngine(); engine.Activate(); container.RegisterInstance<Domain.Database>(engine.SterlingDatabase.RegisterDatabase<Domain.Database>(new FileSystemDriver()) as Domain.Database); container.Register<Domain.Db.ITestQuery, Domain.Db.TestQuery>(); container.Register<Domain.Db.ISubjectQuery, Domain.Db.SubjectQuery>(); }
public void TryResolveAllByTypeNotRegisteredReturnsEmptyEnumerable() { using (var container = new IocContainer()) { // Arrange var foo1 = new Foo1(); var foo2 = new Foo2(); var bar1 = new Bar1(); container.RegisterInstance<IFoo>(foo1); container.RegisterInstance<IFoo>("Foo", foo2); container.RegisterInstance<IBar>(bar1); // Act var results = container.TryResolveAll<IFooBar>(); // Assert Assert.IsTrue(results.Count() == 0); } }
public void CompileCanInlineInstanceRegistrations() { // Doesnt check that Compile actually works - just that it doesnt break anything using (var container = new IocContainer()) { var i1 = new Foo1(); var i2 = new Bar1(); container.RegisterInstance(i1); container.RegisterInstance(i2); var reg1 = container.Register<IFooBar>(x => new FooBar(x.Resolve<Foo1>(), x.Resolve<Bar1>())); container.Compile(); var result = (IFooBar)container.Resolve(reg1); Assert.AreEqual(i1, result.Foo); } }
public void RegisterInstanceGenericUsingKeyReturnsCorrectType() { using (var container = new IocContainer()) { var fooInstance = new Foo1(); var result = container.RegisterInstance<IFoo>("Bar", fooInstance); Assert.IsInstanceOfType(result, typeof(IRegistration)); Assert.IsInstanceOfType(result, typeof(InstanceRegistration)); Assert.AreSame(result.Type, typeof(IFoo)); Assert.AreEqual(result.Key, "Bar"); } }
public void ResolveCanResolveCollection() { var foo1 = new Foo1(); var foo2 = new Foo2(); var foo3 = new Foo1(); IList<IFoo> list = new List<IFoo>(new IFoo[] { foo1, foo2, foo3 }); // Register Instance using (var container = new IocContainer()) { container.RegisterInstance(typeof(IEnumerable<IFoo>), list); container.RegisterInstance(typeof(IEnumerable<IFoo>), list, "TheKey"); AssertResolveCanResolveIEnumerableType(container); AssertResolveCanResolveIEnumerableType(container, "TheKey"); } // RegisterInstance - Generic using (var container = new IocContainer()) { container.RegisterInstance<IEnumerable<IFoo>>(list); container.RegisterInstance<IEnumerable<IFoo>>(list, "TheKey"); AssertResolveCanResolveIEnumerableType(container); AssertResolveCanResolveIEnumerableType(container, "TheKey"); } // Register using (var container = new IocContainer()) { container.Register<IEnumerable<IFoo>>(x => list); container.Register<IEnumerable<IFoo>>(x => list, "TheKey"); AssertResolveCanResolveIEnumerableType(container); AssertResolveCanResolveIEnumerableType(container, "TheKey"); } }
public void RegisterInstanceReturnsCorrectType() { using (var container = new IocContainer()) { var fooInstance = new Foo1(); var result = container.RegisterInstance(typeof(IFoo), fooInstance); Assert.IsInstanceOfType(result, typeof(InstanceRegistration<IFoo>)); Assert.AreSame(result.ReturnType, typeof(IFoo)); // Check index Assert.IsTrue(container.Index.Contains(typeof(IFoo))); } }
public void RegisterInstanceGenericUsingKeyReturnsCorrectType() { using (var container = new IocContainer()) { var fooInstance = new Foo1(); var reg = container.RegisterInstance<IFoo>(fooInstance, "Bar"); Assert.IsInstanceOfType(reg, typeof(InstanceRegistration<IFoo>)); Assert.AreSame(reg.ReturnType, typeof(IFoo)); // Check index Assert.IsTrue(container.Index.Contains(typeof(IFoo), "Bar")); } }
public Bootstrapper() { _container = new IocContainer(); InitialiseLogger(); InitialiseDatabase(); InitialiseValidators(); InitialiseViewModels(); _container.RegisterInstance<IResolver>(_container); ServiceLocator.SetLocatorProvider(() => new DynamoServiceLocator(_container)); App.Current.Exit += new System.Windows.ExitEventHandler(TearDown); }
/// <summary> /// Invoked when the application is launched normally by the end user. Other entry points /// will be used when the application is launched to open a specific file, to display /// search results, and so forth. /// </summary> /// <param server="args">Details about the launch request and process.</param> protected async override void OnLaunched(LaunchActivatedEventArgs args) { var container = new IocContainer(); container.RegisterInstance(() => new ObservableLoggingMechanism(new MainThreadDispatcher()) { ShowInfo = true, ShowDebug = true }); container.RegisterFromAssemblyOf<ResponseHandler>(); Configure.Messaging() .LoggingWith(container.Resolve<ObservableLoggingMechanism>()) .ResolveReferencesWith(container) .RegisterHandlersFromContainer().BasedOn<IMessageConsumer>() .UsingFilePersistence() .UsingHttpTransport() .AsAServerUsingAProxy("SenderServer") .OpenChannel("TestMetroRequest") .ForRequestReplySendingTo("TestReply@ReceiverServer") .HandleRepliesOnMainThread() .WithDurability() .Sequenced() .Initialise(); ViewModelLocator.SetContainer(container); // Do not repeat app initialization when already running, just ensure that // the window is active if (args.PreviousExecutionState == ApplicationExecutionState.Running) { Window.Current.Activate(); return; } if (args.PreviousExecutionState == ApplicationExecutionState.Terminated) { //TODO: Load state from previously suspended application } // Create a Frame to act navigation context and navigate to the first page var rootFrame = new Frame(); if (!rootFrame.Navigate(typeof(MainPage))) { throw new Exception("Failed to create initial page"); } // Place the frame in the current Window and ensure that it is active Window.Current.Content = rootFrame; Window.Current.Activate(); }
public void TryResolveByNameReturnsExpectedInstance() { using (var container = new IocContainer()) { // Arrange var foo = new Foo1(); container.RegisterInstance<IFoo>("Foo", foo); // Act IFoo obj; var result = container.TryResolve<IFoo>("Foo", out obj); // Assert Assert.AreSame(obj, foo); Assert.IsTrue(result); } }
public void RegisterInstanceCanRegisterAStruct() { using (var container = new IocContainer()) { int number = 32; var registration = container.RegisterInstance(typeof(int), number); // Check registration Assert.IsInstanceOfType(registration, typeof(IRegistration)); Assert.AreSame(registration.Type, typeof(int)); Assert.AreEqual(registration.Key, null); // Try to resolve var result = container.Resolve(typeof(int)); Assert.AreEqual(number, result); } }
public void CompileUsingKeyDoesntBreakUsingCompileModeDynamicWhenUsingInstanceRegistrationOrContainerLifetime() { // Test that InstanceRegistration and ContainerLifetime works using CompileMode Dynamic (should not be inlined) using (var container = new IocContainer(defaultCompileMode: CompileMode.Dynamic)) { var barInstance = new Bar1(); var reg1 = container.Register<IFoo, Foo1>("FirstKey").WithContainerLifetime(); var reg2 = container.RegisterInstance<IBar>(barInstance, "SecondKey"); var reg3 = container.Register<IFooBar>(x => new FooBar(x.Resolve<IFoo>("FirstKey"), x.Resolve<IBar>("SecondKey"))); container.Compile(); var res1 = container.Resolve<IFooBar>(); var res2 = container.Resolve<IFooBar>(); // Assert Assert.AreNotSame(res1, res2); Assert.AreSame(barInstance, res1.Bar); Assert.AreSame(res1.Foo, res2.Foo); } }
public void NonGenericNamelessInstanceRegistrationReturnsRegistrationObject() { using (var iocContainer = new IocContainer()) { var fooInstance = new Foo1(); var result = iocContainer.RegisterInstance(typeof(IFoo), fooInstance); Verify.That(result).IsAnInstanceOfType(typeof(IRegistration)); Verify.That(result.Name).IsNull(); } }
public void GenericNamedInstanceRegistrationReturnsRegistrationObject() { using (var iocContainer = new IocContainer()) { var fooInstance = new Foo1(); var result = iocContainer.RegisterInstance("Bob", fooInstance); Verify.That(result).IsAnInstanceOfType(typeof(IRegistration)); Verify.That(result.Name).IsAStringThat().IsEqualTo("Bob"); } }
static void CustomRegistrations(IocContainer ioc) { ioc.RegisterInstance<ICustomInterface, CustomType>(); }
public void CompileWorksWhenCompiledMultipleTimes() { using (var container = new IocContainer()) { // Arrange var foo = new Foo1(); var bar = new Bar1(); container.RegisterInstance<IFoo>(foo); container.RegisterInstance<IBar>(bar); container.Register<IFooBar, FooBar>().WithContainerLifetime(); container.Register<IFooBarContainer, FooBarContainer>().WithContainerLifetime(); // Act container.Compile(); container.Compile(); var fooBarContainer = container.Resolve<IFooBarContainer>(); // Assert Assert.IsNotNull(fooBarContainer); Assert.IsNotNull(fooBarContainer.FooBar); Assert.IsNotNull(fooBarContainer.FooBar.Foo); Assert.IsNotNull(fooBarContainer.FooBar.Bar); Assert.AreEqual(foo, fooBarContainer.FooBar.Foo); Assert.AreEqual(bar, fooBarContainer.FooBar.Bar); } }