Beispiel #1
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)));
 }
 /// <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);
     }));
 }
 /// <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);
            }));
        }
        /// <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);
            }));
        }
Beispiel #7
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);
            }
        }
 public static void InPerUserCacheScope <T>(this IBindingInSyntax <T> parent)
 {
     parent.InScope(CacheScopeCallback);
 }
 public static IBindingNamedWithOrOnSyntax <T> InRequestScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(new Func <IContext, object>(RequestScopeExtensionMethod.GetScope)));
 }
 /// <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>
 /// 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));
 }
Beispiel #12
0
 public static IBindingNamedWithOrOnSyntax <T> InTenantSingletonScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(context => context.Kernel.Get <ITenantContext>()?.Tenant ?? NoTenantContext));
 }
Beispiel #13
0
 public static IBindingNamedWithOrOnSyntax <T> InCustomScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(t => ProcessingScope.Current ?? (object)HttpContext.Current)); //InRequestScope() is based on "HttpContext.Current"
 }
 /// <summary>
 /// Sets the scope to ambient scope and inject as transient within the current ambient 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> InAmbientScopeAsTransient <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(GetAmbientScopeAsTransient));
 }
 public static IBindingNamedWithOrOnSyntax <T> InTaskScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(new Func <IContext, object>(NinjectExtensionInTaskScope.GetScope)));
 }
Beispiel #16
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)));
 }
Beispiel #17
0
 public static IBindingNamedWithOrOnSyntax <T> InSessionScope <T>(this IBindingInSyntax <T> binding)
 {
     return(binding.InScope(ctx => CurrentScope));
 }
 public static IBindingNamedWithOrOnSyntax <T> InCustomScope <T>(this IBindingInSyntax <T> bindingSyntax)
 {
     return(bindingSyntax.InScope(CustomScope.ResolutionRootFromRequest));
 }
 public IBindingNamedWithOrOnSyntax <T> InTaskScope <T>(IBindingInSyntax <T> syntax)
 {
     return(syntax
            .InScope(context => TaskContext.Current ?? StandardScopeCallbacks.Thread(context)));
 }
Beispiel #20
0
 public static void InSessionScope <T>(this IBindingInSyntax <T> parent)
 {
     parent.InScope(SessionScopeCallback);
 }
 /// <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)));
 }
 /// <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));
 }
 public static IBindingNamedWithOrOnSyntax <T> InSession <T>(this IBindingInSyntax <T> bis)
 {
     return(bis.InScope(_ => IOC.Instance.SessionId));
 }
 public static IBindingNamedWithOrOnSyntax <T> InMainContextScope <T>(this IBindingInSyntax <T> syntax)
 {
     return(syntax.InScope(x => ((MainContext)x.Kernel.GetService(typeof(MainContext)))));
 }