public override void Load() { IBindingToSyntax <IRepository <BankAccount> > bind = Bind <IRepository <BankAccount> >(); switch (_storageType.ToLower()) { case "ef": { bind.To <BankAccountEFRepository>().WithConstructorArgument("MyDefaultConnection"); break; } case "json": { bind.To <BankAccountJsonRepository>(); break; } case "xml": { bind.To <BankAccountXmlRepository>(); break; } default: { bind.To <BankAccountBinaryRepository>(); break; } } Bind <IUnitOfWork>().To <UnitOfWork>() .WithConstructorArgument(bind); Bind <IBankAccountService>().To <BankAccountService>(); }
public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding) { if (_useHttpLog) { logBinding.To <HttpLog>(); } else { logBinding.To <ConsoleLog>(); } eventPublisherBinding.To <SpyEventPublisher>().InSingletonScope(); }
public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding) { if (_useHttpLog) { logBinding.To <HttpLog>(); } else { logBinding.To <ConsoleLog>(); } eventPublisherBinding.To <NullEventPublisher>(); }
public static IBindingModeInfoBehaviorSyntax <TTarget> ToSelf <TTarget>([NotNull] this IBindingToSyntax <TTarget> syntax, [NotNull] string selfPath) where TTarget : class { Should.NotBeNull(syntax, "syntax"); return(syntax.To((TTarget)syntax.Builder.GetData(BindingBuilderConstants.Target, true), selfPath)); }
/// <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 static IBindingModeInfoBehaviorSyntax To <TTarget, TSource>( [NotNull] this IBindingToSyntax <TTarget, TSource> syntax, [NotNull] Expression <Func <TSource, object> > sourcePath) { Should.NotBeNull(sourcePath, "sourcePath"); return(syntax.To(BindingExtensions.GetMemberPath(sourcePath))); }
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); }
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>(); }
/// <summary> /// Indicates that the service should be mocked. The mock is activated in the singleton scope. /// </summary> /// <typeparam name="T">The service type.</typeparam> /// <param name="builder">The fluent syntax.</param> /// <returns>The fluent syntax.</returns> public static IBindingWhenInNamedWithOrOnSyntax <T> ToMock <T>(this IBindingToSyntax <T> builder) { return(ProfilerInterceptor.GuardInternal(() => { var result = builder.To <T>(); builder.BindingConfiguration.ScopeCallback = StandardScopeCallbacks.Singleton; builder.Kernel.Components.Get <IMockResolver>().AttachToBinding(builder.BindingConfiguration, typeof(T)); return result; })); }
/// <summary> /// Used by Microcore hosts to initialize logging and tracing. /// </summary> /// <param name="logBinding"></param> /// <param name="eventPublisherBinding"></param> public void Bind(IBindingToSyntax <ILog> logBinding, IBindingToSyntax <IEventPublisher> eventPublisherBinding) { logBinding .To <SerilogLogger>() .InScope(GetTypeOfTarget) .WithConstructorArgument("receivingType", (context, target) => GetTypeOfTarget(context)); eventPublisherBinding .To <LogEventPublisher>() .InSingletonScope(); }
/// <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 IBindingWhenInNamedWithOrOnSyntax<object> HandleToAttribute(XElement element, IBindingToSyntax<object> builder) { XAttribute toAttribute = element.Attribute("to"); if (toAttribute == null) { return null; } Type implementation = GetTypeFromAttributeValue(toAttribute); return builder.To(implementation); }
/// <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 IBindingWhenInNamedWithOrOnSyntax <object> HandleToAttribute(XElement element, IBindingToSyntax <object> builder) { XAttribute toAttribute = element.Attribute("to"); if (toAttribute == null) { return(null); } Type implementation = GetTypeFromAttributeValue(toAttribute); return(builder.To(implementation)); }
/// <summary> /// Indicates that the service should be bound to a mocked instance of the specified type. /// </summary> /// <typeparam name="T">The service that is being mocked.</typeparam> /// <param name="builder">The builder that is building the binding.</param> /// <param name="additionalInterfaces">The additional interfaces for the mock.</param> /// <returns>The syntax for adding more information to the binding.</returns> public static IBindingWhenInNamedWithOrOnSyntax <T> ToMock <T>(this IBindingToSyntax <T> builder, params Type[] additionalInterfaces) { var result = builder.To <T>(); var bindingConfiguration = builder.BindingConfiguration; foreach (var additionalInterface in additionalInterfaces) { bindingConfiguration.Parameters.Add(new AdditionalInterfaceParameter(additionalInterface)); } bindingConfiguration.ProviderCallback = builder.Kernel.Components.Get <IMockProviderCallbackProvider>().GetCreationCallback(); return(result); }
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); }
/// <summary> /// Defines that the interface shall be bound to an automatically created factory proxy. /// </summary> /// <typeparam name="TInterface">The type of the interface.</typeparam> /// <param name="syntax">The syntax.</param> /// <param name="instanceProvider">The instance provider.</param> /// <param name="factoryType">Type of the factory.</param> /// <returns> /// The <see cref="IBindingWhenInNamedWithOrOnSyntax{TInterface}"/> to configure more things for the binding. /// </returns> public static IBindingWhenInNamedWithOrOnSyntax <TInterface> ToFactory <TInterface>(this IBindingToSyntax <TInterface> syntax, Func <IContext, IInstanceProvider> instanceProvider, Type factoryType) where TInterface : class { var proxy = Generator.ProxyBuilder.CreateInterfaceProxyTypeWithoutTarget( factoryType, new[] { typeof(IFactoryProxy) }, ProxyGenerationOptions.Default); var result = syntax.To(proxy); result.WithParameter(new ProxyTargetParameter()); var bindingConfiguration = syntax.BindingConfiguration; // Do not pass syntax to the lambda!!! We do not want the lambda referencing the syntax!!! syntax.Kernel.Bind <IInstanceProvider>().ToMethod(instanceProvider) .When(r => r.ParentRequest != null && r.ParentRequest.ParentContext.Binding.BindingConfiguration == bindingConfiguration) .InScope(ctx => bindingConfiguration); return(result); }
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 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)); }