protected override object build(Type pluginType, BuildSession session) { Current = session.BuildStack.Current; Root = session.BuildStack.Root; return new ColorRule("Red"); }
protected override object build(Type pluginType, BuildSession session) { Type elementType = _coercion.ElementType; IEnumerable<object> objects = buildObjects(elementType, session); return _coercion.Convert(objects); }
protected override object build(Type pluginType, BuildSession session) { if (EnumerableInstance.IsEnumerable(pluginType)) { var enumerable = new EnumerableInstance(pluginType, null); return enumerable.Build(pluginType, session); } return session.CreateInstance(pluginType); }
public T Get <T>(string propertyName, BuildSession session) { object o = Get(propertyName, typeof(T), session); if (o == null) { return(default(T)); } return((T)o); }
public void Throw_exception_When_trying_to_build_an_instance_that_cannot_be_found() { var graph = PipelineGraph.BuildEmpty(); var ex = Exception <StructureMapConfigurationException> .ShouldBeThrownBy(() => { var session = new BuildSession(graph); session.CreateInstance(typeof(IGateway), "Gateway that is not configured"); }); ex.Title.ShouldEqual("Could not find an Instance named 'Gateway that is not configured' for PluginType StructureMap.Testing.Widget3.IGateway"); }
public void Create_SimpleType_NamedMember() { var session = new BuildSession(null, null, new Random(0)); var obj = new SimpleType(); session.PushObject(new ObjectBuildRecord(typeof(SimpleType), obj, true)); session.PushMember(MemberExpressions.GetMemberInfo <SimpleType>(c => c.NamedMember)); Assert.AreEqual("NamedMember0", Generator.CreateRecord(typeof(string), null, session).Object); Assert.AreEqual("NamedMember1", Generator.CreateRecord(typeof(string), null, session).Object); Assert.AreEqual("NamedMember2", Generator.CreateRecord(typeof(string), null, session).Object); }
public void GetCollectionSize_ReturnsDefinedRange() { var stringRange = new Range(-10, 1); var defRange = new Range(10, 1000); _registry.SetForType <string>(stringRange); _session = new BuildSession(new TestBuilder { CollectionSizes = _registry, DefaultCollectionSize = defRange }, null, _random); Assert.AreSame(stringRange, _session.GetCollectionSize(typeof(string))); }
public void SetUp() { PluginCache.ResetAll(); builder = PluginCache.FindBuilder(typeof(ClassWithMixOfSetters)); instance = new SmartInstance <ClassWithMixOfSetters>().WithCtorArg("Age").EqualTo(34); _session = new BuildSession(); TheDefaultGateway = new DefaultGateway(); _session.RegisterDefault(typeof(IGateway), TheDefaultGateway); _target = null; }
public bool IsForMember(MemberInfo member, IBuilder builder, BuildSession session) { switch (member.MemberType) { case MemberTypes.Property: var propertyType = member as PropertyInfo; return(propertyType != null && propertyType.GetIndexParameters().Length == 0 && !session.ShouldIgnoreUnset(session.CurrentObject.Type)); default: return(!session.ShouldIgnoreUnset(session.CurrentObject.Type)); } }
protected override object build(Type pluginType, BuildSession session) { // Find the first InstanceCase that matches the BuildSession/IContext InstanceCase instanceCase = _cases.Find(c => c.Predicate(session)); // Use the Instance from the InstanceCase if it exists, // otherwise, use the "default" Instance instance = instanceCase == null ? _default : instanceCase.Instance; // delegate to the chosen Instance return(instance.Build(pluginType, session)); }
public BuildUpTester() { theDefaultGateway = new DefaultGateway(); var args = new ExplicitArguments(); args.Set<IGateway>(theDefaultGateway); session = BuildSession.Empty(args); theDependencies = new DependencyCollection { { "Age", 34 } }; target = null; }
public bool GeneratesType(Type type, IBuilder builder, BuildSession session) { var dictionaryInterface = GetIDictionaryInterface(type); if (dictionaryInterface != null) { var dictionaryType = typeof(Dictionary <, >) .MakeGenericType(KeyType(dictionaryInterface), ValueType(dictionaryInterface)); return(type.IsAssignableFrom(dictionaryType)); } return(false); }
public void push_and_pop_an_instance_onto_a_session() { var session = BuildSession.ForPluginGraph(PluginGraph.CreateRoot()); session.Push(new SmartInstance <AWidget>()); session.Push(new LambdaInstance <StubbedGateway>(c => new StubbedGateway())); session.Push(new SmartInstance <ARule>()); session.Pop(); session.ParentType.ShouldBe(typeof(AWidget)); }
public void SetUp() { TheDefaultGateway = new DefaultGateway(); var args = new ExplicitArguments(); args.Set<IGateway>(TheDefaultGateway); builder = new Plugin(typeof (ClassWithMixOfSetters)).CreateBuilder(); instance = new SmartInstance<ClassWithMixOfSetters>().Ctor<int>("Age").Is(34); _session = BuildSession.Empty(args); _target = null; }
public void SetUp() { instance = new ConditionalInstance <Rule>(x => { x.If(context => context.RequestedName == "red").ThenIt.IsThis(Red); x.If(context => context.RequestedName == "green").ThenIt.IsThis(Green); x.If(context => context.RequestedName == "blue").ThenIt.IsThis(Blue); x.TheDefault.IsThis(Default); }); _session = new BuildSession(); }
public void Process(object obj, IBuilder builder, BuildSession session) { var databaseEntity = (DatabaseEntity)obj; // Need to guard against the entity having already been saved // The instance Processor is called after ever object is constructed and set, // but in instances where an object is being re-used, the instance processor will // still be called if (!databaseEntity.IsSaved) { _session.Save(databaseEntity); } }
protected override object build(Type pluginType, BuildSession session) { Control control = new Page().LoadControl(_url); Type pluggedType = control.GetType(); if (!pluggedType.CanBeCastTo(pluginType)) { throw new StructureMapException(303, pluginType, pluggedType); } return(control); }
object GetSpecialParameter(Type pluginType, BuildSession session) { if (pluginType == typeof(IBus)) { return(_bus); } if (pluginType == typeof(MessageContext)) { return(_messageContext); } return(new DefaultInstance().Build(pluginType, session)); }
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 When_calling_GetInstance_if_no_default_can_be_found_throw_202() { var graph = PipelineGraph.BuildEmpty(); var ex = Exception <StructureMapConfigurationException> .ShouldBeThrownBy(() => { var session = new BuildSession(graph); session.GetInstance(typeof(IGateway)); }); ex.Context.ShouldContain("There is no configuration specified for StructureMap.Testing.Widget3.IGateway"); ex.Title.ShouldEqual("No default Instance is registered and cannot be automatically determined for type 'StructureMap.Testing.Widget3.IGateway'"); }
public void SetUp() { var registry = new Registry(); registry.For <Rule>(); registry.Scan(x => { x.Assembly("StructureMap.Testing.Widget"); x.Assembly("StructureMap.Testing.Widget2"); }); var graph = registry.Build(); _session = BuildSession.ForPluginGraph(graph); }
public ConfiguredInstanceTester() { var registry = new Registry(); registry.For<Rule>(); registry.Scan(x => { x.Assembly("StructureMap.Testing.Widget"); x.Assembly("StructureMap.Testing.Widget2"); }); var graph = registry.Build(); _session = BuildSession.ForPluginGraph(graph); }
public void SetUp() { TheDefaultGateway = new DefaultGateway(); var args = new ExplicitArguments(); args.Set <IGateway>(TheDefaultGateway); builder = new Plugin(typeof(ClassWithMixOfSetters)).CreateBuilder(); instance = new SmartInstance <ClassWithMixOfSetters>().Ctor <int>("Age").Is(34); _session = BuildSession.Empty(args); _target = null; }
public void PushChild_NullParent_NonNullObject() { _session = ConstructSession(); var childObject = new object(); _session.PushObject(new ObjectBuildRecord(typeof(object), null, true)); var expectedParent = _session.CurrentObject; _session.PushObject(new ObjectBuildRecord(typeof(object), childObject, true)); Assert.AreSame(childObject, _session.CurrentObject.Object); Assert.AreEqual(1, _session.BuildDepth); Assert.AreSame(expectedParent, _session.CurrentObject.Parent); }
public BuildUpTester() { theDefaultGateway = new DefaultGateway(); var args = new ExplicitArguments(); args.Set <IGateway>(theDefaultGateway); session = BuildSession.Empty(args); theDependencies = new DependencyCollection { { "Age", 34 } }; target = null; }
protected override object build(Type pluginType, BuildSession session) { if (_builder == null) { lock (_locker) { if (_builder == null) { _builder = session.CreateBuilder(Plugin); } } } return(Build(pluginType, session, _builder)); }
public void push_an_instance_onto_a_session() { var session = BuildSession.ForPluginGraph(new PluginGraph()); session.Push(new LambdaInstance <StubbedGateway>(c => new StubbedGateway())); session.ParentType.ShouldBeNull(); session.Push(new SmartInstance <ARule>()); session.ParentType.ShouldBe(typeof(StubbedGateway)); session.Push(new SmartInstance <AWidget>()); session.ParentType.ShouldBe(typeof(ARule)); }
public ObjectBuildRecord CreateRecord(Type type, IBuilder builder, BuildSession session) { var key = session.CurrentMember != null ? session.CurrentMember.Name : type.Name; int lastCount; if (!_memberCounts.TryGetValue(key, out lastCount)) { lastCount = 0; _memberCounts.Add(key, lastCount); } var newString = key + (lastCount); _memberCounts[key] = lastCount + 1; return(new ObjectBuildRecord(type, newString, false)); }
public void when_retrieving_an_object_by_name() { var red = new ColorService("red"); var green = new ColorService("green"); var graph = PluginGraph.CreateRoot(); var family = graph.Families[typeof(IService)]; family.AddInstance(new ObjectInstance(red).Named("red")); family.AddInstance(new ObjectInstance(green).Named("green")); var session = BuildSession.ForPluginGraph(graph); session.GetInstance <IService>("red").ShouldBeTheSameAs(red); }
public void when_retrieving_by_try_get_named_instance_with_nongeneric_type_that_does_exist() { var red = new ColorService("red"); var green = new ColorService("green"); var registry = new Registry(); registry.For <IService>().Add(red).Named("red"); registry.For <IService>().Add(green).Named("green"); var graph = registry.Build(); var session = BuildSession.ForPluginGraph(graph); session.TryGetInstance(typeof(IService), "red").ShouldBeTheSameAs(red); }
public void when_retrieving_an_object_by_nongeneric_type_and_name() { var red = new ColorService("red"); var green = new ColorService("green"); var registry = new Registry(); registry.For <IService>().Add(red).Named("red"); registry.For <IService>().Add(green).Named("green"); var graph = registry.Build(); var session = BuildSession.ForPluginGraph(graph); session.GetInstance(typeof(IService), "red").ShouldBeTheSameAs(red); }
public ObjectBuildRecord CreateRecord(Type type, IBuilder builder, BuildSession session) { var list = new List <TClassType>(); if (session.AvailableBuildDepth >= 1) { foreach (var memberValue in RandomExtensions.Shuffle(EnumUtils.GetValues <TType>(), session.Random)) { var entry = builder.Build <TClassType>(); _member.SetValue(entry, memberValue); list.Add(entry); } } return(new ObjectBuildRecord(type, list, false)); }
public virtual object Build(Type pluginType, BuildSession session) { markBuildStackStart(session, pluginType); // "Build" the desired object object rawValue = createRawObject(pluginType, session); // Allow the Interceptor a chance to enhance, configure, // wrap with a decorator, or even replace the rawValue object finalValue = applyInterception(rawValue, pluginType, session); markBuildStackFinish(session); return(finalValue); }
private object createRawObject(Type pluginType, BuildSession session) { try { return(build(pluginType, session)); } catch (StructureMapException) { throw; } catch (Exception ex) { throw new StructureMapException(400, ex); } }
public void when_retrieving_an_object_by_name() { var red = new ColorService("red"); var green = new ColorService("green"); var graph = new PluginGraph(); PluginFamily family = graph.FindFamily(typeof(IService)); family.AddInstance(new ObjectInstance(red).WithName("red")); family.AddInstance(new ObjectInstance(green).WithName("green")); var session = new BuildSession(graph); session.GetInstance <IService>("red").ShouldBeTheSameAs(red); }
public void Can_get_an_instance_using_the_non_generic_method() { var registry = new Registry(); registry.For <IFooService>().Use <Service>(); var graph = registry.Build(); var session = new BuildSession(graph); var instance = session.GetInstance(typeof(IFooService)); instance.ShouldNotBeNull(); instance.ShouldBeOfType <Service>(); }
public void PushChild_NullObject() { _session = ConstructSession(); var expectedParent = _session.CurrentObject; _session.PushObject(new ObjectBuildRecord(typeof(object), null, true)); Assert.IsNull(_session.CurrentObject.Object); Assert.AreEqual(0, _session.BuildDepth); Assert.AreSame(expectedParent, _session.CurrentObject.Parent); Assert.AreEqual(1, _session.CurrentObject.Parent.Children.Count); Assert.IsNull(_session.CurrentObject.Parent.Children[0].Object); Assert.AreEqual(1, _session.ConstructedNodes.Count); Assert.IsNotNull(_session.ConstructedNodes[0]); Assert.IsNull(_session.ConstructedNodes[0].Object); }
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 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 CanMakeAClassWithNoConstructorParametersWithoutADefinedMemento() { var registry = new Registry(); registry.Scan(x => x.Assembly("StructureMap.Testing.Widget3")); registry.BuildInstancesOf<IGateway>(); PluginGraph graph = registry.Build(); var pipelineGraph = new PipelineGraph(graph); var session = new BuildSession(graph); var gateway = (DefaultGateway) session.CreateInstance(typeof (IGateway), "Default"); Assert.IsNotNull(gateway); }
public void when_retrieving_with_try_get_instance_for_instance_that_does_not_exists() { var session = new BuildSession(new PluginGraph()); session.TryGetInstance<IService>().ShouldBeNull(); }
protected override object build(Type pluginType, BuildSession session) { return TheInstanceThatWasBuilt; }
public void when_retrieving_with_try_get_instance_with_nongeneric_type_that_does_not_exist() { var session = new BuildSession(new PluginGraph()); session.TryGetInstance(typeof(IService)).ShouldBeNull(); }
public void when_retrieving_with_try_get_instance_with_nongeneric_type_that_does_exist() { var theService = new ColorService("red"); var registry = new Registry(); registry.For<IService>().Use(theService); var session = new BuildSession(registry.Build()); session.TryGetInstance(typeof(IService)).ShouldBeTheSameAs(theService); }
public object ConstructNew(Type pluginType, Instance instance, BuildSession session) { object returnValue = instance.Build(pluginType, session); return ApplyInterception(pluginType, returnValue, session, instance); }
public void When_calling_GetInstance_if_no_default_can_be_found_throw_202() { var graph = new RootPipelineGraph(new PluginGraph()); assertActionThrowsErrorCode(202, delegate { var session = new BuildSession(graph); session.GetInstance(typeof (IGateway)); }); }
public void when_retrieving_by_try_get_named_instance_with_nongeneric_type_that_does_exist() { var red = new ColorService("red"); var green = new ColorService("green"); var registry = new Registry(); registry.For<IService>().Add(red).Named("red"); registry.For<IService>().Add(green).Named("green"); var graph = registry.Build(); var session = new BuildSession(graph); session.TryGetInstance(typeof(IService), "red").ShouldBeTheSameAs(red); }
public void SetUp() { var registry = new Registry(); registry.BuildInstancesOf<Rule>(); registry.Scan(x => { x.Assembly("StructureMap.Testing.Widget"); x.Assembly("StructureMap.Testing.Widget2"); }); PluginGraph graph = registry.Build(); var pipelineGraph = new PipelineGraph(graph); _session = new BuildSession(pipelineGraph, graph.InterceptorLibrary); }
public void Throw_exception_When_trying_to_build_an_instance_that_cannot_be_found() { var graph = PipelineGraph.BuildEmpty(); var ex = Exception<StructureMapConfigurationException>.ShouldBeThrownBy(() => { var session = new BuildSession(graph); session.CreateInstance(typeof (IGateway), "Gateway that is not configured"); }); ex.Title.ShouldBe( "Could not find an Instance named 'Gateway that is not configured' for PluginType StructureMap.Testing.Widget3.IGateway"); }
protected override object build(Type pluginType, BuildSession session) { return new ClassWithNoArgs { TheAddress = (Address) session.GetInstance(typeof (Address)) }; }
public IObjectCache FindCache(Type pluginType, Instance instance, BuildSession session) { return _pipeline.FindCache(pluginType); }
public void when_retrieving_an_object_by_nongeneric_type_and_name() { var red = new ColorService("red"); var green = new ColorService("green"); var registry = new Registry(); registry.For<IService>().Add(red).Named("red"); registry.For<IService>().Add(green).Named("green"); var graph = registry.Build(); var session = new BuildSession(graph); session.GetInstance(typeof(IService), "red").ShouldBeTheSameAs(red); }
public void when_building_an_instance_use_the_register_the_stack_frame() { var recordingInstance = new BuildSessionInstance1(); ConfiguredInstance instance = new ConfiguredInstance(typeof (ClassWithRule)).Child("rule").Is(recordingInstance); var session = new BuildSession(new PluginGraph()); session.CreateInstance(typeof (IClassWithRule), instance); recordingInstance.Root.ConcreteType.ShouldEqual(typeof (ClassWithRule)); recordingInstance.Root.RequestedType.ShouldEqual(typeof (IClassWithRule)); recordingInstance.Root.Name.ShouldEqual(instance.Name); recordingInstance.Current.ConcreteType.ShouldEqual(typeof (ColorRule)); recordingInstance.Current.RequestedType.ShouldEqual(typeof (Rule)); recordingInstance.Current.Name.ShouldEqual(recordingInstance.Name); }
public void When_calling_GetInstance_if_no_default_can_be_found_throw_202() { var graph = PipelineGraph.BuildEmpty(); var ex = Exception<StructureMapConfigurationException>.ShouldBeThrownBy(() => { var session = new BuildSession(graph); session.GetInstance(typeof (IGateway)); }); ex.Context.ShouldContain("There is no configuration specified for StructureMap.Testing.Widget3.IGateway"); ex.Title.ShouldBe( "No default Instance is registered and cannot be automatically determined for type 'StructureMap.Testing.Widget3.IGateway'"); }
public void Return_the_same_object_within_a_session_for_the_default_of_a_plugin_type() { int count = 0; var instance = new LambdaInstance<ColorRule>(() => { count++; return new ColorRule("Red"); }); var registry = new Registry(); registry.ForRequestedType<ColorRule>().TheDefault.IsThis(instance); PluginGraph graph = registry.Build(); var session = new BuildSession(graph); object result1 = session.CreateInstance(typeof (ColorRule)); object result2 = session.CreateInstance(typeof (ColorRule)); object result3 = session.CreateInstance(typeof (ColorRule)); object result4 = session.CreateInstance(typeof (ColorRule)); Assert.AreEqual(1, count); Assert.AreSame(result1, result2); Assert.AreSame(result1, result3); Assert.AreSame(result1, result4); }
public void Throw_200_When_trying_to_build_an_instance_that_cannot_be_found() { var graph = new RootPipelineGraph(new PluginGraph()); assertActionThrowsErrorCode(200, delegate { var session = new BuildSession(graph); session.CreateInstance(typeof (IGateway), "Gateway that is not configured"); }); }
public void when_retrieving_by_try_get_named_instance_that_does_not_exist() { var session = new BuildSession(); session.TryGetInstance<IService>("red").ShouldBeNull(); }
public void when_retrieving_by_try_get_instance_for_instance_that_does_exist() { var session = new BuildSession(); var theService = new ColorService("red"); session.RegisterDefault(typeof (IService), theService); session.TryGetInstance<IService>().ShouldBeTheSameAs(theService); }
public void when_retrieving_by_try_get_named_instance_that_does_exist() { var red = new ColorService("red"); var green = new ColorService("green"); var graph = new PluginGraph(); PluginFamily family = graph.FindFamily(typeof (IService)); family.AddInstance(new ObjectInstance(red).WithName("red")); family.AddInstance(new ObjectInstance(green).WithName("green")); var session = new BuildSession(graph); session.TryGetInstance<IService>("red").ShouldBeTheSameAs(red); session.TryGetInstance<IService>("green").ShouldBeTheSameAs(green); }