コード例 #1
0
		public void MemberBasedBindingOfTypeDependencies()
		{
			IModule module = new InlineModule(
				m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Member.Tag.EqualTo("A")),
				m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Member.Tag.EqualTo("B")),
				m => m.Bind(typeof(IMock)).To(typeof(ImplC)).Only(!When.Context.Member.Tag.EqualTo("A") & !When.Context.Member.Tag.EqualTo("B"))
			);

			using (var kernel = new StandardKernel(module))
			{
				var obj1 = kernel.Get<RequestsTagA>();
				Assert.That(obj1, Is.Not.Null);
				Assert.That(obj1.Child, Is.Not.Null);
				Assert.That(obj1.Child, Is.InstanceOfType(typeof(ImplA)));

				var obj2 = kernel.Get<RequestsTagB>();
				Assert.That(obj2, Is.Not.Null);
				Assert.That(obj2.Child, Is.Not.Null);
				Assert.That(obj2.Child, Is.InstanceOfType(typeof(ImplB)));

				var obj3 = kernel.Get<RequestsNoTag>();
				Assert.That(obj3, Is.Not.Null);
				Assert.That(obj3.Child, Is.Not.Null);
				Assert.That(obj3.Child, Is.InstanceOfType(typeof(ImplC)));
			}
		}
コード例 #2
0
		public void ManyPublishersOneSubscriber()
		{
			using (var kernel = new StandardKernel(new MessageBrokerModule()))
			{
				PublisherMock pub1 = kernel.Get<PublisherMock>();
				PublisherMock pub2 = kernel.Get<PublisherMock>();
				Assert.That(pub1, Is.Not.Null);
				Assert.That(pub2, Is.Not.Null);

				SubscriberMock sub = kernel.Get<SubscriberMock>();
				Assert.That(sub, Is.Not.Null);

				Assert.That(pub1.HasListeners);
				Assert.That(pub2.HasListeners);
				Assert.That(sub.LastMessage, Is.Null);

				pub1.SendMessage("Hello, world!");
				Assert.That(sub.LastMessage, Is.EqualTo("Hello, world!"));

				sub.LastMessage = null;
				Assert.That(sub.LastMessage, Is.Null);

				pub2.SendMessage("Hello, world!");
				Assert.That(sub.LastMessage, Is.EqualTo("Hello, world!"));
			}
		}
コード例 #3
0
		public void OneInstanceCreatedForSingletonTypes()
		{
			using (var kernel = new StandardKernel())
			{
				var mock1 = kernel.Get<ObjectWithSingletonBehavior>();
				var mock2 = kernel.Get<ObjectWithSingletonBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.SameAs(mock2));
			}
		}
コード例 #4
0
		public void ManyInstancesCreatedForTransientTypes()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithTransientBehavior mock1 = kernel.Get<ObjectWithTransientBehavior>();
				ObjectWithTransientBehavior mock2 = kernel.Get<ObjectWithTransientBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.Not.SameAs(mock2));
			}
		}
コード例 #5
0
		public void BehaviorDefaultsToTransient()
		{
			using (var kernel = new StandardKernel())
			{
				SimpleObject mock1 = kernel.Get<SimpleObject>();
				SimpleObject mock2 = kernel.Get<SimpleObject>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.Not.SameAs(mock2));
			}
		}
コード例 #6
0
		public void SameInstanceReusedOnSameThreadForOnePerThreadTypes()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithOnePerThreadBehavior mock1 = kernel.Get<ObjectWithOnePerThreadBehavior>();
				ObjectWithOnePerThreadBehavior mock2 = kernel.Get<ObjectWithOnePerThreadBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.SameAs(mock2));
			}
		}
コード例 #7
0
		public void CustomBehaviorProviderUsed()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithCustomBehavior mock1 = kernel.Get<ObjectWithCustomBehavior>();
				ObjectWithCustomBehavior mock2 = kernel.Get<ObjectWithCustomBehavior>();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.SameAs(mock2));
			}
		}
コード例 #8
0
		public void KernelLoadsModulesAndAppliesTheirBindings()
		{
			using (var kernel = new StandardKernel(new MockModuleA(), new MockModuleB()))
			{
				var one = kernel.Get<IServiceA>();
				Assert.That(one, Is.Not.Null);
				Assert.That(one, Is.InstanceOfType(typeof(ServiceImplA)));

				var two = kernel.Get<IServiceB>();
				Assert.That(two, Is.Not.Null);
				Assert.That(two, Is.InstanceOfType(typeof(ServiceImplB)));
			}
		}
コード例 #9
0
		public void InstancesOfTransietnServicesAreNotTracked()
		{
			using (var kernel = new StandardKernel())
			{
				var obj1 = kernel.Get<ObjectWithTransientBehavior>();
				Assert.That(obj1, Is.Not.Null);

				var obj2 = kernel.Get<ObjectWithTransientBehavior>();
				Assert.That(obj2, Is.Not.Null);

				var scope = kernel.Components.Tracker.GetScope(kernel);
				Assert.That(scope.Count, Is.EqualTo(0));
			}
		}
コード例 #10
0
		public void CanActivateGenericTypeViaGenericTypeDefinitionBindingWithSingletonBehavior()
		{
			var module = new InlineModule(m => m.Bind(typeof(IGenericObject<>)).To(typeof(GenericImpl<>)).Using<SingletonBehavior>());

			using (var kernel = new StandardKernel(module))
			{
				var mock1 = kernel.Get<IGenericObject<string>>();
				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock1, Is.InstanceOfType(typeof(GenericImpl<string>)));

				var mock2 = kernel.Get<IGenericObject<int>>();
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock2, Is.InstanceOfType(typeof(GenericImpl<int>)));
			}
		}
コード例 #11
0
ファイル: CacheFixture.cs プロジェクト: jamarlthomas/ninject1
		public void ResultOfMethodCallWithSameArgumentsIsCached()
		{
			IModule testModule = new InlineModule(m => m.Bind<CacheMock>().ToSelf());

			using (var kernel = new StandardKernel(new LinFuModule(), new CacheModule(), testModule))
			{
				var obj = kernel.Get<CacheMock>();

				CacheMock.ResetCounts();

				int result;

				result = obj.Multiply(2, 3);
				Assert.That(result, Is.EqualTo(6));
				Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

				result = obj.Multiply(2, 3);
				Assert.That(result, Is.EqualTo(6));
				Assert.That(CacheMock.MultiplyCount, Is.EqualTo(1));

				result = obj.Multiply(4, 4);
				Assert.That(result, Is.EqualTo(16));
				Assert.That(CacheMock.MultiplyCount, Is.EqualTo(2));
			}
		}
コード例 #12
0
		public void StaticInterceptorsAreRegisteredFromAttributesDefinedOnMethods()
		{
			var module = new InlineModule(m => m.Bind<ObjectWithMethodInterceptor>().ToSelf());

			using (var kernel = new StandardKernel(module))
			{
				kernel.Components.Connect<IProxyFactory>(new DummyProxyFactory());

				var obj = kernel.Get<ObjectWithMethodInterceptor>();

				IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));

				IRequest request = new StandardRequest(
					context,
					obj,
					typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
					new object[0],
					Type.EmptyTypes
				);

				ICollection<IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
				IEnumerator<IInterceptor> enumerator = interceptors.GetEnumerator();
				enumerator.MoveNext();

				Assert.That(interceptors.Count, Is.EqualTo(1));
				Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
			}
		}
コード例 #13
0
		public void MemberBasedBindingOfTypeDependencies()
		{
			IModule module = new InlineModule(
				m => m.Bind(typeof(ObjectWithMethodInterceptor)).ToSelf(),
				m => m.Intercept(When.Request.Method.Name.StartsWith("F")).With<CountInterceptor>()
			);

			using (var kernel = new StandardKernel(module))
			{
				kernel.Components.Connect<IProxyFactory>(new DummyProxyFactory());

				var obj = kernel.Get<ObjectWithMethodInterceptor>();

				IContext context = kernel.Components.ContextFactory.Create(typeof(ObjectWithMethodInterceptor));

				IRequest request = new StandardRequest(
					context,
					obj,
					typeof(ObjectWithMethodInterceptor).GetMethod("Foo"),
					new object[0],
					Type.EmptyTypes
				);

				ICollection<IInterceptor> interceptors = kernel.Components.AdviceRegistry.GetInterceptors(request);
				Assert.That(interceptors.Count, Is.EqualTo(2));

				IEnumerator<IInterceptor> enumerator = interceptors.GetEnumerator();

				enumerator.MoveNext();
				Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));

				enumerator.MoveNext();
				Assert.That(enumerator.Current, Is.InstanceOfType(typeof(CountInterceptor)));
			}
		}
コード例 #14
0
		public void CanCallParameterlessConstructor()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<SimpleObject>();
				Assert.That(mock, Is.Not.Null);
			}
		}
コード例 #15
0
		public void NewInstanceCreatedOnDifferentThreadForOnePerThreadTypes()
		{
			using (var kernel = new StandardKernel())
			{
				ObjectWithOnePerThreadBehavior mock1 = kernel.Get<ObjectWithOnePerThreadBehavior>();
				ObjectWithOnePerThreadBehavior mock2 = null;

				Thread thread = new Thread(delegate(object state) { mock2 = kernel.Get<ObjectWithOnePerThreadBehavior>(); });

				thread.Start();
				thread.Join();

				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock1, Is.Not.SameAs(mock2));
			}
		}
コード例 #16
0
		public void StartableObjectIsStartedWhenActivated()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<StartableMock>();
				Assert.That(mock, Is.Not.Null);
				Assert.That(mock.Started, Is.True);
			}
		}
コード例 #17
0
		public void ContextVariablesCanBeUsedToAlterBindingsWithCaseInsensitivity()
		{
			var module = new InlineModule(
				m => m.Bind(typeof(IMock)).To(typeof(ImplA)).Only(When.Context.Variable("bind").EqualTo("Foo", StringComparer.OrdinalIgnoreCase)),
				m => m.Bind(typeof(IMock)).To(typeof(ImplB)).Only(When.Context.Variable("bind").EqualTo("BaR", StringComparer.OrdinalIgnoreCase))
			);

			using (var kernel = new StandardKernel(module))
			{
				var mock1 = kernel.Get<IMock>(With.Parameters.Variable("bind", "FOO"));
				Assert.That(mock1, Is.Not.Null);
				Assert.That(mock1, Is.InstanceOfType(typeof(ImplA)));

				var mock2 = kernel.Get<IMock>(With.Parameters.Variable("bind", "bar"));
				Assert.That(mock2, Is.Not.Null);
				Assert.That(mock2, Is.InstanceOfType(typeof(ImplB)));
			}
		}
コード例 #18
0
		public void BindingsAreRemovedWhenModulesAreUnloaded()
		{
			var moduleA = new MockModuleA();
			var moduleB = new MockModuleB();

			using (var kernel = new StandardKernel(moduleA, moduleB))
			{
				var one = kernel.Get<IServiceA>();
				Assert.That(one, Is.Not.Null);
				Assert.That(one, Is.InstanceOfType(typeof(ServiceImplA)));

				var two = kernel.Get<IServiceB>();
				Assert.That(two, Is.Not.Null);
				Assert.That(two, Is.InstanceOfType(typeof(ServiceImplB)));

				kernel.Unload(moduleA);
				Assert.That(kernel.Components.BindingRegistry.HasBinding(typeof(IServiceA)), Is.False);
			}
		}
コード例 #19
0
ファイル: Program.cs プロジェクト: ms-lilibeth/dotnetconf
        public static void Main()
        {
            IKernel kernel = new StandardKernel(new InlineModule(
                                                    m => m.Bind<IReportBuilder>().To<ReportBuilder>(),
                                                    m => m.Bind<IReportSender>().To<EmailReportSender>(),
                                                    m => m.Bind<IReporter>().To<Reporter>()
                                                    ));

            kernel.Get<IReporter>().SendReports();
        }
コード例 #20
0
		public void OptionalDependenciesForServiceBoundTypesAreIgnoredIfNoBindingsExist()
		{
			using (var kernel = new StandardKernel())
			{
				RequestsOptionalService parent = kernel.Get<RequestsOptionalService>();

				Assert.That(parent, Is.Not.Null);
				Assert.That(parent.Child, Is.Null);
			}
		}
コード例 #21
0
		public void SelfBoundTypeReceivesMethodInjection()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<RequestsMethodInjection>();

				Assert.That(mock, Is.Not.Null);
				Assert.That(mock.Child, Is.Not.Null);
			}
		}
コード例 #22
0
		public void MemberBasedBindingOfConstantDependencies()
		{
			IModule module = new InlineModule(
				m => m.Bind<string>().ToConstant("Hello, world!").Only(When.Context.Member.Tag.EqualTo("HelloWorld")),
				m => m.Bind<string>().ToConstant("SNAFU").Only(When.Context.Member.Tag.EqualTo("FooBar"))
			);

			using (var kernel = new StandardKernel(module))
			{
				var obj1 = kernel.Get<RequestsHelloWorldConstant>();
				Assert.That(obj1, Is.Not.Null);
				Assert.That(obj1.Message, Is.Not.Null);
				Assert.That(obj1.Message, Is.EqualTo("Hello, world!"));

				var obj2 = kernel.Get<RequestsFooBarConstant>();
				Assert.That(obj2, Is.Not.Null);
				Assert.That(obj2.Message, Is.Not.Null);
				Assert.That(obj2.Message, Is.EqualTo("SNAFU"));
			}
		}
コード例 #23
0
		public void ConstructorReceivesInjection()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<RequestsConstructorInjection>();

				Assert.That(mock, Is.Not.Null);
				Assert.That(mock.Child, Is.Not.Null);
				Assert.That(mock.Child, Is.InstanceOfType(typeof(SimpleObject)));
			}
		}
コード例 #24
0
		public void CanInjectKernelInstanceIntoConstructors()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<RequestsKernelViaConstructorInjection>();

				Assert.That(mock, Is.Not.Null);
				Assert.That(mock.Kernel, Is.Not.Null);
				Assert.That(mock.Kernel, Is.SameAs(kernel));
			}
		}
コード例 #25
0
		public void SelfBoundTypesDeclaringMethodInterceptorsAreProxied()
		{
			var testModule = new InlineModule(m => m.Bind<ObjectWithMethodInterceptor>().ToSelf());

			using (var kernel = new StandardKernel(new DynamicProxy2Module(), testModule))
			{
				var obj = kernel.Get<ObjectWithMethodInterceptor>();
				Assert.That(obj, Is.Not.Null);
				Assert.That(obj, Is.InstanceOfType(typeof(IProxyTargetAccessor)));
			}
		}
コード例 #26
0
		public void HeuristicIgnoresParameterlessMethods()
		{
			var testModule = new InlineModule(m => m.Bind<PocoForMethodAutoWiring>().ToSelf());

			using (var kernel = new StandardKernel(new AutoWiringModule(), testModule))
			{
				var mock = kernel.Get<PocoForMethodAutoWiring>();
				Assert.That(mock, Is.Not.Null);
				Assert.That(mock.ParameterlessMethodWasCalled, Is.False);
			}
		}
コード例 #27
0
		public void DisposableObjectIsDisposedWhenReleased()
		{
			using (var kernel = new StandardKernel())
			{
				var mock = kernel.Get<DisposableMock>();
				Assert.That(mock, Is.Not.Null);

				kernel.Release(mock);
				Assert.That(mock.Disposed, Is.True);
			}
		}
コード例 #28
0
		public void OptionalDependenciesForSelfBoundTypesAreResolved()
		{
			using (var kernel = new StandardKernel())
			{
				RequestsOptionalConcreteType parent = kernel.Get<RequestsOptionalConcreteType>();

				Assert.That(parent, Is.Not.Null);
				Assert.That(parent.Child, Is.Not.Null);
				Assert.That(parent.Child, Is.InstanceOfType(typeof(SimpleObject)));
			}
		}
コード例 #29
0
		public void SelfBoundTypesDeclaringMethodInterceptorsCanBeReleased()
		{
			var testModule = new InlineModule(m => m.Bind<ObjectWithMethodInterceptor>().ToSelf());

			using (var kernel = new StandardKernel(new DynamicProxy2Module(), testModule))
			{
				var obj = kernel.Get<ObjectWithMethodInterceptor>();
				Assert.That(obj, Is.Not.Null);

				kernel.Release(obj);
			}
		}
コード例 #30
0
		public void SelfBoundTypeReceivesPrivateMethodInjection()
		{
			var options = new KernelOptions { InjectNonPublicMembers = true };

			using (var kernel = new StandardKernel(options))
			{
				var mock = kernel.Get<RequestsPrivateMethodInjection>();

				Assert.That(mock, Is.Not.Null);
				Assert.That(mock.Child, Is.Not.Null);
			}
		}