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; } }
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))); } }
public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance) { if (pluginType == typeof(IService)) { yield return new FuncInterceptor<IService>((context, service) => new DecoratorService(instance.Name, service)); } }
public ValidationError(Type pluginType, Instance instance, Exception exception, MethodInfo method) { PluginType = pluginType; Instance = instance; Exception = exception; MethodName = method.Name; }
public void FillTypeInto(Type pluginType, Instance instance) { if (!_instances.ContainsKey(pluginType)) { _instances.Add(pluginType, instance); } }
/// <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; }
public bool Has(Type pluginType, Instance instance) { return _lock.Read(() => { var key = instance.InstanceKey(pluginType); return _objects.ContainsKey(key); }); }
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; }
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); } }
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); }
/// <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); } }
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; } }
public object Get(Type pluginType, Instance instance, IBuildSession session) { var @object = session.BuildNewInSession(pluginType, instance); if (@object is IDisposable) { _tracked.Add(@object); } return @object; }
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); }
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(); }); }); }
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()); } }
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(); }
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); } }
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(); }
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>; }
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); } }
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; } }
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); } }
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>(); }