Ejemplo n.º 1
0
        /*----------------------------------------------------------------------------------------*/
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the <see cref="KernelBase"/> class.
        /// </summary>
        /// <param name="options">The options to use.</param>
        /// <param name="modules">The modules to load into the kernel.</param>
        protected KernelBase(KernelOptions options, IEnumerable <IModule> modules)
        {
            Ensure.ArgumentNotNull(options, "options");

            Options = options;

            // Ensure that at least a null logger is connected while we load the components.
            Logger = NullLogger.Instance;

            Components = InitializeComponents();
            ValidateComponents();

            // If the user has connected a real logger factory, get a real logger.
            Logger = Components.LoggerFactory.GetLogger(GetType());

            // Create the container scope, and register it with the tracker.
            IScope scope = Components.ScopeFactory.Create();

            Components.Tracker.RegisterScope(this, scope);

            // Load the modules into the module manager.
            Components.ModuleManager.Load(modules);

            ActivateEagerServices();
        }
		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);
			}
		}
Ejemplo n.º 3
0
		public void DebugInfoCreatedFromStackTrace()
		{
			var module = new InlineModule(m => m.Bind<IMock>().To<SimpleObject>());
			var options = new KernelOptions { GenerateDebugInfo = true };

			using (var kernel = new StandardKernel(options, module))
			{
				IContext context = kernel.Components.ContextFactory.Create(typeof(IMock));
				IBinding binding = kernel.Components.BindingSelector.SelectBinding(typeof(IMock), context);

				Assert.That(binding, Is.Not.Null);
				Assert.That(binding.DebugInfo, Is.Not.Null);
			}
		}
		public void ServiceBoundTypeReceivesPrivateFieldInjection()
		{
			var module = new InlineModule(m => m.Bind(typeof(IMock)).To(typeof(RequestsPrivateFieldInjection)));
			var options = new KernelOptions { InjectNonPublicMembers = true };

			using (var kernel = new StandardKernel(options, module))
			{
				var mock = kernel.Get<IMock>();
				Assert.That(mock, Is.Not.Null);

				var typedMock = mock as RequestsPrivateFieldInjection;
				Assert.That(typedMock, Is.Not.Null);
				Assert.That(typedMock.Child, Is.Not.Null);
			}
		}
		public void EagerActivationCausesSingletonInstancesToBeImmediatelyActivated()
		{
			var module = new InlineModule(m => m.Bind<ObjectWithSingletonBehavior>().ToSelf());
			var options = new KernelOptions { UseEagerActivation = true };

			using (var kernel = new StandardKernel(options, module))
			{
				Type type = typeof(ObjectWithSingletonBehavior);

				IContext context = kernel.Components.ContextFactory.Create(type);
				IBinding binding = kernel.Components.BindingSelector.SelectBinding(type, context);

				Assert.That(binding, Is.Not.Null);

				IActivationPlan plan = binding.Components.Planner.GetPlan(binding, type);
				var behavior = plan.Behavior as SingletonBehavior;
				Assert.That(behavior, Is.Not.Null);

				Assert.That(behavior.ContextCache, Is.Not.Empty);
			}
		}
		public void HeuristicSelectsConstructorWithMostValidArgumentsEvenIfNotLargest()
		{
			var testModule = new InlineModule(
				m => m.Bind<PocoForFieldAutoWiring>().ToSelf(),
				m => m.Bind<IMock>().To<SimpleObject>()
			);

			var options = new KernelOptions { InjectNonPublicMembers = true };

			using (var kernel = new StandardKernel(options, new AutoWiringModule(), testModule))
			{
				var mock = kernel.Get<PocoForFieldAutoWiring>();
				Assert.That(mock, Is.Not.Null);

				Assert.That(mock.Child, Is.Not.Null);
				Assert.That(mock.Child, Is.InstanceOfType(typeof(SimpleObject)));

				Assert.That(mock.ServiceA, Is.Null);
				Assert.That(mock.ServiceB, Is.Null);
			}
		}
		public void HeuristicSelectsPropertiesIfTheyHaveMatchingBindings()
		{
			var testModule = new InlineModule(
				m => m.Bind<PocoForFieldAutoWiring>().ToSelf(),
				m => m.Bind<IServiceA>().To<ServiceImplA>(),
				m => m.Bind<IServiceB>().To<ServiceImplB>()
			);

			var options = new KernelOptions { InjectNonPublicMembers = true };

			using (var kernel = new StandardKernel(options, new AutoWiringModule(), testModule))
			{
				var mock = kernel.Get<PocoForFieldAutoWiring>();
				Assert.That(mock, Is.Not.Null);

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

				Assert.That(mock.ServiceA, Is.Not.Null);
				Assert.That(mock.ServiceA, Is.InstanceOfType(typeof(ServiceImplA)));

				Assert.That(mock.ServiceB, Is.Not.Null);
				Assert.That(mock.ServiceB, Is.InstanceOfType(typeof(ServiceImplB)));
			}
		}
		public void CanInjectCircularReferencesIntoMethods()
		{
			var module = new InlineModule(
				m => m.Bind<CircularMethodMockA>().ToSelf(),
				m => m.Bind<CircularMethodMockB>().ToSelf()
			);

			var options = new KernelOptions { InjectNonPublicMembers = true };

			using (var kernel = new StandardKernel(options, module))
			{
				var mockA = kernel.Get<CircularMethodMockA>();
				var mockB = kernel.Get<CircularMethodMockB>();

				Assert.That(mockA, Is.Not.Null);
				Assert.That(mockB, Is.Not.Null);
				Assert.That(mockA.MockB, Is.SameAs(mockB));
				Assert.That(mockB.MockA, Is.SameAs(mockA));
			}
		}
Ejemplo n.º 9
0
		/*----------------------------------------------------------------------------------------*/
		#region Constructors
		/// <summary>
		/// Initializes a new instance of the <see cref="KernelBase"/> class.
		/// </summary>
		/// <param name="options">The options to use.</param>
		/// <param name="modules">The modules to load into the kernel.</param>
		protected KernelBase(KernelOptions options, IEnumerable<IModule> modules)
		{
			Ensure.ArgumentNotNull(options, "options");

			Options = options;

			// Ensure that at least a null logger is connected while we load the components.
			Logger = NullLogger.Instance;

			Components = InitializeComponents();
			ValidateComponents();

			// If the user has connected a real logger factory, get a real logger.
			Logger = Components.LoggerFactory.GetLogger(GetType());

			// Create the container scope, and register it with the tracker.
			IScope scope = Components.ScopeFactory.Create();
			Components.Tracker.RegisterScope(this, scope);

			// Load the modules into the module manager.
			Components.ModuleManager.Load(modules);

			ActivateEagerServices();
		}
Ejemplo n.º 10
0
		public void IncompatibleProviderAndBindingServiceTypeThrowsException()
		{
			var module = new InlineModule(m => m.Bind(typeof(IMock)).To(typeof(ObjectWithNoInterfaces)));
			var options = new KernelOptions { IgnoreProviderCompatibility = false };

			using (var kernel = new StandardKernel(options, module))
			{
				kernel.Get<IMock>();
			}
		}
Ejemplo n.º 11
0
		public void IncompatibleProviderAllowedIfProviderCompatibilityIsIgnored()
		{
			var module = new InlineModule(m => m.Bind(typeof(IMock)).To(typeof(ObjectWithNoInterfaces)));
			var options = new KernelOptions { IgnoreProviderCompatibility = true };

			using (var kernel = new StandardKernel(options, module))
			{
				var mock = kernel.Get(typeof(IMock)) as ObjectWithNoInterfaces;
				Assert.That(mock, Is.Not.Null);
			}
		}
Ejemplo n.º 12
0
 /*----------------------------------------------------------------------------------------*/
 /// <summary>
 /// Initializes a new instance of the <see cref="StandardKernel"/> class.
 /// </summary>
 /// <param name="options">The kernel options to use.</param>
 /// <param name="modules">One or more modules to load into the kernel.</param>
 public StandardKernel(KernelOptions options, params IModule[] modules)
     : base(options, modules)
 {
 }
Ejemplo n.º 13
0
 /*----------------------------------------------------------------------------------------*/
 /// <summary>
 /// Initializes a new instance of the <see cref="StandardKernel"/> class.
 /// </summary>
 /// <param name="options">The kernel options to use.</param>
 /// <param name="modules">One or more modules to load into the kernel.</param>
 public StandardKernel(KernelOptions options, IEnumerable <IModule> modules)
     : base(options, modules)
 {
 }
Ejemplo n.º 14
0
		/*----------------------------------------------------------------------------------------*/
		/// <summary>
		/// Initializes a new instance of the <see cref="StandardKernel"/> class.
		/// </summary>
		/// <param name="options">The kernel options to use.</param>
		/// <param name="modules">One or more modules to load into the kernel.</param>
		public StandardKernel(KernelOptions options, params IModule[] modules)
			: base(options, modules)
		{
		}
Ejemplo n.º 15
0
		/*----------------------------------------------------------------------------------------*/
		/// <summary>
		/// Initializes a new instance of the <see cref="StandardKernel"/> class.
		/// </summary>
		/// <param name="options">The kernel options to use.</param>
		/// <param name="modules">One or more modules to load into the kernel.</param>
		public StandardKernel(KernelOptions options, IEnumerable<IModule> modules)
			: base(options, modules)
		{
		}