public void Configure(Type component, DependencyLifecycle dependencyLifecycle) { this.ThrowIfDisposed(); lock (this.configuredInstances) { if (this.configuredInstances.ContainsKey(component)) { return; } } ILifecycle lifecycle = NServiceBusStructureMapObjectBuilder.GetLifecycleFrom(dependencyLifecycle); ConfiguredInstance configuredInstance = null; this.container.Configure(delegate(ConfigurationExpression x) { configuredInstance = x.For(component, null).LifecycleIs(lifecycle).Use(component); x.EnableSetterInjectionFor(component); List <Type> list = NServiceBusStructureMapObjectBuilder.GetAllInterfacesImplementedBy(component).ToList <Type>(); foreach (Type current in list) { x.For(current, null).LifecycleIs(lifecycle).Use((IContext c) => c.GetInstance(component)); x.EnableSetterInjectionFor(current); } }); lock (this.configuredInstances) { this.configuredInstances.Add(component, configuredInstance); } }
/// <summary> /// Sets the instanceault implementation of a service if there is no /// previous registration /// </summary> /// <param name="interfaceType"></param> /// <param name="concreteType"></param> /// <returns></returns> public Instance SetServiceIfNone(Type interfaceType, Type concreteType) { var instance = new ConfiguredInstance(concreteType); For(interfaceType).Configure(x => x.Fallback = instance); return(instance); }
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>)); }
private object buildInstanceWithArgs(Type pluginType, Instance defaultInstance, ExplicitArguments args, string requestedName) { if (defaultInstance == null && pluginType.IsConcrete()) { defaultInstance = new ConfiguredInstance(pluginType); } var basicInstance = defaultInstance as IConfiguredInstance; var instance = basicInstance == null ? defaultInstance : basicInstance.Override(args); if (instance == null) { throw new StructureMapConfigurationException("No default instance or named instance '{0}' for requested plugin type {1}", requestedName, pluginType.GetFullName()); } var session = new BuildSession(_pipelineGraph, requestedName, args) { RootType = instance.ReturnedType }; return(session.FindObject(pluginType, instance)); }
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")); x.For(sqlAssembly.GetType("RequestReduce.SqlServer.DbDiskCache")) .Singleton(); var diskStore = new ConfiguredInstance( sqlAssembly.GetType("RequestReduce.SqlServer.SqlServerStore")); diskStore.CtorDependency <IStore>("fileStore").Is( new ConfiguredInstance( sqlAssembly.GetType("RequestReduce.SqlServer.DbDiskCache"))); 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(); }
void Common.IContainer.Configure(Type component, DependencyLifecycle dependencyLifecycle) { lock (configuredInstances) { if (configuredInstances.ContainsKey(component)) { return; } } var lifecycle = GetLifecycleFrom(dependencyLifecycle); ConfiguredInstance configuredInstance = null; container.Configure(x => { configuredInstance = x.For(component) .LifecycleIs(lifecycle) .Use(component); x.EnableSetterInjectionFor(component); foreach (var implementedInterface in GetAllInterfacesImplementedBy(component)) { x.RegisterAdditionalInterfaceForPluginType(implementedInterface, component, lifecycle); x.EnableSetterInjectionFor(implementedInterface); } }); lock (configuredInstances) configuredInstances.Add(component, configuredInstance); }
public void adding_an_instance_sets_itself_as_the_parent() { var family = new PluginFamily(typeof(IGateway)); var instance = new ConfiguredInstance(typeof(TheGateway)); family.AddInstance(instance); instance.Parent.ShouldBeTheSameAs(family); }
protected override IConfiguredInstance buildInstance() { var instance = new ConfiguredInstance(typeof(InputBehavior <>), _inputType); instance.Dependencies.Add(typeof(IInputNode), this); return(instance); }
public Instance ToInstance() { var instance = new ConfiguredInstance(typeof(PollingJob <,>), JobType, SettingType); instance.Dependencies.Add(typeof(PollingJobChain), this); return(instance); }
/// <summary> /// Registers an *additional* implementation of a service. Actual behavior varies by actual /// IoC container /// </summary> /// <typeparam name="TService"></typeparam> /// <param name="implementation"></param> /// <returns></returns> public Instance AddService <TService>(Type implementationType) { var instance = new ConfiguredInstance(implementationType); For <TService>().AddInstance(instance); return(instance); }
public void can_set_scope_directly_on_the_instance() { var i1 = new ConfiguredInstance(GetType()).Named("foo"); i1.SetLifecycleTo(Lifecycles.ThreadLocal); i1.Lifecycle.ShouldBeOfType <ThreadLocalStorageLifecycle>(); }
Instance IContainerModel.ToInstance() { var instance = new ConfiguredInstance(_authType); configure(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 ConfiguredInstance AddType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); alterAndContinue(family => { family.AddInstance(instance); }); return(instance); }
public ConfiguredInstance TheDefaultIsConcreteType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); Use(instance); return(instance); }
public void has_implementations_is_true_if_there_are_instances_for_the_underlying_family() { var instance = new ConfiguredInstance(typeof(Service <>)); family.SetDefault(instance); configuration.HasImplementations().ShouldBeTrue(); }
public void get_the_default_instance_when_it_exists() { var instance = new ConfiguredInstance(typeof(Service <>)); family.SetDefault(instance); configuration.Default.ReturnedType.ShouldBe(typeof(Service <>)); }
public void do_not_blow_up_if_the_profile_does_not_have_the_plugin_type_being_removed() { var profile = new Profile("something"); ConfiguredInstance instance = new ConfiguredInstance(typeof(SomethingOne)).WithName("Red"); profile.Remove(typeof(ISomething), instance); }
public void get_settable_properties() { IConfiguredInstance instance = new ConfiguredInstance(typeof(GuyWithProperties)); instance.SettableProperties() .Single().Name.ShouldBe("Widget"); }
/// <summary> /// Shortcut to register a Concrete Type as an instance. This method supports /// method chaining to allow you to add constructor and setter arguments for /// the concrete type /// </summary> /// <param name="concreteType"></param> /// <returns></returns> public ConfiguredInstance Is(Type concreteType) { var instance = new ConfiguredInstance(concreteType); _action(instance); return(instance); }
public ConfiguredInstance UseConcreteType(Type concreteType) { var instance = new ConfiguredInstance(concreteType); Use(instance); return(instance); }
/// <summary> /// Applies a decorator type to all Instances that return a type that can be cast to this PluginType /// </summary> /// <param name="decoratorType"></param> /// <param name="filter"></param> /// <returns></returns> public ConfiguredInstance DecorateAllWith(Type decoratorType, Func <Instance, bool> filter = null) { var instance = new ConfiguredInstance(decoratorType); var policy = new DecoratorPolicy(_pluginType, instance, filter); _registry.alter = graph => graph.Policies.Interceptors.Add(policy); return(instance); }
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)); }
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); }
private void setDefault <T, U>(string key) where U : T, new() { PluginFamily family = _pluginGraph.FindFamily(typeof(T)); ConfiguredInstance instance = new ConfiguredInstance(typeof(U)).WithName(key); family.AddInstance(instance); _profile.SetDefault(typeof(T), instance); }
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>(); }
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 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 If_PluginFamily_only_has_one_instance_make_that_the_default() { var family = new PluginFamily(typeof(IGateway)); var theInstanceKey = "the default"; var instance = new ConfiguredInstance(typeof(TheGateway)).Named(theInstanceKey); family.AddInstance(instance); family.GetDefaultInstance().ShouldBeTheSameAs(instance); }