public static IBindingNamedWithOrOnSyntax <T> ConfigureImplementationAndLifecycle <T>(this IBindingToSyntax <T> bindingToSyntax, ServiceDescriptor descriptor) where T : class { IBindingNamedWithOrOnSyntax <T> result; if (descriptor.ImplementationType != null) { result = bindingToSyntax.To(descriptor.ImplementationType).ConfigureLifecycle(descriptor.Lifetime); } else if (descriptor.ImplementationFactory != null) { result = bindingToSyntax.ToMethod(context => { var provider = context.Kernel.Get <IServiceProvider>(); return(descriptor.ImplementationFactory(provider) as T); }).ConfigureLifecycle(descriptor.Lifetime); } else { // use ToMethod here as ToConstant has the wrong return type. result = bindingToSyntax.ToMethod(context => descriptor.ImplementationInstance as T).InSingletonScope(); } return(result); }
/// <summary> /// Used by Microdot hosts to initialize logging and tracing. /// </summary> /// <param name="logBinding"></param> /// <param name="eventPublisherBinding"></param> public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding, IBindingToSyntax <Func <string, ILog> > funcLog) { // Bind microdot log that takes the caller name from class asking for the log logBinding .To <NLogLogger>() .InScope(GetTypeOfTarget) .WithConstructorArgument("receivingType", (context, target) => GetTypeOfTarget(context)); eventPublisherBinding .To <LogEventPublisher>() .InSingletonScope(); // Bind Orleans log with string context funcLog.ToMethod(c => { return(loggerName => { var dict = c.Kernel.Get <DisposableCollection <string, ILog> >(); return dict.GetOrAdd(loggerName , logName => { var caller = new ConstructorArgument("caller", logName); return c.Kernel.Get <NLogLogger>(caller); }); }); }) .InTransientScope(); }
public LazyBindingWithOrOnSyntax ( IBindingToSyntax <T1> eager, IBindingToSyntax <Lazy <T1> > lazy, IKernel kernel, string name, bool inSingletonScope ) { _kernel = kernel; _name = name; _eager = eager.To <T2>().Named(name); _lazy = inSingletonScope ? lazy.ToMethod(GetInstance).InSingletonScope().Named(name) : lazy.ToMethod(GetInstance).Named(name); }
public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding, IBindingToSyntax <Func <string, ILog> > logFactory) { logBinding.To <ConsoleLog>(); logFactory.ToMethod(c => caller => c.Kernel.Get <ConsoleLog>()); eventPublisherBinding.To <NullEventPublisher>(); }
private static IBindingWhenInNamedWithOrOnSyntax <TContract> BindChannel <TContract>( IBindingToSyntax <TContract> bindingSyntax, Func <ChannelFactory <TContract> > factoryFactory) where TContract : class { // Don't create the ChannelFactory at binding time, since it's an expensive operation var lazyFactory = new Lazy <ChannelFactory <TContract> >(factoryFactory); return(bindingSyntax.ToMethod(x => lazyFactory.Value.CreateChannel())); }
public static IBindingWithOrOnSyntax <TService> ToExtrnalService <TService>(this IBindingToSyntax <TService> syntax) { var serviceBinding = new ServiceBindingInfo { ServiceType = typeof(TService), IsExternal = true }; syntax.Kernel.Bind <ServiceBindingInfo>().ToConstant(serviceBinding); return(syntax.ToMethod(ThrowOnAttemptToResolve <TService>) .WithMetadata(nameof(ServiceBindingInfo), serviceBinding)); }
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)); }
public static IBindingWhenInNamedWithOrOnSyntax <T1> ConstructUsing <TService1, T1>(this IBindingToSyntax <T1> binding, Func <TService1, T1> method) { if (binding == null) { throw new ArgumentNullException("binding"); } if (method == null) { throw new ArgumentNullException("method"); } return(binding.ToMethod(context => { var service1 = context.Kernel.Get <TService1>(); return method(service1); })); }
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"); }
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 IBindingWhenInNamedWithOrOnSyntax <object> ToOpenGenericProvider( this IBindingToSyntax <object> @this, Type providerType) { if (@this == null) { throw new ArgumentNullException(nameof(@this)); } if (providerType == null) { throw new ArgumentNullException(nameof(providerType)); } if (!providerType.IsGenericTypeDefinition) { throw new ArgumentException("Provider type should be open generic type definition.", nameof(providerType)); } return(@this.ToMethod( ctx => CreateProviderFromOpenGeneric(ctx, providerType))); }
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)); }
/// <summary> /// Defines that the interface shall be bound to an existing class without the interface. /// </summary> /// <typeparam name="TInterface">The type of the interface.</typeparam> /// <param name="syntax">The syntax.</param> /// <param name="perspectiveType">Type of the perspective.</param> public static IBindingWhenInNamedWithOrOnSyntax <TInterface> ToPerspective <TInterface>( this IBindingToSyntax <TInterface> syntax, Type perspectiveType) { return(syntax.ToMethod(ctx => Activate <TInterface>(ctx, perspectiveType))); }
public static IBindingWhenInNamedWithOrOnSyntax <T> ToConfig <T>(this IBindingToSyntax <T> bind, string sectionName = null) { return(bind.ToMethod(ctx => AutoConfig.Map <T>(sectionName))); }
public static IBindingWhenInNamedWithOrOnSyntax <T> ToMock <T>(this IBindingToSyntax <T> binding) { return(binding.ToMethod(CreateMockObject <T>)); }