Esempio n. 1
0
 public static IBindingNamedWithOrOnSyntax<T> AsEagerSingleton<T>(this IBindingInSyntax<T> binding)
 {
     var r = binding.InSingletonScope();
     
     binding.Kernel.Bind<IEagerSingleton>().To<EagerSingleton<T>>().InSingletonScope();
     return r;
 }
Esempio n. 2
0
        /// <summary>
        /// Set scope based on <see cref="BindingLifecycle">ActivationScope</see>
        /// </summary>
        /// <param name="syntax"><see cref="IBindingInSyntax{T}">Binding syntax</see> to set the scope for</param>
        /// <param name="lifecycle"><see cref="BindingLifecycle"/> to use</param>
        public static void WithLifecycle <T>(this IBindingInSyntax <T> syntax, BindingLifecycle lifecycle)
        {
            switch (lifecycle)
            {
            case BindingLifecycle.Singleton:
                syntax.InSingletonScope();
                break;

#if (false)
            case BindingLifecycle.Request:
                syntax.InRequestScope();
                break;
#endif

#if (NET461)
            case BindingLifecycle.Thread:
                syntax.InThreadScope();
                break;
#endif


            case BindingLifecycle.Transient:
                syntax.InTransientScope();
                break;
            }
        }
 public static IBindingNamedWithOrOnSyntax <T> InCommandScope <T>(this IBindingInSyntax <T> src, KernelMode mode)
 {
     return(mode switch
     {
         KernelMode.HangFireJob => src.InBackgroundJobScope(),
         KernelMode.Web => src.InRequestScope(),
         _ => throw new ArgumentOutOfRangeException(nameof(mode))
     });
Esempio n. 4
0
 public IBindingNamedWithOrOnSyntax <T> InRequestScope <T>(IBindingInSyntax <T> syntax)
 {
     return(syntax
            .InScope(context =>
                     RevoStartup.RequestScope(context)
                     ?? (object)TaskContext.Current
                     ?? StandardScopeCallbacks.Thread(context)));
 }
 public IBindingNamedWithOrOnSyntax <T> InTaskScope <T>(IBindingInSyntax <T> syntax)
 {
     return(syntax
            .InScope(context =>
                     (object)TaskContext.Current
                     ?? context.Kernel.Components.GetAll <INinjectHttpApplicationPlugin>()
                     .Select(c => c.GetRequestScope(context)).FirstOrDefault(s => s != null)
                     ?? StandardScopeCallbacks.Thread(context)));
 }
Esempio n. 6
0
        /// <summary>
        /// Registers a component so all dependant components will resolve the same shared instance within the test
        /// lifetime scope.
        /// </summary>
        public static IBindingNamedWithOrOnSyntax <T> InstancePerTest <T>(this IBindingInSyntax <T> binding)
        {
            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }

            return(binding.InNamedScope(NinjectDependencyResolver.TestLifetimeScopeTag));
        }
 /// <summary>
 /// Defines that a binding is in the scope of its target.
 /// </summary>
 /// <typeparam name="T">The type of the binding.</typeparam>
 /// <param name="syntax">The In syntax.</param>
 /// <returns>The Named syntax.</returns>
 public static IBindingNamedWithOrOnSyntax <T> InParentScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(
                context =>
     {
         const string ScopeParameterName = "NamedScopeInParentScope";
         var parentContext = context.Request.ParentContext;
         return GetOrAddScope(parentContext, ScopeParameterName);
     }));
 }
Esempio n. 8
0
        /// <summary>
        /// Set scope based on <see cref="BindingLifecycle">ActivationScope</see>
        /// </summary>
        /// <param name="syntax"><see cref="IBindingInSyntax{T}">Binding syntax</see> to set the scope for</param>
        /// <param name="lifecycle"><see cref="BindingLifecycle"/> to use</param>
        public static void WithLifecycle <T>(this IBindingInSyntax <T> syntax, BindingLifecycle lifecycle)
        {
            switch (lifecycle)
            {
            case BindingLifecycle.Singleton:
                syntax.InSingletonScope();
                break;

            case BindingLifecycle.Transient:
                syntax.InTransientScope();
                break;
            }
        }
 /// <summary>
 /// Sets up the behavior of a binding for one instance per unit of work.
 /// A unit of work must be available at the time the binding is used for resolve.
 /// Use <see cref="UnitOfWorkScope.Create"/> to create a scope of type unit of work and remember to dispose it when you're done with it.
 /// </summary>
 public static IBindingNamedWithOrOnSyntax <T> InUnitOfWorkScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(
                context =>
     {
         var currentUnitOfWork = UnitOfWorkScope.Current;
         if (currentUnitOfWork == null)
         {
             throw new InvalidOperationException($"No unit-of-work context present for the IOC resolve of component {context?.Request?.Service}");
         }
         return currentUnitOfWork;
     }));
 }
        /// <summary>
        /// Defines that a binding is in the scope of its target.
        /// </summary>
        /// <typeparam name="T">The type of the binding.</typeparam>
        /// <param name="syntax">The In syntax.</param>
        /// <returns>The Named syntax.</returns>
        public static IBindingNamedWithOrOnSyntax <T> InCallScope <T>(this IBindingInSyntax <T> syntax)
        {
            return(syntax.InScope(
                       context =>
            {
                const string ScopeParameterName = "NamedScopeInCallScope";
                var rootContext = context;
                while (!IsCurrentResolveRoot(rootContext) && rootContext.Request.ParentContext != null)
                {
                    rootContext = rootContext.Request.ParentContext;
                }

                return GetOrAddScope(rootContext, ScopeParameterName);
            }));
        }
Esempio n. 11
0
 /// <summary>
 ///     Sets the lifecycle.
 /// </summary>
 protected virtual void SetLifecycle(IBindingInSyntax <object> syntax, DependencyLifecycle lifecycle)
 {
     if (lifecycle == DependencyLifecycle.SingleInstance)
     {
         syntax.InSingletonScope();
         return;
     }
     if (lifecycle == DependencyLifecycle.TransientInstance)
     {
         syntax.InTransientScope();
         return;
     }
     Should.MethodBeSupported(false,
                              "SetLifecycle(IBindingInSyntax<object> syntax, DependencyLifecycle lifecycle)");
 }
        /// <summary>
        /// Defines that a binding is in the scope of its target.
        /// </summary>
        /// <typeparam name="T">The type of the binding.</typeparam>
        /// <param name="syntax">The In syntax.</param>
        /// <returns>The Named syntax.</returns>
        public static IBindingNamedWithOrOnSyntax <T> InCallScope <T>(this IBindingInSyntax <T> syntax)
        {
            return(syntax.InScope(
                       context =>
            {
                const string ScopeParameterName = "NamedScopeInCallScope";
                var rootContext = context.Request.ParentContext ?? context;
                while (rootContext.Request.ParentContext != null &&
                       !IsBindingActivated(rootContext.Request.ParentContext))
                {
                    rootContext = rootContext.Request.ParentContext;
                }

                return GetOrAddScope(rootContext, ScopeParameterName);
            }));
        }
Esempio n. 13
0
        private static IBindingNamedWithOrOnSyntax <object> InScope(this IBindingInSyntax <object> syntax, ServiceDescriptor s, ScopedBindingResolver scopeBindingResolver)
        {
            switch (s.Lifetime)
            {
            case ServiceLifetime.Singleton:
                return(syntax.InSingletonScope());

            case ServiceLifetime.Scoped:
                return(scopeBindingResolver(s, syntax));

            case ServiceLifetime.Transient:
                return(syntax.InTransientScope());
            }

            throw new Exception("Invalid service descriptor binding");
        }
Esempio n. 14
0
        public static IBindingNamedWithOrOnSyntax <T> SetLifeStyle <T>(this IBindingInSyntax <T> registration, LifetimeScope lifeTimeKey)
        {
            switch (lifeTimeKey)
            {
            case LifetimeScope.Unowned:
                return(registration.InTransientScope());

            case LifetimeScope.PerHttpRequest:
                return(registration.InRequestScope());

            case LifetimeScope.PerThread:
                return(registration.InThreadScope());

            default:
                return(registration.InTransientScope());
            }
        }
        public static IBindingNamedWithOrOnSyntax <T> ConfigureLifecycle <T>(
            this IBindingInSyntax <T> bindingInSyntax,
            ServiceLifetime lifecycleKind)
        {
            switch (lifecycleKind)
            {
            case ServiceLifetime.Singleton:
                return(bindingInSyntax.InSingletonScope());

            case ServiceLifetime.Scoped:
                return(bindingInSyntax.InRequestScope());

            case ServiceLifetime.Transient:
                return(bindingInSyntax.InTransientScope());

            default:
                throw new NotSupportedException();
            }
        }
Esempio n. 16
0
        private static void inScope(this IBindingInSyntax <object> to, ServiceLifetime lifetime)
        {
            switch (lifetime)
            {
            case ServiceLifetime.Singleton:
                to.InSingletonScope();
                break;

            case ServiceLifetime.Scoped:
                to.InScope(ctx => ctx.Kernel);
                break;

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

            default:
                throw new ArgumentOutOfRangeException(nameof(lifetime), lifetime, null);
            }
        }
 /// <summary>
 /// Sets the scope using the given syntax.
 /// </summary>
 /// <param name="syntax">The syntax that is used to set the scope.</param>
 public void SetScope(IBindingInSyntax<object> syntax)
 {
     syntax.InRequestScope();
 }
 public static IBindingNamedWithOrOnSyntax <T> InRequestScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(new Func <IContext, object>(RequestScopeExtensionMethod.GetScope)));
 }
 /// <summary>
 /// Defines that a binding is in a named scope.
 /// </summary>
 /// <typeparam name="T">The type of the binding.</typeparam>
 /// <param name="syntax">The In syntax.</param>
 /// <param name="scopeParameterName">Name of the scope parameter.</param>
 /// <returns>The Named syntax.</returns>
 public static IBindingNamedWithOrOnSyntax <T> InNamedScope <T>(this IBindingInSyntax <T> syntax, string scopeParameterName)
 {
     return(syntax.InScope(context => GetNamedScope(context, scopeParameterName)));
 }
 public static void InPerUserCacheScope <T>(this IBindingInSyntax <T> parent)
 {
     parent.InScope(CacheScopeCallback);
 }
 /// <summary>
 ///     Sets the lifecycle.
 /// </summary>
 protected virtual void SetLifecycle(IBindingInSyntax<object> syntax, DependencyLifecycle lifecycle)
 {
     if (lifecycle == DependencyLifecycle.SingleInstance)
     {
         syntax.InSingletonScope();
         return;
     }
     if (lifecycle == DependencyLifecycle.TransientInstance)
     {
         syntax.InTransientScope();
         return;
     }
     Should.MethodBeSupported(false,
         "SetLifecycle(IBindingInSyntax<object> syntax, DependencyLifecycle lifecycle)");
 }
Esempio n. 22
0
 public static void InSessionScope <T>(this IBindingInSyntax <T> parent)
 {
     parent.InScope(SessionScopeCallback);
 }
Esempio n. 23
0
 public static IBindingNamedWithOrOnSyntax <T> InSessionScope <T>(this IBindingInSyntax <T> binding)
 {
     return(binding.InScope(ctx => CurrentScope));
 }
 public static IBindingNamedWithOrOnSyntax <T> InTaskScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(new Func <IContext, object>(NinjectExtensionInTaskScope.GetScope)));
 }
Esempio n. 25
0
 /// <summary>
 ///     Uses the IUnitOfWorkScopeResolver to resolve the scope.  A unit of work is typically a web request
 ///     or a job execution.
 /// </summary>
 public static IBindingNamedWithOrOnSyntax <T> InUnitOfWorkScope <T> (this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(context => context.Kernel.Get <IUnitOfWorkScopeResolver> ()
                           .Resolve(context)));
 }
 /// <summary>
 /// Sets the scope using the given syntax.
 /// </summary>
 /// <param name="syntax">The syntax that is used to set the scope.</param>
 public void SetScope(IBindingInSyntax <object> syntax)
 {
     syntax.InTransientScope();
 }
 /// <summary>
 /// Defines the unit of work scope on the requested service.
 /// </summary>
 /// <typeparam name="T">The requested service type.</typeparam>
 /// <param name="syntax">The syntax.</param>
 /// <returns>The binding.</returns>
 public static IBindingNamedWithOrOnSyntax <T> InUnitOfWorkScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InNamedScope(ScopeName));
 }
 /// <summary>
 /// Sets the scope using the given syntax.
 /// </summary>
 /// <param name="syntax">The syntax that is used to set the scope.</param>
 public void SetScope(IBindingInSyntax<object> syntax)
 {
     syntax.InSingletonScope();
 }
 /// <summary>
 /// Indicates that instances activated via the binding should be re-used
 /// within the same background job type instance.
 /// </summary>
 /// <typeparam name="T">Type of the service.</typeparam>
 /// <param name="syntax">Binding syntax.</param>
 /// <returns>The syntax to define more information.</returns>
 public static IBindingNamedWithOrOnSyntax <T> InBackgroundJobScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(c => NinjectJobActivator.CurrentAsync));
 }
 /// <summary>
 /// Indicates that instances activated via the binding should be re-used
 /// within the same named scope (if available), or within the same background
 /// job type instance.
 /// </summary>
 /// <typeparam name="T">Type of the service.</typeparam>
 /// <param name="syntax">Binding syntax.</param>
 /// <returns>The syntax to define more information.</returns>
 public static IBindingNamedWithOrOnSyntax <T> InNamedOrBackgroundJobScope <T>(this IBindingInSyntax <T> syntax, Func <IContext, object> scopeCallback)
 {
     return(syntax.InScope(context => scopeCallback(context) ?? NinjectJobActivator.CurrentAsync));
 }
 /// <summary>
 /// Sets the scope to request scope.
 /// </summary>
 /// <typeparam name="T">The type of the service.</typeparam>
 /// <param name="syntax">The syntax.</param>
 /// <returns>The syntax to define more information.</returns>
 public static IBindingNamedWithOrOnSyntax <T> InRequestScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(GetScope));
 }
Esempio n. 32
0
 public static IBindingNamedWithOrOnSyntax <T> InCustomScope <T>(this IBindingInSyntax <T> bindingSyntax)
 {
     return(bindingSyntax.InScope(CustomScope.ResolutionRootFromRequest));
 }
Esempio n. 33
0
 /// <summary>
 /// Sets the scope using the given syntax.
 /// </summary>
 /// <param name="syntax">The syntax that is used to set the scope.</param>
 public void SetScope(IBindingInSyntax <object> syntax)
 {
     syntax.InRequestScope();
 }
 /// <summary>
 /// Sets the scope using the given syntax.
 /// </summary>
 /// <param name="syntax">The syntax that is used to set the scope.</param>
 public void SetScope(IBindingInSyntax<object> syntax)
 {
     syntax.InTransientScope();
 }