protected override IIoCContainer CreateTarget() { IKernel kernel = new StandardKernel(); DynamicIoCContainerContext context = new DynamicIoCContainerContext() { Register = registerContext => { IBindingWhenInNamedWithOrOnSyntax <object> binding = kernel.Bind(registerContext.Dependency) .To(registerContext.Implementation); if (registerContext.Settings.IsSingleton()) { binding.InSingletonScope(); } }, RegisterFactory = registerFactoryContext => { IBindingWhenInNamedWithOrOnSyntax <object> binding = kernel.Bind(registerFactoryContext.Type) .ToMethod(internalContext => registerFactoryContext.ObjectFactory()); if (registerFactoryContext.Settings.IsSingleton()) { binding.InSingletonScope(); } }, Resolve = typeToResolve => kernel.GetService(typeToResolve) }; return(new DynamicIoCContainer(context)); }
WhenAnyAncestorNamedLikeFactoryMethod <TInterface, TFactory>(this IBindingWhenInNamedWithOrOnSyntax <TInterface> syntax, Expression <Action <TFactory> > action) { if (action == null) { throw new ArgumentNullException("action"); } var methodCallExpression = action.Body as MethodCallExpression; if (methodCallExpression == null) { throw new ArgumentException("expected factory method instead of " + action, "action"); } var methodName = methodCallExpression.Method.Name; if (!methodName.StartsWith("Get")) { throw new ArgumentException("expected factory 'Get' method instead of " + action, "action"); } var bindingName = methodName.Substring(3); return(syntax.WhenAnyAncestorNamed(bindingName)); }
public static IBindingWhenInNamedWithOrOnSyntax <object> InScope(this IBindingWhenInNamedWithOrOnSyntax <object> spec, DependencyScope scope) { if (spec == null) { throw new ArgumentNullException(nameof(scope)); } switch (scope) { case DependencyScope.Singleton: spec.InSingletonScope(); break; case DependencyScope.Thread: spec.InThreadScope(); break; case DependencyScope.Transient: spec.InTransientScope(); break; } return(spec); }
public IInstanceRegistrarModifier <TContract> RegisterFactory(string serviceName, Func <IResolutionContext, TImplementation> @delegate) { IBindingWhenInNamedWithOrOnSyntax <TContract> bindingResult = _kernel.Bind <TContract>().ToMethod(context => @delegate.Invoke(new NinjectResolutionContext(_kernel))); bindingResult.Named(serviceName); return(new NinjectServiceRegistrarModifier <TContract, TImplementation>(this, bindingResult)); }
/// <summary> /// Initializes a new instance of the <see cref="FilterFilterBindingBuilder<T>"/> class. /// </summary> /// <param name="ninjectFilterBindingSyntax">The ninject filter binding syntax.</param> /// <param name="filterBindingSyntax">The filter binding syntax.</param> public FilterFilterBindingBuilder( IBindingWhenInNamedWithOrOnSyntax <NinjectFilter <T> > ninjectFilterBindingSyntax, IBindingWhenInNamedWithOrOnSyntax <T> filterBindingSyntax) { this.ninjectFilterBindingSyntax = ninjectFilterBindingSyntax; this.filterBindingSyntax = filterBindingSyntax; }
/// <summary>Registers a service with the container.</summary> /// <typeparam name="TContract">The <typeparamref name="TContract" /> type used for the contract.</typeparam> /// <typeparam name="TImplementation">The <typeparamref name="TImplementation" /> type used for the implementation.</typeparam> /// <param name="name">The name of the service.</param> public IServiceRegistrarModifier <TContract, TImplementation> Register(string name) { IBindingWhenInNamedWithOrOnSyntax <TContract> bindingResult = _kernel.Bind <TContract>().To <TImplementation>(); bindingResult.Named(name); return(new NinjectServiceRegistrarModifier <TContract, TImplementation>(this, bindingResult)); }
public void RegisterType(Type t, string name, ObjectLifetimeMode lifeitme) { IBindingWhenInNamedWithOrOnSyntax <object> bindingInSyntax = this.kernel.Bind(t).ToSelf(); bindingInSyntax.Named(name); this.SetLifetimeManager(bindingInSyntax, lifeitme); }
public void RegisterType <TFrom>(Type to, string name, ObjectLifetimeMode lifeitme) { IBindingWhenInNamedWithOrOnSyntax <TFrom> bindingInSyntax = this.kernel.Bind <TFrom>().To(to); bindingInSyntax.Named(name); this.SetLifetimeManager(bindingInSyntax, lifeitme); }
public void RegisterType <TFrom, TTo>(string name, ObjectLifetimeMode lifeitme) where TTo : TFrom { IBindingWhenInNamedWithOrOnSyntax <TFrom> bindingInSyntax = this.kernel.Bind <TFrom>().To <TTo>(); bindingInSyntax.Named(name); this.SetLifetimeManager(bindingInSyntax, lifeitme); }
public static void WithAdapterInterceptors <TAdapter>(this IBindingWhenInNamedWithOrOnSyntax <TAdapter> binding) { binding .Intercept() .With(new ExceptionWrapper <ServiceException>((e, m) => new AdapterException(HttpStatusCode.InternalServerError, m, e))) .InOrder(1); binding.Intercept().With <TransactionInterceptor>().InOrder(2); binding.Intercept().With <LogInterceptor>().InOrder(3); }
public EagerAndLazyBindingsWhenInNamedWithOrOnSyntax ( IBindingWhenInNamedWithOrOnSyntax <T2> eager, IBindingWhenInNamedWithOrOnSyntax <Lazy <T1> > lazy ) { Eager = eager; Lazy = lazy; }
/// <summary> /// Indicates that the service should be bound to the specified constant value. /// </summary> public void BindToConstant(Type service, object constValue, string name = null) { this.NotBeDisposed(); Should.NotBeNull(service, "service"); IBindingWhenInNamedWithOrOnSyntax <object> syntax = _kernel.Bind(service).ToConstant(constValue); if (name != null) { syntax.Named(name); } }
protected override void OnBuild(IDependencyInfo info) { IBindingToSyntax <object> initial = Kernel.Bind(info.ResolvedTypes.ToArray()); IBindingWhenInNamedWithOrOnSyntax <object> binding = BuildTo(info, initial); BindScope(info, binding); BindNamedIfConfigured(info, binding); Definitions.Add(info); }
private static void BindLifeTime(IDependency dependency, IBindingWhenInNamedWithOrOnSyntax <object> binding) { if (dependency.LifeTime == LifeTime.Singleton) { binding.InSingletonScope(); } if (dependency.LifeTime == LifeTime.OnRequest) { binding.InRequestScope(); } }
private IBindingNamedWithOrOnSyntax <TService> SetLifetime <TService>(IBindingWhenInNamedWithOrOnSyntax <TService> bindingWhenInNamedWithOrOnSyntax) { switch (LifetimeForRegisters) { case ServiceRegistrarLifetime.Transient: return(bindingWhenInNamedWithOrOnSyntax.InScope(StandardScopeCallbacks.Transient)); case ServiceRegistrarLifetime.Singleton: return(bindingWhenInNamedWithOrOnSyntax.InScope(StandardScopeCallbacks.Singleton)); case ServiceRegistrarLifetime.Thread: return(bindingWhenInNamedWithOrOnSyntax.InScope(StandardScopeCallbacks.Thread)); default: throw new NotSupportedException(); } }
private static void BindScope(IDependencyInfo info, IBindingWhenInNamedWithOrOnSyntax <object> binding) { switch (info.Scope) { case DependyBuilderScope.Transient: binding.InTransientScope(); break; case DependyBuilderScope.Singleton: binding.InSingletonScope(); break; } }
/// <summary> /// Indicates that the service should be bound to the specified type. /// </summary> /// <param name="service">The specified service type.</param> /// <param name="typeTo">The specified to type</param> /// <param name="lifecycle"> /// The specified <see cref="DependencyLifecycle" /> /// </param> /// <param name="name">The specified binding name.</param> public void Bind(Type service, Type typeTo, DependencyLifecycle lifecycle, string name = null) { this.NotBeDisposed(); Should.NotBeNull(service, "service"); Should.NotBeNull(typeTo, "typeTo"); IBindingWhenInNamedWithOrOnSyntax <object> syntax = _kernel .Bind(service) .To(typeTo); SetLifecycle(syntax, lifecycle); if (name != null) { syntax.Named(name); } }
/// <summary> /// Indicates that the service should be bound to the specified method. /// </summary> /// <param name="service">The specified service type.</param> /// <param name="methodBindingDelegate">The specified factory delegate.</param> /// <param name="lifecycle"> /// The specified <see cref="DependencyLifecycle" /> /// </param> /// <param name="name">The specified binding name.</param> public void BindToMethod(Type service, Func <IIocContainer, IList <IIocParameter>, object> methodBindingDelegate, DependencyLifecycle lifecycle, string name = null) { this.NotBeDisposed(); Should.NotBeNull(service, "service"); Should.NotBeNull(methodBindingDelegate, "methodBindingDelegate"); IBindingWhenInNamedWithOrOnSyntax <object> syntax = _kernel .Bind(service) .ToMethod(context => methodBindingDelegate(this, GetParameters(context))); SetLifecycle(syntax, lifecycle); if (name != null) { syntax.Named(name); } }
private void ApplyScope <T>(IBindingWhenInNamedWithOrOnSyntax <T> registration, RegisterScope scope) { switch (scope) { case RegisterScope.PerInject: registration.InTransientScope(); break; case RegisterScope.PerContext: registration.InRequestScope(); break; case RegisterScope.Unique: registration.InSingletonScope(); break; } }
private static void SetScope(IConfig config, IBindingWhenInNamedWithOrOnSyntax <object> binding) { binding.InRequestScope(); if (config.IsSingleton) { binding.InSingletonScope(); } else if (config.IsThreadInstance) { binding.InThreadScope(); } else if (config.IsTransientScope) { binding.InTransientScope(); } }
public static IBindingNamedWithOrOnSyntax <T> PerLifeStyle <T>(this IBindingWhenInNamedWithOrOnSyntax <T> binding, ComponentLifeStyle lifeStyle) { switch (lifeStyle) { case ComponentLifeStyle.Singleton: return(binding.InSingletonScope()); case ComponentLifeStyle.InThreadScope: return(binding.InThreadScope()); case ComponentLifeStyle.InRequestScope: return(binding.InRequestScope()); case ComponentLifeStyle.Transient: default: return(binding.InTransientScope()); } }
private void SetLifetimeManager <TObject>(IBindingWhenInNamedWithOrOnSyntax <TObject> bindingInSyntax, ObjectLifetimeMode lifeitme) { if (lifeitme == ObjectLifetimeMode.Transient) { bindingInSyntax.InTransientScope(); } else if (lifeitme == ObjectLifetimeMode.Singleton) { bindingInSyntax.InSingletonScope(); } else if (lifeitme == ObjectLifetimeMode.WeakReferenceRequest) { bindingInSyntax.InRequestScope(); } else { bindingInSyntax.InThreadScope(); } }
WhenInjectedIntoRequestChain <T> (this IBindingWhenInNamedWithOrOnSyntax <T> @this , params Type[] parentChain ) { @this.BindingConfiguration.Condition = request => { var result = true; foreach (var parent in parentChain) { result = result && WhenInjectedInto(request, parent); request = request?.ParentRequest; } return(result); }; return((IBindingInNamedWithOrOnSyntax <T>)@this); }
public static void RegisterTypeWithArgs <TInterface, TImplementation>(Dictionary <string, object> args) where TImplementation : TInterface { if (types.Contains(typeof(TInterface))) { RemoveExistingBinding <TInterface>(); } else { types.Add(typeof(TInterface)); } IBindingWhenInNamedWithOrOnSyntax <TImplementation> bindings = kernel.Bind <TInterface>().To <TImplementation>(); foreach (var arg in args) { bindings.WithConstructorArgument(arg.Key, arg.Value); } }
protected virtual void SetBinding <T>(IBindingWhenInNamedWithOrOnSyntax <T> binding, ServiceScope scope) { switch (scope) { case ServiceScope.Transient: binding.InTransientScope(); break; case ServiceScope.Thread: binding.InThreadScope(); break; case ServiceScope.Singleton: binding.InSingletonScope(); break; case ServiceScope.Request: this.SetRequestScope(binding); 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)); }
public void Bind(Type service, Type typeTo, DependencyLifecycle lifecycle, string name = null, params IIocParameter[] parameters) { this.NotBeDisposed(); Should.NotBeNull(service, nameof(service)); Should.NotBeNull(typeTo, nameof(typeTo)); IBindingWhenInNamedWithOrOnSyntax <object> syntax = _kernel .Bind(service) .To(typeTo); if (parameters != null) { for (int index = 0; index < parameters.Length; index++) { syntax.WithParameter(ConvertParameter(parameters[index], true)); } } SetLifecycle(syntax, lifecycle); if (name != null) { syntax.Named(name); } }
public static void Configure(IBindingWhenInNamedWithOrOnSyntax <object> syntax, Type serviceType) { if (!IsDefaultImplementation(serviceType)) { int indexOfSuffix = serviceType.Name.IndexOf(DefaultImplementationName, StringComparison.InvariantCultureIgnoreCase); if (indexOfSuffix > 0) { // specific handler string route = serviceType.Name.Substring(0, indexOfSuffix); syntax .When(x => route == syntax.Kernel.Get <IMyContext>().CustomRouteValue); } else { // invalid name! throw CreateExceptioForNamingConventionViolation(serviceType); } } //syntax.InRequestScope(); }
public IEnumerable <IBindingWhenInNamedWithOrOnSyntax <object> > CreateBindings(Type type, IBindingRoot bindingRoot) { if (type == null) { throw new ArgumentNullException("type"); } if (bindingRoot == null) { throw new ArgumentNullException("bindingRoot"); } if (type.IsAbstract || type.IsInterface) { return(Enumerable.Empty <IBindingWhenInNamedWithOrOnSyntax <object> >()); } var bindings = new List <IBindingWhenInNamedWithOrOnSyntax <object> >(); IBindingWhenInNamedWithOrOnSyntax <object> binding = bindingRoot .Bind(typeof(IExtract)) // you maybe want to pass typeof(IExtract) to constructor .To(m_Generic.MakeGenericType(type)); bindings.Add(binding); return(bindings); }
private static void BindNamedIfConfigured(IDependencyInfo info, IBindingWhenInNamedWithOrOnSyntax <object> binding) { string named; if (info.Key is Enum enumKey) { named = enumKey.GetBindingName(); } else if (info.Key is string name) { named = name; } else { named = info.Key?.ToString(); } if (!string.IsNullOrWhiteSpace(named)) { binding.Named(named); } }
public IBindingNamedWithOrOnSyntax<object> InScope(IBindingWhenInNamedWithOrOnSyntax<object> binded, BindingScope scope) { if (binded == null) throw new ArgumentNullException(nameof(binded)); if (scope == null) throw new ArgumentNullException(nameof(scope)); IBindingNamedWithOrOnSyntax<object> scoped; if (scope == BindingScope.SingletonScope) { scoped = binded.InSingletonScope(); } else if (scope == BindingScope.ThreadScope) { scoped = binded.InThreadScope(); } else if (scope == BindingScope.TransientScope) { scoped = binded.InTransientScope(); } else { throw new NotSupportedException($"Undefined scope {scope}"); } return scoped; }
/// <summary> /// Add scope configuration to binding /// </summary> public abstract void Configure(IBindingWhenInNamedWithOrOnSyntax<object> binding);
public void Bind(IBindingWhenInNamedWithOrOnSyntax<object> ninjectBinding) { ninjectBinding.InSingletonScope(); }
public void Bind(IBindingWhenInNamedWithOrOnSyntax<object> ninjectBinding) { ninjectBinding.InTransientScope(); }
/// <summary> /// Adds thread-scope configuration to binding /// </summary> public override void Configure(IBindingWhenInNamedWithOrOnSyntax<object> binding) { binding.InThreadScope(); }