public void get_the_default_instance_when_it_exists() { var instance = new ConfiguredInstance(typeof (Service<>)); family.SetDefault(instance); configuration.Default.ConcreteType.ShouldEqual(typeof (Service<>)); }
/// <summary> /// Convenience method that sets the default concrete type of the PluginType. The "concreteType" /// can only accept types that do not have any primitive constructor arguments. /// StructureMap has to know how to construct all of the constructor argument types. /// </summary> /// <param name="concreteType"></param> /// <returns></returns> public ConfiguredInstance Use(Type concreteType) { var instance = new ConfiguredInstance(concreteType); Use(instance); return instance; }
public void can_build_a_simple_class_type() { var instance = new ConfiguredInstance(typeof (Rule1)); var rule = instance.Build<Rule>(_session); rule.ShouldNotBeNull(); rule.ShouldBeOfType<Rule1>(); }
public void can_set_scope_directly_on_the_instance() { var i1 = new ConfiguredInstance(GetType()).Named("foo"); i1.SetLifecycleTo(Lifecycles.ThreadLocal); i1.Lifecycle.ShouldBeOfType<ThreadLocalStorageLifecycle>(); }
public void can_use_a_configured_instance_with_generic_template_type_and_arguments() { var instance = new ConfiguredInstance(typeof (Service2<>), typeof (string)); var container = new Container(); container.GetInstance<IService<string>>(instance).ShouldBeOfType(typeof (Service2<string>)); }
public static void LoadAppropriateStoreRegistry(IContainer initContainer) { var store = initContainer.GetInstance<IRRConfiguration>().ContentStore; if (store == Configuration.Store.SqlServerStore) { var sqlAssembly = Assembly.Load("RequestReduce.SqlServer"); initContainer.Configure(x => { x.For(sqlAssembly.GetType("RequestReduce.SqlServer.IFileRepository")) .Use( sqlAssembly.GetType( "RequestReduce.SqlServer.FileRepository")); var diskStore = new ConfiguredInstance( sqlAssembly.GetType("RequestReduce.SqlServer.SqlServerStore")); var diskCache = new ConfiguredInstance( sqlAssembly.GetType("RequestReduce.SqlServer.DbDiskCache")); x.For<LocalDiskStore>().Singleton() .Use(diskCache); diskStore.CtorDependency<LocalDiskStore>("fileStore").Is( initContainer.GetInstance<LocalDiskStore>()); diskStore.CtorDependency<IUriBuilder>("uriBuilder").Is( initContainer.GetInstance<IUriBuilder>()); diskStore.CtorDependency<IReductionRepository>("reductionRepository").Is( initContainer.GetInstance<IReductionRepository>()); x.For<IStore>().LifecycleIs(new RRHybridLifecycle()) .Use(diskStore); }); } else initContainer.Configure(x => x.AddRegistry<RRLocalStoreRegistry>()); }
public void Can_NOT_be_plugged_in_if_plugged_type_cannot_be_cast_to_the_plugin_type() { var instance = new ConfiguredInstance(typeof (ColorRule)); var family = new PluginFamily(typeof (IWidget)); instance.As<IDiagnosticInstance>().CanBePartOfPluginFamily(family).ShouldBeFalse(); }
public void BuildRule1() { var instance = new ConfiguredInstance(typeof (Rule1)); var rule = (Rule) instance.Build(typeof (Rule), _session); Assert.IsNotNull(rule); Assert.IsTrue(rule is Rule1); }
public void Can_be_plugged_in_if_there_is_a_plugged_type_and_the_plugged_type_can_be_cast_to_the_plugintype() { var instance = new ConfiguredInstance(typeof (ColorWidget)); var family = new PluginFamily(typeof (IWidget)); IDiagnosticInstance diagnosticInstance = instance; Assert.IsTrue(diagnosticInstance.CanBePartOfPluginFamily(family)); }
/// <summary> /// Shortcut method to add an additional Instance to this Plugin Type /// as just a Concrete Type. This will only work if the Concrete Type /// has no primitive constructor or mandatory Setter arguments. /// </summary> /// <param name="concreteType"></param> /// <returns></returns> public ConfiguredInstance Add(Type concreteType) { var instance = new ConfiguredInstance(concreteType); alterAndContinue(family => family.AddInstance(instance)); return instance; }
public void get_settable_properties() { IConfiguredInstance instance = new ConfiguredInstance(typeof(GuyWithProperties)); instance.SettableProperties() .Single().Name.ShouldBe("Widget"); }
public InstanceMementoPropertyReader(ConfiguredInstance instance, InstanceMemento memento, PluginGraph pluginGraph, Type pluginType) { _instance = instance; _memento = memento; _pluginGraph = pluginGraph; _pluginType = pluginType; }
public void build_fails_with_StructureMapException_adds_context() { var instance = new ConfiguredInstance(typeof(ClassThatBlowsUp)); var actual = Exception<StructureMapBuildException>.ShouldBeThrownBy(() => { instance.Build<ClassThatBlowsUp>(); }); actual.Message.ShouldContain(instance.Description); actual.ShouldBeOfType<StructureMapBuildException>(); }
/// <summary> /// Start the definition of a child instance by defining the concrete type /// </summary> /// <param name="pluggedType"></param> /// <returns></returns> public ConfiguredInstance IsConcreteType(Type pluggedType) { ExpressionValidator.ValidatePluggabilityOf(pluggedType).IntoPluginType(_childType); var childInstance = new ConfiguredInstance(pluggedType); _instance.SetChild(_propertyName, childInstance); return(_instance); }
public void instance_key_is_predictable() { var i1 = new ConfiguredInstance(GetType()).Named("foo"); var i2 = new ConfiguredInstance(GetType()).Named("bar"); i1.InstanceKey(GetType()).ShouldEqual(i1.InstanceKey(GetType())); i2.InstanceKey(GetType()).ShouldEqual(i2.InstanceKey(GetType())); i1.InstanceKey(GetType()).ShouldNotEqual(i2.InstanceKey(GetType())); i1.InstanceKey(typeof(InstanceUnderTest)).ShouldNotEqual(i1.InstanceKey(GetType())); }
public void still_chooses_PerRequest_if_nothing_is_selected_on_either_family_or_instance() { var family = new PluginFamily(GetType()); var i1 = new ConfiguredInstance(GetType()).Named("foo"); family.AddInstance(i1); i1.Lifecycle.ShouldBeOfType<TransientLifecycle>(); }
public void set_to_singleton() { // SAMPLE: set-iconfigured-instance-to-SingletonThing IConfiguredInstance instance = new ConfiguredInstance(typeof (WidgetHolder)); instance.Singleton(); instance.Lifecycle.ShouldBeOfType<SingletonLifecycle>(); // ENDSAMPLE }
public void get_instance_if_the_object_does_not_already_exist() { var instance = new ConfiguredInstance(typeof (Foo)); var foo = new Foo(); theResolver.Stub(x => x.ResolveFromLifecycle(typeof (IFoo), instance)).Return(foo); theCache.GetObject(typeof (IFoo), instance, new TransientLifecycle()).ShouldBeTheSameAs(foo); }
public void Import_an_instance_from_the_default_profile() { var source = new ProfileManager(); var sourceInstance = new ConfiguredInstance(typeof (AWidget)); source.SetDefault(PROFILE, typeof (IWidget), sourceInstance); var destination = new ProfileManager(); destination.DefaultProfileName = PROFILE; destination.ImportFrom(source); Assert.AreSame(sourceInstance, destination.GetDefault(typeof (IWidget))); }
public void Import_the_default_does_not_impact_the_source() { var source = new ProfileManager(); var sourceInstance = new ConfiguredInstance(typeof (AWidget)); source.SetDefault(typeof (IWidget), sourceInstance); var destination = new ProfileManager(); destination.ImportFrom(source); destination.SetDefault(typeof (IWidget), new ObjectInstance(new AWidget())); Assert.AreSame(sourceInstance, source.GetDefault(typeof (IWidget))); }
public void get_default_if_it_does_not_already_exist() { var instance = new ConfiguredInstance(typeof (Foo)); thePipeline.Stub(x => x.GetDefault(typeof (IFoo))).Return(instance); var foo = new Foo(); theResolver.Stub(x => x.ResolveFromLifecycle(typeof (IFoo), instance)).Return(foo); theCache.GetDefault(typeof (IFoo), thePipeline) .ShouldBeTheSameAs(foo); }
public void does_override_the_scope_of_the_parent() { var family = new PluginFamily(GetType()); family.SetScopeTo(InstanceScope.Singleton); var i1 = new ConfiguredInstance(GetType()).Named("foo"); i1.SetScopeTo(InstanceScope.ThreadLocal); family.AddInstance(i1); i1.Lifecycle.ShouldBeOfType<ThreadLocalStorageLifecycle>(); }
public void eject_and_remove_the_default_value() { var instance = new ConfiguredInstance(typeof (Service<>)); family.SetDefault(instance); family.AddInstance(new ConfiguredInstance(typeof (Service2<>))); InstanceRef iRef = configuration.Instances.FirstOrDefault(x => x.Name == instance.Name); configuration.EjectAndRemove(iRef); family.GetDefaultInstance().ShouldBeNull(); }
public void Import_a_default_when_the_destination_already_has_an_active_profile() { var source = new ProfileManager(); var sourceInstance = new ConfiguredInstance(typeof (AWidget)); source.SetDefault(PROFILE, typeof (IWidget), sourceInstance); var destination = new ProfileManager(); destination.SetDefault(PROFILE, typeof (Rule), new ConfiguredInstance(typeof (ColorRule))); destination.CurrentProfile = PROFILE; destination.ImportFrom(source); Assert.AreSame(sourceInstance, destination.GetDefault(typeof (IWidget))); }
public void get_must_build_the_object_in_the_current_session() { var session = MockRepository.GenerateMock<IBuildSession>(); var instance = new ConfiguredInstance(typeof (Foo)); var foo = new Foo(Guid.Empty); session.Stub(x => x.BuildNewInSession(typeof (IFoo), instance)) .Return(foo); new NulloTransientCache().Get(typeof (IFoo), instance, session) .ShouldBeTheSameAs(foo); }
public void Seal() { // TODO -- this goes to the runtime interception stuff if (_pluginType.IsConcrete() && PluginCache.GetPlugin(_pluginType).CanBeAutoFilled) { MissingInstance = new ConfiguredInstance(_pluginType); } // TODO -- This just goes away when we make PluginGraph responsible if (_instances.Count == 1) { _defaultKey = _instances.First.Name; } }
public void Import_the_default_for_a_profile_make_sure_overriding_the_destination_does_not_impact_the_source() { var source = new ProfileManager(); var profileInstance = new ConfiguredInstance(typeof (AWidget)); source.SetDefault(PROFILE, typeof (IWidget), profileInstance); var destination = new ProfileManager(); destination.ImportFrom(source); // Source should be unchanged when destination IS changed destination.SetDefault(PROFILE, typeof (IWidget), new ObjectInstance(new AWidget())); Assert.AreSame(profileInstance, source.GetDefault(typeof (IWidget), PROFILE)); }
public void Modify() { IConfiguredInstance instance = new ConfiguredInstance(typeof(ImportantService)); instance.SetLifecycleTo(new SingletonLifecycle()); instance.SetLifecycleTo<SingletonLifecycle>(); instance.Singleton(); }
public void dependency_with_setter_with_instance() { var instance = new ConfiguredInstance(typeof(GuyWithProperties)); var prop = instance.PluggedType.GetProperty("Widget"); var dependency = new SmartInstance<AWidget>(); instance.Dependencies.AddForProperty(prop, dependency); var container = new Container(); container.GetInstance<GuyWithProperties>(instance) .Widget.ShouldBeOfType<AWidget>(); }
public void dependency_with_setter_with_value() { var instance = new ConfiguredInstance(typeof(GuyWithProperties)); var prop = instance.PluggedType.GetProperty("Widget"); var myWidget = new ColorWidget("red"); instance.Dependencies.AddForProperty(prop, myWidget); var container = new Container(); container.GetInstance<GuyWithProperties>(instance) .Widget.ShouldBeTheSameAs(myWidget); }
public Instance RepositoryFor(SagaTypes sagaTypes) { if (sagaTypes.StateType.GetProperty(SagaTypes.Id) == null) { return null; } var instance = new ConfiguredInstance(typeof (InMemorySagaRepository<,>), sagaTypes.StateType, sagaTypes.MessageType); instance.Dependencies.Add(typeof(Func<,>).MakeGenericType(sagaTypes.StateType, typeof(Guid)), sagaTypes.ToSagaIdFunc()); instance.Dependencies.Add(typeof(Func<,>).MakeGenericType(sagaTypes.MessageType, typeof(Guid)), sagaTypes.ToCorrelationIdFunc()); return instance; }
public ChildInstanceExpression(ConfiguredInstance instance, string propertyName) { _instance = instance; _propertyName = propertyName; }
public ChildInstanceExpression(ConfiguredInstance instance, string propertyName, Type childType) : this(instance, propertyName) { _childType = childType; }