/// <summary> /// Tries to get a named scope from the request. /// </summary> /// <param name="context">The context.</param> /// <param name="scopeParameterName">Name of the scope parameter.</param> /// <returns>The scope, null if not found.</returns> /// <exception cref="ScopeDisposedException">Thrown when the scope is already disposed.</exception> public static object TryGetNamedScope(this IContext context, string scopeParameterName) { NamedScopeParameter namedScopeParameter = GetNamedScopeParameter(context, scopeParameterName); if (namedScopeParameter != null) { if (namedScopeParameter.Scope.IsDisposed) { throw new ScopeDisposedException(); } { return(namedScopeParameter.Scope); } } if (context.Request.ParentContext != null) { { return(context.Request.ParentContext.TryGetNamedScope(scopeParameterName)); } } return(null); }
/// <summary> /// Gets the specified named scope or adds a scope with the specified name in case it does not exist yet. /// </summary> /// <param name="parentContext">The parent context.</param> /// <param name="scopeParameterName">Name of the scope parameter.</param> /// <returns>The requested scope.</returns> private static object GetOrAddScope(IContext parentContext, string scopeParameterName) { var namedScopeParameter = GetNamedScopeParameter(parentContext, scopeParameterName); if (namedScopeParameter == null) { namedScopeParameter = new NamedScopeParameter(scopeParameterName); parentContext.Parameters.Add(namedScopeParameter); } return(namedScopeParameter.Scope); }
/// <summary> /// Gets a named scope from the request. /// </summary> /// <param name="context">The context.</param> /// <param name="scopeParameterName">Name of the scope parameter.</param> /// <returns>The scope.</returns> private static object GetScope(IContext context, string scopeParameterName) { NamedScopeParameter namedScopeParameter = GetNamedScopeParameter(context, scopeParameterName); if (namedScopeParameter != null) { if (namedScopeParameter.Scope.IsDisposed) { throw new ScopeDisposedException(); } return(namedScopeParameter.Scope); } if (context.Request.ParentContext != null) { return(GetScope(context.Request.ParentContext, scopeParameterName)); } throw new UnknownScopeException(scopeParameterName); }
public void ActivationCreatesNamedScopeReferenceUsingTheKernel() { var requestParameters = new List <IParameter>(); var reference = new InstanceReference { Instance = new object() }; var namedScopeParameter = new NamedScopeParameter("Scope1"); var kernelMock = new Mock <IKernel>(); var contextMock = CreateContextMock(); contextMock.Object.Parameters.Add(namedScopeParameter); contextMock.SetupGet(context => context.Kernel).Returns(kernelMock.Object); SetupKernelGetNamedScopeReference(kernelMock, requestParameters); var testee = new NamedScopeActivationStrategy(); testee.Activate(contextMock.Object, reference); requestParameters.Count().Should().Be(2); AssertConstructorArgumentExists("scope", namedScopeParameter.Scope, requestParameters); AssertNamedScopeReferenceScopeParameterExists(reference.Instance, requestParameters); }