public override object FindObject(Type pluginType, Instance instance)
        {
            _dependencyStack.Push(new BuildDependency(pluginType, instance));

            try
            {
                //clearBuildStack();
                return base.FindObject(pluginType, instance);
            }
            catch (StructureMapException ex)
            {
                _dependencyStack.Pop();

                // Don't log exceptions for inline instances.  I
                // think it would cause more confusion that not
                // because the name is a Guid
                if (!_explicitInstances.Contains(instance))
                {
                    throw;
                }

                _errors.LogError(instance, pluginType, ex, _dependencyStack);

                throw;
            }
        }
Пример #2
0
        public void Apply(Type pluginType, Instance instance)
        {
            _policies.Where(x => !instance.AppliedPolicies.Contains(x))
                .Each(policy =>
                {
                    try
                    {
                        policy.Apply(pluginType, instance);
                    }
                    finally
                    {
                        instance.AppliedPolicies.Add(policy);
                    }
                });

            var configured = instance.As<IConfiguredInstance>();
            if (configured != null)
            {
                configured.Constructor.GetParameters()
                    .Each(param => param.ForAttribute<StructureMapAttribute>(att => att.Alter(configured, param)));

                configured.SettableProperties()
                    .Each(prop => prop.ForAttribute<StructureMapAttribute>(att => att.Alter(configured, prop)));
            }
        }
Пример #3
0
 public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance)
 {
     if (pluginType == typeof(IService))
     {
         yield return new FuncInterceptor<IService>((context, service) => new DecoratorService(instance.Name, service));
     }
 }
Пример #4
0
 public ValidationError(Type pluginType, Instance instance, Exception exception, MethodInfo method)
 {
     PluginType = pluginType;
     Instance = instance;
     Exception = exception;
     MethodName = method.Name;
 }
Пример #5
0
 public void FillTypeInto(Type pluginType, Instance instance)
 {
     if (!_instances.ContainsKey(pluginType))
     {
         _instances.Add(pluginType, instance);
     }
 }
Пример #6
0
 /// <summary>
 /// FOR TESTING ONLY!
 /// </summary>
 /// <param name="pluginType"></param>
 /// <param name="instance"></param>
 /// <param name="inner"></param>
 /// <param name="interceptionPlan"></param>
 public BuildPlan(Type pluginType, Instance instance, IDependencySource inner, IInterceptionPlan interceptionPlan)
 {
     _pluginType = pluginType;
     _instance = instance;
     _inner = inner;
     _interceptionPlan = interceptionPlan;
 }
Пример #7
0
 public bool Has(Type pluginType, Instance instance)
 {
     return _lock.Read(() => {
         var key = instance.InstanceKey(pluginType);
         return _objects.ContainsKey(key);
     });
 }
Пример #8
0
        public object Get(Type pluginType, Instance instance, IBuildSession session)
        {
            object result = null;
            int key = instance.InstanceKey(pluginType);
            _lock.EnterUpgradeableReadLock();
            if (_objects.ContainsKey(key))
            {

                result = _objects[key];
                _lock.ExitUpgradeableReadLock();
            }
            else
            {
                _lock.EnterWriteLock();
                try
                {
                    result = buildWithSession(pluginType, instance, session);
                    _objects.Add(key, result);
                }
                finally
                {
                    _lock.ExitWriteLock();
                }
            }

            return result;
        }
Пример #9
0
        public void LogError(
            Instance instance,
            Type pluginType,
            StructureMapException ex,
            IEnumerable<BuildDependency> dependencies)
        {
            if (_buildErrors.ContainsKey(instance))
            {
                BuildError existingError = _buildErrors[instance];
                addDependenciesToError(instance, dependencies, existingError);
            }

            if (_brokenInstances.Contains(instance))
            {
                return;
            }

            InstanceToken token = ((IDiagnosticInstance) instance).CreateToken();
            var error = new BuildError(pluginType, instance);
            error.Exception = ex;

            _buildErrors.Add(instance, error);

            addDependenciesToError(instance, dependencies, error);
        }
 public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance)
 {
     if (MatchesType(instance.ReturnedType))
      {
      Expression<Action<IContext, object>> register = (c, o) => c.GetInstance<IEventPublisher>().Subscribe(o);
      yield return new ActivatorInterceptor<object>(register);
      }
 }
Пример #11
0
        public static Expression WrapFunc(Type returnType, Instance instance, Expression inner)
        {
            var push = Expression.Call(Parameters.Session, PushMethod, Expression.Constant(instance));
            var block = Expression.Block(returnType, push, inner);
            var pop = Expression.Call(Parameters.Session, PopMethod);

            return Expression.TryFinally(block, pop);
        }
Пример #12
0
 /// <summary>
 /// Applies explicit configuration to an IConfiguredInstance
 /// </summary>
 /// <param name="pluginType"></param>
 /// <param name="instance"></param>
 public void Apply(Type pluginType, Instance instance)
 {
     var configured = instance as IConfiguredInstance;
     if (configured != null)
     {
         apply(pluginType, configured);
     }
 }
Пример #13
0
        public void Eject(Type pluginType, Instance instance)
        {
            var key = new InstanceKey(instance, pluginType);
            if (!_objects.Has(key)) return;

            var disposable = _objects[key] as IDisposable;
            _objects.Remove(key);
            disposable.SafeDispose();
        }
 public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance)
 {
     if (_filter(pluginType, instance))
     {
         var interceptorType = typeof(DynamicProxyInterceptor<>).MakeGenericType(pluginType);
         var interceptor = (IInterceptor)Activator.CreateInstance(interceptorType, new object[] { _interceptionBehaviors });
         yield return interceptor;
     }
 }
Пример #15
0
        public object Get(Type pluginType, Instance instance, IBuildSession session)
        {
            var @object = session.BuildNewInSession(pluginType, instance);
            if (@object is IDisposable)
            {
                _tracked.Add(@object);
            }

            return @object;
        }
Пример #16
0
        protected MessageHandlerInvoker(Type handlerType, Type messageType, bool? shouldBeSubscribedOnStartup = null)
        {
            MessageHandlerType = handlerType;
            DispatchQueueName = DispatchQueueNameScanner.GetQueueName(handlerType);
            MessageType = messageType;
            MessageTypeId = new MessageTypeId(MessageType);
            ShouldBeSubscribedOnStartup = shouldBeSubscribedOnStartup ?? MessageShouldBeSubscribedOnStartup(messageType);

            _instance = CreateConstructorInstance(handlerType);
        }
        public bool HasBeenCreated(Instance instance)
        {
            var lifecycle = _pipelineGraph.DetermineLifecycle(_family.PluginType, instance);

            // Fixes GH-363
            if (lifecycle is ObjectLifecycle) return true;

            return
                lifecycle
                    .FindCache(_pipelineGraph)
                    .Has(_family.PluginType, instance);
        }
Пример #18
0
        public void Eject(Type pluginType, Instance instance)
        {
            int key = instance.InstanceKey(pluginType);
            _lock.MaybeWrite(() => {
                if (!_objects.ContainsKey(key)) return;

                _lock.Write(() => {
                    var disposable = _objects[key] as IDisposable;
                    _objects.Remove(key);
                    disposable.SafeDispose();
                });
            });
        }
Пример #19
0
        public virtual object ApplyInterception(Type pluginType, object actualValue, BuildSession session,
            Instance instance)
        {
            if (actualValue == null) return null;

            try
            {
                return _library.FindInterceptor(actualValue.GetType()).Process(actualValue, session);
            }
            catch (Exception e)
            {
                throw new StructureMapException(308, e, instance.Name, actualValue.GetType());
            }
        }
Пример #20
0
        public void SetUp()
        {
            theTarget = new BuildTarget();
            theInstance = new ObjectInstance(theTarget);

            theInterceptors = new IInterceptor[0];

            theInner = Constant.For(theTarget);

            _plan =
                new Lazy<BuildPlan>(() => new BuildPlan(typeof (IBuildTarget), theInstance, theInner, new Policies(), theInterceptors));

            theSession = new FakeBuildSession();
        }
Пример #21
0
        private void addDependenciesToError(Instance instance, IEnumerable<BuildDependency> dependencies,
                                            BuildError error)
        {
            foreach (BuildDependency dependency in dependencies)
            {
                if (_brokenInstances.Contains(instance))
                {
                    continue;
                }

                error.AddDependency(dependency);
                _brokenInstances.Add(dependency.Instance);
            }
        }
Пример #22
0
        public BuildPlanTester()
        {
            theTarget = new BuildTarget();
            theInstance = new ObjectInstance(theTarget);

            theInterceptors = new IInterceptor[0];

            theInner = Constant.For(theTarget);

            plan =
                new Lazy<BuildPlan>(
                    () => new BuildPlan(typeof(IBuildTarget), theInstance, theInner, Policies.Default(), theInterceptors));

            theSession = new FakeBuildSession();
        }
Пример #23
0
        public BuildPlan(Type pluginType, Instance instance, IDependencySource inner, Policies policies,
            IEnumerable<IInterceptor> interceptors)
        {
            _pluginType = pluginType;
            _instance = instance;
            _inner = inner;

            if (interceptors.Any())
            {
                _interceptionPlan = new InterceptionPlan(pluginType, _inner, policies, interceptors);
            }

            var @delegate = ToDelegate();
            _func = @delegate as Func<IBuildSession, IContext, object>;
        }
Пример #24
0
        public void Set(Type pluginType, Instance instance, object value)
        {
            if (value == null) return;

            try
            {
                var key = new InstanceKey(instance, pluginType);
                _objects[key] = value;
            }
            catch (ArgumentException e)
            {
                string message = string.Format("Duplicate key for Instance {0} of PluginType {1}", instance.Name,
                                               pluginType.AssemblyQualifiedName);
                throw new ArgumentException(message, e);
            }
        }
Пример #25
0
        public object Resolve(Type pluginType, Instance instance, BuildSession session)
        {
            IObjectCache cache = FindCache(pluginType, instance, session);
            lock (cache.Locker)
            {
                object returnValue = cache.Get(pluginType, instance);
                if (returnValue == null)
                {
                    returnValue = ConstructNew(pluginType, instance, session);

                    cache.Set(pluginType, instance, returnValue);
                }

                return returnValue;
            }
        }
Пример #26
0
        private void tryBuildInstance(Type pluginType, Instance instance, IPipelineGraph pipeline, ProfileReport report)
        {
            var session = new BuildSession(pipeline, instance.Name);

            try
            {
                var @object = session.FindObject(pluginType, instance);
                validate(pluginType, instance, @object, report);
            }
            catch (StructureMapException ex)
            {
                ex.Instances.Each(x => _buildPlanFailureIds.Fill(x));

                report.AddError(pluginType, instance, ex);
            }
        }
Пример #27
0
        public void Eject(Type pluginType, Instance instance)
        {
            // Prevent null reference exception.
            if (pluginType.AssemblyQualifiedName == null)
                return;

            var key = instance.InstanceKey(pluginType);
            _lock.MaybeWrite(() => {
                if (!_objects.ContainsKey(key)) return;

                _lock.Write(() => {
                    var disposable = _objects[key] as IDisposable;
                    _objects.Remove(key);
                    disposable.SafeDispose();
                });
            });
        }
        public object Build(BuildSession buildSession, Type pluginType, Instance instance)
        {
            var constructorArgs = _ConcreteType
                .GetConstructors()
                .FirstOrDefault()
                .GetParameters()
                .Select(p => buildSession.CreateInstance(p.ParameterType))
                .ToArray();

            var interceptors = new List<IInterceptor>
                                   {
                                       new NotifyInterceptor()
                                   }
                                   .ToArray();

            return new ProxyGenerator().CreateClassProxy(_ConcreteType, interceptors, constructorArgs);
        }
        public void build_children_to_a_list()
        {
            var children = new Instance[]
            {
                new SmartInstance<ColorWidget>().Ctor<string>("color").Is("red"),
                new SmartInstance<ColorWidget>().Ctor<string>("color").Is("green"),
                new ObjectInstance(new AWidget())
            };

            var theInstance = new EnumerableInstance(children);
            var list =
                theInstance.Build<IList<IWidget>>(new StubBuildSession()).As<IList<IWidget>>();

            list.Count.ShouldBe(3);

            list[0].ShouldBeOfType<ColorWidget>().Color.ShouldBe("red");
            list[2].ShouldBeOfType<AWidget>();
        }
        public void build_children_to_an_array()
        {
            var children = new Instance[]
            {
                new SmartInstance<ColorWidget>().WithCtorArg("color").EqualTo("red"),
                new SmartInstance<ColorWidget>().WithCtorArg("color").EqualTo("green"),
                new ObjectInstance(new AWidget())
            };

            var theInstance = new EnumerableInstance(typeof (IWidget[]), children);

            var list = theInstance.Build(typeof (IWidget[]), new StubBuildSession()).ShouldBeOfType<IWidget[]>();

            list.Length.ShouldEqual(3);

            list[0].ShouldBeOfType<ColorWidget>().Color.ShouldEqual("red");
            list[2].ShouldBeOfType<AWidget>();
        }