Example #1
0
        private static IBindingInSyntax <object> To(this IBindingToSyntax <object> syntax, ServiceDescriptor s)
        {
            if (s.ImplementationType != null)
            {
                return(syntax.To(s.ImplementationType));
            }

            if (s.ImplementationFactory != null)
            {
                return(syntax.ToMethod(ctx => s.ImplementationFactory(ctx.Kernel)));
            }

            if (s.ImplementationInstance != null)
            {
                return(syntax.ToConstant(s.ImplementationInstance));
            }

            throw new Exception("Invalid service descriptor binding");
        }
Example #2
0
        public override void Load()
        {
            Bind <IServiceProvider>().To <NinjectServiceProvider>().InSingletonScope();
            Bind <IServiceScopeFactory>().To <NinjectServiceScopeFactory>().InSingletonScope();
            foreach (ServiceDescriptor serviceDescriptor in _serviceCollection)
            {
                IBindingToSyntax <object> binding = Bind(serviceDescriptor.ServiceType);
                IBindingWhenInNamedWithOrOnSyntax <object> binded;
                if (serviceDescriptor.ImplementationInstance != null)
                {
                    binded = binding.ToConstant(serviceDescriptor.ImplementationInstance);
                }
                else
                {
                    if (serviceDescriptor.ImplementationType != null)
                    {
                        binded = binding.To(serviceDescriptor.ImplementationType);
                    }
                    else
                    {
                        binded = binding.ToMethod(ctx => serviceDescriptor.ImplementationFactory(new NinjectServiceProvider(ctx.Kernel)));
                    }
                }
                switch (serviceDescriptor.Lifetime)
                {
                case ServiceLifetime.Singleton:
                    binded.InSingletonScope();
                    break;

                case ServiceLifetime.Scoped:
                    binded.InRequestScope();
                    break;

                case ServiceLifetime.Transient:
                    binded.InTransientScope();
                    break;

                default:
                    break;
                }
            }
        }
        public static IServiceProvider BuildNinjectServiceProvider(this IServiceCollection services, IKernel kernel)
        {
            var rvalue = new NinjectServiceProvider(kernel);

            kernel.Bind <IServiceProvider>().ToConstant(rvalue);
            foreach (var service in services)
            {
                IBindingToSyntax <object> bind = kernel.Rebind(service.ServiceType);
                IBindingWhenInNamedWithOrOnSyntax <object> binding = null;
                if (service.ImplementationInstance != null)
                {
                    binding = bind.ToConstant(service.ImplementationInstance);
                }
                else if (service.ImplementationType != null)
                {
                    binding = bind.To(service.ImplementationType);
                }
                else
                {
                    binding = bind.ToMethod(ctx => service.ImplementationFactory(ctx.Kernel.Get <IServiceProvider>()));
                }

                switch (service.Lifetime)
                {
                case ServiceLifetime.Scoped:
                    binding.InThreadScope();
                    break;

                case ServiceLifetime.Singleton:
                    binding.InSingletonScope();
                    break;

                case ServiceLifetime.Transient:
                    binding.InTransientScope();
                    break;
                }
            }

            return(new NinjectServiceProvider(kernel));
        }
        private IBindingWhenInNamedWithOrOnSyntax <object> BuildTo(IDependencyInfo info, IBindingToSyntax <object> initial)
        {
            if (info is IDependencyMethodInfo methodInfo)
            {
                return(initial.ToMethod(ctx => ExecuteMethod(methodInfo, ctx)));
            }

            if (info is IDependencyTypeInfo typeInfo)
            {
                return(initial.To(typeInfo.ResolutionType));
            }

            if (info is IDependencyInstanceInfo instanceInfo)
            {
                return(initial.ToConstant(instanceInfo.Instance));
            }

            if (info is IDependencyFactoryInfo factoryInfo)
            {
                return(initial.ToMethod(ctx => ExecuteFactory(factoryInfo, ctx)));
            }

            throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "The binding for the types '{0}' is invalid. The binding has not been configured correctly", info));
        }
Example #5
0
 public static IBindingWhenInNamedWithOrOnSyntax <TContract> FromMock <TContract>(this IBindingToSyntax <TContract> binder)
     where TContract : class
 {
     return(binder.ToConstant(Mock.Of <TContract>()));
 }