Beispiel #1
0
        /// <summary>
        /// Registers a context with the current thread - always put a matching
        /// <see cref="Close"/> call in a finally block to ensure that the correct
        /// context is available in the enclosing block.
        /// </summary>
        /// <param name="stepExecution">the step context to register</param>
        /// <returns>a new StepContext or the current one if it has the same StepExecution</returns>
        public static StepContext Register(StepExecution stepExecution)
        {
            var context = Manager.Register(stepExecution);

            StepScopeSynchronization.ResetInstances();
            return(context);
        }
        /// <summary>
        /// Resolves the dependency by crating a proxy and registers it with <see cref="StepScopeSynchronization"/>.
        /// </summary>
        /// <param name="context">Current build context</param>
        /// <returns>The created proxy</returns>
        public object Resolve(IBuilderContext context)
        {
            var mappedType = StepScopeSynchronization.GetMappedType(_dependency.RegisteredType, _dependency.Name);
            var proxy      = ProxyFactory.Create(mappedType.GetInterfaces(), typeof(StepScopeProxyObject));

            StepScopeSynchronization.AddProxy(_dependency.RegisteredType, _dependency.Name, (IProxyObject)proxy);
            return(proxy);
        }
 private static void ResolveProperties(IBuilderContext context, IPropertySelectorPolicy propertySelector,
                                       IPolicyList resolverPolicyDestination, Dictionary <string, StepScopeDependency> properties)
 {
     foreach (var property in propertySelector.SelectProperties(context, resolverPolicyDestination))
     {
         var namedTypeResolver = property.Resolver as NamedTypeDependencyResolverPolicy;
         if (namedTypeResolver != null)
         {
             var type = namedTypeResolver.Type;
             var name = namedTypeResolver.Name;
             if (StepScopeSynchronization.IsStepScope(type, name))
             {
                 properties[property.Property.Name] = new StepScopeDependency(type, name);
             }
         }
     }
 }
        private void CheckStepScope(object sender, RegisterEventArgs args)
        {
            var isStepScope = args.LifetimeManager is StepScopeLifetimeManager;

            // First we need to check any registered dependency with the same registered type and name
            Container.Registrations.Where(r => r.RegisteredType != (args.TypeFrom ?? args.TypeTo) &&
                                          r.MappedToType == args.TypeTo && r.Name == args.Name).ForEach(r =>
            {
                if (StepScopeSynchronization.IsStepScope(r.MappedToType, r.Name))
                {
                    // if the previously registered dependency is in step scope, a newly defined
                    // liftetime manager takes precedence. If no lifetime manager is defined for
                    // the new dependency, it is in step scope.
                    if (args.LifetimeManager == null)
                    {
                        isStepScope = true;
                    }
                    if (!isStepScope)
                    {
                        StepScopeSynchronization.RemoveScopeDependency(r.RegisteredType, r.Name);
                        StepScopeSynchronization.RemoveScopeDependency(r.MappedToType, r.Name);
                    }
                }
                else if (isStepScope)
                {
                    // if the previously registered dependency is not in step scope but the new dependency
                    // is, the previously registered dependency is put in step scope.
                    if (r.RegisteredType != r.MappedToType)
                    {
                        StepScopeSynchronization.AddStepScopeDependency(r.RegisteredType, r.Name, Container, r.MappedToType);
                    }
                    StepScopeSynchronization.AddStepScopeDependency(r.MappedToType, r.Name, Container, r.MappedToType);
                }
            });
            // Finally, we register the new dependency in the step scope if required
            if (isStepScope)
            {
                if (args.TypeFrom != null)
                {
                    StepScopeSynchronization.AddStepScopeDependency(args.TypeFrom, args.Name, Container, args.TypeTo);
                }
                StepScopeSynchronization.AddStepScopeDependency(args.TypeTo, args.Name, Container, args.TypeTo);
            }
        }
        private static void ResolveConstructorParameters(SelectedConstructor constructor, Dictionary <string, StepScopeDependency> constructorParameters)
        {
            var parameterResolvers = constructor.GetParameterResolvers();

            for (var i = 0; i < parameterResolvers.Length; i++)
            {
                var namedTypeResolver = parameterResolvers[i] as NamedTypeDependencyResolverPolicy;
                if (namedTypeResolver != null)
                {
                    var type = namedTypeResolver.Type;
                    var name = namedTypeResolver.Name;
                    if (StepScopeSynchronization.IsStepScope(type, name))
                    {
                        var parameter = constructor.Constructor.GetParameters()[i];
                        constructorParameters[parameter.Name] = new StepScopeDependency(type, name);
                    }
                }
            }
        }
 private static void ResolveMethodParameters(IBuilderContext context, IMethodSelectorPolicy methodSelector,
                                             IPolicyList resolverPolicyDestination, Dictionary <Tuple <string, string>, IList <StepScopeDependency> > methodParameters)
 {
     foreach (var method in methodSelector.SelectMethods(context, resolverPolicyDestination))
     {
         var parameterResolvers = method.GetParameterResolvers();
         for (var i = 0; i < parameterResolvers.Length; i++)
         {
             var namedTypeResolver = parameterResolvers[i] as NamedTypeDependencyResolverPolicy;
             if (namedTypeResolver != null)
             {
                 var type = namedTypeResolver.Type;
                 var name = namedTypeResolver.Name;
                 if (StepScopeSynchronization.IsStepScope(type, name))
                 {
                     var parameter = method.Method.GetParameters()[i];
                     var key       = new Tuple <string, string>(GetSignature(method.Method), parameter.Name);
                     AddMethodParameterDependency(methodParameters, key,
                                                  new StepScopeDependency(type, name));
                 }
             }
         }
     }
 }
 /// <summary>
 /// A convenient "deep" close operation. Call this instead of
 /// <see cref="Close"/> if the step execution for the current context is ending.
 /// Delegates to StepContext<see cref="Close"/> and then ensures that
 /// <see cref="Close"/> is also called in a finally block.
 /// </summary>
 public static void Release()
 {
     Manager.Release();
     StepScopeSynchronization.ResetInstances();
 }