static Binding Bind(Type type, object target)
        {
            var containerBindingBuilder = new BindingBuilder(Binding.For(type));
            var scope = containerBindingBuilder.To(target);

            scope.Singleton();
            return(containerBindingBuilder.Build());
        }
        static Binding Bind(Type type, Func <object> target, bool singleton = false)
        {
            var containerBindingBuilder = new BindingBuilder(Binding.For(type));
            var scope = containerBindingBuilder.To(target);

            if (singleton)
            {
                scope.Singleton();
            }
            return(containerBindingBuilder.Build());
        }
Exemple #3
0
            public IEnumerable <IBinding> Resolve(Multimap <Type, IBinding> bindings, IRequest request)
            {
                if (request.Service == typeof(IBar))
                {
                    var binding = new Binding(request.Service);
                    var builder = new BindingBuilder <IBar>(binding, _kernel, string.Empty);
                    builder.To <Bar>().WithConstructorArgument("name", "parent");
                    return(new[] { binding });
                }

                return(Enumerable.Empty <IBinding>());
            }
        /// <summary>
        /// Adds the dependency.
        /// </summary>
        /// <param name="serviceType">Type of the service to bind to.</param>
        /// <param name="concreteType">Type of the concrete class to create.</param>
        /// <param name="lifetime">The lifetime of the registration.</param>
        protected override void AddDependencyCore(Type serviceType, Type concreteType, DependencyLifetime lifetime)
        {
            var binding = CreateBinding(serviceType, lifetime);

            if (lifetime == DependencyLifetime.PerRequest)
            {
                binding.ProviderCallback = ctx => new PerRequestProvider(concreteType, ctx.Kernel.Components.Get <IPlanner>(), ctx.Kernel.Components.Get <ISelector>());
                binding.Target           = BindingTarget.Provider;
            }
            else
            {
                var bindingBuilder = new BindingBuilder <object>(binding, _kernel);
                var bindingScope   = bindingBuilder.To(concreteType);
                if (lifetime == DependencyLifetime.Singleton)
                {
                    bindingScope.InSingletonScope();
                }
            }

            _kernel.AddBinding(binding);
        }
            public IEnumerable<IBinding> Resolve(Multimap<Type, IBinding> bindings, IRequest request)
            {
                if (request.Service == typeof(IBar))
                {
                    var binding = new Binding(request.Service);
                    var builder = new BindingBuilder<IBar>(binding, this.kernel, string.Empty);
                    builder.To<Bar>().WithConstructorArgument("name", "parent");
                    return new[] { binding };
                }

                return Enumerable.Empty<IBinding>();
            }
		private bool TryHandleToAttribute(XElement element, BindingBuilder<object> builder)
		{
			XAttribute toAttribute = element.Attribute("to");

			if (toAttribute == null)
				return false;

            if (toAttribute.Value.ToLowerInvariant().Equals("self"))
            {
                builder.ToSelf();
            }
            else
            {
                Type implementation = GetTypeFromAttributeValue(toAttribute);
                builder.To(implementation);
            }

			return true;
		}
        /// <summary>
        /// Tries to handle the to attribute.
        /// </summary>
        /// <param name="element">The element.</param>
        /// <param name="builder">The builder.</param>
        /// <returns>True if the To Attribute was found</returns>
        private static bool TryHandleToAttribute(XElement element, BindingBuilder<object> builder)
        {
            XAttribute toAttribute = element.Attribute("to");

            if (toAttribute == null)
            {
                return false;
            }

            Type implementation = GetTypeFromAttributeValue(toAttribute);
            builder.To(implementation);

            return true;
        }