Example #1
0
        /// <summary>
        /// Returns a value indicating whether the specified member should be injected.
        /// </summary>
        /// <param name="member">The member in question.</param>
        /// <returns>
        ///     <see langword="true"/> if the member should be injected; otherwise <see langword="false"/>.
        /// </returns>
        public bool ShouldInject(MemberInfo member)
        {
            if (member.MemberType != MemberTypes.Property)
            {
                return(false);
            }

            var propertyInfo = member as PropertyInfo;

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

            if (propertyInfo.GetSetMethod() == null)
            {
                return(false);
            }

            // If the types are the same, or if the property is an interface or abstract class
            // that the declaring type implements (which would cause a cyclic resolution)
            if ((propertyInfo.PropertyType == propertyInfo.DeclaringType) ||
                ((propertyInfo.DeclaringType.IsAssignableFrom(propertyInfo.PropertyType))))
            {
                return(false);
            }

            var request = _kernel.CreateRequest(propertyInfo.PropertyType, null, EmptyParameters, true, false);

            return(_kernel.CanResolve(request));
        }
        /// <summary>
        /// Determines whether a binding exists between the specified service and concrete types.
        /// </summary>
        public bool HasDependencyImplementation(Type serviceType, Type concreteType)
        {
            if (serviceType == null || concreteType == null)
            {
                return(false);
            }

            if (serviceType == concreteType)
            {
                return(HasDependency(serviceType));
            }

            var bindings = GetBindings(serviceType);
            var request  = _kernel.CreateRequest(serviceType, null, EmptyParameters, false, false);

            return(bindings.Any(b =>
            {
                if (b.Target != BindingTarget.Type)
                {
                    return false;
                }
                var context = new Context(_kernel, request, b, _kernel.Components.Get <ICache>(),
                                          _kernel.Components.Get <IPlanner>(),
                                          _kernel.Components.Get <IPipeline>());
                return b.GetProvider(context).Type == concreteType;
            }));
        }
Example #3
0
        private object GetService(Type serviceType)
        {
            //Реализация подсмотрена здесь https://github.com/ninject/Ninject.Web.Mvc/blob/master/mvc3/src/Ninject.Web.Mvc/NinjectDependencyResolver.cs
            var request = _kernel.CreateRequest(serviceType, null, new Parameter[0], true, true);

            return(_kernel.Resolve(request).SingleOrDefault());
        }
        public bool can_resolve_type(Type type)
        {
            ConfigureKernel();
            var  request           = kernel.CreateRequest(type, x => true, new IParameter[] { }, false, false);
            bool canResolveRequest = kernel.CanResolve(request);

            return(canResolveRequest);
        }
        private static void TryGetSyncHandler <TResult>(IKernel scope, IQuery <TResult> query, out object handler)
        {
            var      asyncGenericType   = typeof(IQueryHandler <,>);
            var      closedAsyncGeneric = asyncGenericType.MakeGenericType(query.GetType(), typeof(TResult));
            IRequest request            = scope.CreateRequest(closedAsyncGeneric, null, new Parameter[0], true, true);

            handler = scope.Resolve(request).SingleOrDefault();
        }
Example #6
0
        private object GetExportedValue(IBinding binding)
        {
            bool Constraint(IBindingMetadata bindingMetadata)
            {
                return(bindingMetadata == binding.Metadata);
            }

            var request = _kernel.CreateRequest(binding.Service, Constraint, binding.Parameters, false, true);

            var result = _kernel.Resolve(request).Single();

            return(result);
        }
Example #7
0
        public KernelBootstrapperTests()
        {
            kernel = Substitute.For <IKernel>();
            kernel.WhenForAnyArgs(x => x.Load((IEnumerable <INinjectModule>)null)).Do(ci => loadedModules.AddRange(ci.ArgAt <IEnumerable <INinjectModule> >(0)));
            kernel.CreateRequest(null, null, null, false, false).ReturnsForAnyArgs(ci =>
            {
                var request = Substitute.For <IRequest>();
                request.Service.Returns(ci.ArgAt <Type>(0));
                return(request);
            });

            revoConfiguration = Substitute.For <IRevoConfiguration>();
            revoConfiguration.GetSection <KernelConfigurationSection>().Returns(kernelConfigurationSection);

            sut = new KernelBootstrapper(kernel, revoConfiguration);
        }
        /// <summary>
        ///     Determines whether the specified request can be resolved.
        /// </summary>
        /// <param name="service">The specified service type.</param>
        /// <param name="name">The specified binding name.</param>
        /// <returns>
        ///     <c>True</c> if the specified service has been resolved; otherwise, <c>false</c>.
        /// </returns>
        public bool CanResolve(Type service, string name = null)
        {
            Should.NotBeNull(service, "service");
            if (IsDisposed)
            {
                return(false);
            }
            Func <IBindingMetadata, bool> canResolve = null;

            if (name != null)
            {
                canResolve = metadata => metadata.Name == name;
            }
            IRequest req = _kernel.CreateRequest(service, canResolve, Empty.Array <IParameter>(), false, true);

            return(_kernel.CanResolve(req, true));
        }
Example #9
0
        public IExpression CreateFile(int id, string text, string name)
        {
            IRequest req = Kernel.CreateRequest(typeof(IExpression), null, new IParameter[] { new Parameter("id", id, false), new Parameter("text", text, false), new Parameter("name", name, false) }, false, false);

            // I don't know if I've missed the point here or not, but I anticipated that each time i called Bind<> I'd get a different instance
            // of the class put in the kernel with those passed in parameter values. However I don't I get the same class back each time
            // with the same values, so the only way to create a new instance is to Unbind<> the original instance and add another with the
            // new params?
            if (!this.Kernel.CanResolve(req))
            {
                Kernel.Bind <IExpression>().To <OxTailHelpers.Expression>().InTransientScope()
                .WithConstructorArgument("id", id)
                .WithConstructorArgument("text", text)
                .WithConstructorArgument("name", name);
            }

            IExpression expr = this.Kernel.Get <IExpression>();

            Kernel.Unbind <IExpression>();

            return(expr);
        }
        public TValue TryGet <TValue>(string name = null) where TValue : class
        {
            var request = _kernel.CreateRequest(typeof(TValue), b => b.Name == name, new IParameter[0], true, true);

            return(_kernel.Resolve(request).Cast <TValue>().FirstOrDefault());
        }
Example #11
0
 public static bool IsRegistered(this IKernel kernel, Type type)
 {
     return(kernel.CanResolve(kernel.CreateRequest(type, _ => true, new IParameter[] { }, false, false)));
 }
Example #12
0
        public object GetService(Type serviceType)
        {
            var request = _kernel.CreateRequest(serviceType, null, new Parameter[0], true, true);

            return(_kernel.Resolve(request).SingleOrDefault());
        }
Example #13
0
        public object Get(Type type, bool optional)
        {
            var request = Kernel.CreateRequest(type, null, new IParameter[0], optional, true);

            return(Kernel.Resolve(request).SingleOrDefault());
        }
Example #14
0
 /// <summary>
 /// Basic method to find out if the service, <paramref name="serviceType"/>, has been registered with the <param name="kernel"/>
 /// </summary>
 /// <param name="kernel"></param>
 /// <param name="serviceType"></param>
 /// <returns></returns>
 public static bool IsRegistered(this IKernel kernel, Type serviceType)
 {
     return(kernel.CanResolve(kernel.CreateRequest(serviceType, meta => true, new List <IParameter>(), false, false)));
 }
        /// <summary>
        /// Determines whether the specified component type has a component.
        /// </summary>
        /// <param name="componentType">
        /// Type of the component.
        /// </param>
        /// <returns>
        /// <c>true</c> if the specified component type has a component; otherwise, <c>false</c>.
        /// </returns>
        public bool HasComponent(Type componentType)
        {
            var request = kernel.CreateRequest(componentType, null, new IParameter[0], false, true);

            return(kernel.CanResolve(request));
        }
 /// <summary>
 /// Return true if the given type is registered with the container.
 /// </summary>
 /// <param name="type"></param>
 /// <returns></returns>
 public override bool IsRegistered(Type type)
 {
     return(container.CanResolve(container.CreateRequest(type, null, new List <IParameter>(), false, false)));
 }
Example #17
0
 /// <summary>
 /// Determines whether the specified service type is bindable.
 /// </summary>
 /// <param name="serviceType">The type of the service.</param>
 /// <param name="kernel">The kernel.</param>
 /// <returns>
 /// 	<see langword="true"/> if the specified service type is bindable; otherwise, <see langword="false"/>.
 /// </returns>
 public static bool IsBindable(this Type serviceType, IKernel kernel)
 {
     var request = kernel.CreateRequest(serviceType, null, new IParameter[] { }, false);
     return kernel.CanResolve(request);
 }
Example #18
0
        /// <summary>
        /// Determines whether the specified service type is bindable.
        /// </summary>
        /// <param name="serviceType">The type of the service.</param>
        /// <param name="kernel">The kernel.</param>
        /// <returns>
        ///     <see langword="true"/> if the specified service type is bindable; otherwise, <see langword="false"/>.
        /// </returns>
        public static bool IsBindable(this Type serviceType, IKernel kernel)
        {
            var request = kernel.CreateRequest(serviceType, null, new IParameter[] { }, false, false);

            return(kernel.CanResolve(request));
        }