public void select_interceptors() { var activator1 = new ActivatorInterceptor<ITarget>(x => x.Activate()); var activator2 = new ActivatorInterceptor<Target>(x => x.UseSession(null)); var activator3 = new ActivatorInterceptor<Target>(x => x.ThrowUp()); var activator4 = new ActivatorInterceptor<ITarget>(x => x.Debug()); var activator5 = new ActivatorInterceptor<IGateway>(x => x.DoSomething()); var policies = new Policies(); policies.Add(activator1.ToPolicy()); policies.Add(activator2.ToPolicy()); policies.Add(activator3.ToPolicy()); policies.Add(activator4.ToPolicy()); policies.Add(activator5.ToPolicy()); var instance1 = new SmartInstance<Target>(); policies.Apply(typeof(ITarget), instance1); instance1.Interceptors .ShouldHaveTheSameElementsAs(activator1, activator2, activator3, activator4); var instance2 = new SmartInstance<ATarget>(); policies.Apply(typeof(ITarget), instance2); instance2.Interceptors .ShouldHaveTheSameElementsAs(activator1, activator4); var instance3 = new SmartInstance<StubbedGateway>(); policies.Apply(typeof(ITarget), instance3); instance3.Interceptors .ShouldHaveTheSameElementsAs(activator5); }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance<ExceptionHandlerBehavior>(); instance.Ctor<HandlerChain>().Is(_chain); return instance; }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance<BehaviorTracer>(); instance.Ctor<BehaviorNode>().Is(Next); return instance; }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance<AntiForgeryBehavior>(); instance.Ctor<string>().Is(_salt); return instance; }
public void release_from_the_cache() { var cache = new TrackingTransientCache(); var instance = new SmartInstance<Target>(); var session = new FakeBuildSession(); var t1 = cache.Get(typeof(Target), instance, session).As<Target>(); var t2 = cache.Get(typeof(Target), instance, session).As<Target>(); var t3 = cache.Get(typeof(Target), instance, session).As<Target>(); // pre-conditions t2.WasDisposed.ShouldBeFalse(); cache.Tracked.ShouldHaveTheSameElementsAs(t1, t2, t3); cache.Release(t2); // only t2 should be disposed t1.WasDisposed.ShouldBeFalse(); t2.WasDisposed.ShouldBeTrue(); t3.WasDisposed.ShouldBeFalse(); // t2 should be removed from tracked cache.Tracked.ShouldHaveTheSameElementsAs(t1, t3); }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance <BehaviorTracer>(); instance.Ctor <BehaviorNode>().Is(Next); return(instance); }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance <AntiForgeryBehavior>(); instance.Ctor <string>().Is(_salt); return(instance); }
public CacheServiceRegistry(SmartInstance <TConcreteType, TPluginType> smartInstance) { smartInstance.AddInterceptor(new DynamicProxyInterceptor <TPluginType>(new IInterceptionBehavior[] { new CachingInterceptor(), new CacheInvalidateInterceptor(), })); }
public SmartInstance <TPluggedType> Add <TPluggedType>() { var instance = new SmartInstance <TPluggedType>(); Add(instance); return(instance); }
/// <summary> /// For this profile, use this concrete type /// </summary> /// <typeparam name="CONCRETETYPE"></typeparam> /// <returns></returns> public SmartInstance <CONCRETETYPE> Use <CONCRETETYPE>() { var instance = new SmartInstance <CONCRETETYPE>(); Use(instance); return(instance); }
/// <summary> /// Specify the default Instance of this PluginType by a concrete type /// </summary> public SmartInstance <TConcreteType, TPluginType> Use <TConcreteType>() where TConcreteType : TPluginType { var instance = new SmartInstance <TConcreteType, TPluginType>(); registerDefault(instance); return(instance); }
/// <summary> /// Add a new Instance to this PluginType by concrete type /// </summary> /// <typeparam name="TPluggedType"></typeparam> /// <returns></returns> public SmartInstance <TPluggedType, TPluginType> Add <TPluggedType>() where TPluggedType : TPluginType { var instance = new SmartInstance <TPluggedType, TPluginType>(); AddInstance(instance); return(instance); }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance <ExceptionHandlerBehavior>(); instance.Ctor <HandlerChain>().Is(_chain); return(instance); }
public FubuTransportServiceRegistry(string mode) { var eventAggregatorDef = mode.InTesting() ? new SmartInstance <SynchronousEventAggregator>() : (Instance) new SmartInstance <EventAggregator>(); eventAggregatorDef.SetLifecycleTo <SingletonLifecycle>(); SetServiceIfNone(typeof(IEventAggregator), eventAggregatorDef); var stateCacheDef = new SmartInstance <SagaStateCacheFactory>(); stateCacheDef.Singleton(); SetServiceIfNone(typeof(ISagaStateCacheFactory), stateCacheDef); SetServiceIfNone <IChainInvoker, ChainInvoker>(); SetServiceIfNone <IEnvelopeSender, EnvelopeSender>(); AddService <IMessageSerializer, XmlMessageSerializer>(); AddService <IMessageSerializer, JsonMessageSerializer>(); AddService <IActivator, ServiceBusActivator>(); SetServiceIfNone <IServiceBus, ServiceBus>(); SetServiceIfNone <IEnvelopeSerializer, EnvelopeSerializer>(); SetServiceIfNone <IHandlerPipeline, HandlerPipeline>(); AddService <ILogListener, EventAggregationListener>(); if (mode.InTesting()) { var def = new SmartInstance <MessagingSession>(); def.Singleton(); SetServiceIfNone(typeof(IMessagingSession), def); AddService <ILogListener, MessageRecordListener>(); } if (mode.InTesting()) { AddService <IActivator, TransportCleanupActivator>(); } AddService <IEnvelopeHandler, DelayedEnvelopeHandler>(); AddService <IEnvelopeHandler, ResponseEnvelopeHandler>(); AddService <IEnvelopeHandler, ChainExecutionEnvelopeHandler>(); AddService <IEnvelopeHandler, NoSubscriberHandler>(); SetServiceIfNone <IMessageExecutor, MessageExecutor>(); SetServiceIfNone <IOutgoingSender, OutgoingSender>(); subscriptions(); }
public FubuTransportServiceRegistry(string mode) { var eventAggregatorDef =mode.InTesting() ? new SmartInstance<SynchronousEventAggregator>() : (Instance)new SmartInstance<EventAggregator>(); eventAggregatorDef.SetLifecycleTo<SingletonLifecycle>(); SetServiceIfNone(typeof(IEventAggregator), eventAggregatorDef); var stateCacheDef = new SmartInstance<SagaStateCacheFactory>(); stateCacheDef.Singleton(); SetServiceIfNone(typeof(ISagaStateCacheFactory), stateCacheDef); SetServiceIfNone<IChainInvoker, ChainInvoker>(); SetServiceIfNone<IEnvelopeSender, EnvelopeSender>(); ForSingletonOf<IMessageSerializer>().Use<XmlMessageSerializer>(); AddService<IMessageSerializer, JsonMessageSerializer>(); AddService<IActivator, ServiceBusActivator>(); SetServiceIfNone<IServiceBus, ServiceBus>(); SetServiceIfNone<IEnvelopeSerializer, EnvelopeSerializer>(); SetServiceIfNone<IHandlerPipeline, HandlerPipeline>(); AddService<ILogListener, EventAggregationListener>(); if (mode.InTesting()) { var def = new SmartInstance<MessagingSession>(); def.Singleton(); SetServiceIfNone(typeof(IMessagingSession), def); AddService<ILogListener, MessageRecordListener>(); } if (mode.InTesting()) { AddService<IActivator, TransportCleanupActivator>(); } AddService<IEnvelopeHandler, DelayedEnvelopeHandler>(); AddService<IEnvelopeHandler, ResponseEnvelopeHandler>(); AddService<IEnvelopeHandler, ChainExecutionEnvelopeHandler>(); AddService<IEnvelopeHandler, NoSubscriberHandler>(); SetServiceIfNone<IMessageExecutor, MessageExecutor>(); SetServiceIfNone<IOutgoingSender, OutgoingSender>(); subscriptions(); }
public void set_default() { var family = new PluginFamily(typeof(IServiceProvider)); var instance = new SmartInstance <DataSet>(); family.SetDefault(instance); family.GetDefaultInstance().ShouldBeTheSameAs(instance); }
public void find_instance_positive() { var graph = PluginGraph.CreateRoot(); var instance = new SmartInstance <BigThingy>().Named("red"); graph.Families[typeof(BigThingy)].AddInstance(instance); graph.FindInstance(typeof(BigThingy), "red").ShouldBeTheSameAs(instance); }
public void reflecting_over_constructor_args() { IConfiguredInstance instance = new SmartInstance <GuyWithArguments>() // I'm just forcing it to assign the constructor function .SelectConstructor(() => new GuyWithArguments(null, null)); instance.Constructor.GetParameters().Select(x => x.Name) .ShouldHaveTheSameElementsAs("widget", "rule"); }
public void reflecting_over_constructor_args() { IConfiguredInstance instance = new SmartInstance<GuyWithArguments>() // I'm just forcing it to assign the constructor function .SelectConstructor(() => new GuyWithArguments(null, null)); instance.Constructor.GetParameters().Select(x => x.Name) .ShouldHaveTheSameElementsAs("widget", "rule"); }
private SmartInstance <T> instanceOf <T>() { var instance = new SmartInstance <T>(); structuredInstance = instance; configuredInstance = instance; return(instance); }
public void find_instance_can_use_missing_instance() { var graph = new PluginGraph(); var instance = new SmartInstance <BigThingy>().Named("red"); graph.Families[typeof(BigThingy)].MissingInstance = instance; graph.FindInstance(typeof(BigThingy), "green") .ShouldBeTheSameAs(instance); }
/// <summary> /// Shorthand way of saying Use<> /// </summary> /// <typeparam name="CONCRETETYPE"></typeparam> /// <returns></returns> public SmartInstance <CONCRETETYPE> Use <CONCRETETYPE>() where CONCRETETYPE : PLUGINTYPE { // This is *my* team's naming convention for generic parameters // I know you may not like it, but it's my article so there var instance = new SmartInstance <CONCRETETYPE>(); registerDefault(instance); return(instance); }
/// <summary> /// Shorthand way of saying Use<> /// </summary> /// <typeparam name="TConcreteType"></typeparam> /// <returns></returns> public SmartInstance <TConcreteType> Use <TConcreteType>() where TConcreteType : TPluginType { // This is *my* team's naming convention for generic parameters // I know you may not like it, but it's my article so there var instance = new SmartInstance <TConcreteType>(); registerDefault(instance); return(instance); }
private void subscriptions() { var subscriberDef = new SmartInstance<SubscriptionCache>(); subscriberDef.Singleton(); SetServiceIfNone(typeof (ISubscriptionCache), subscriberDef); SetServiceIfNone<ISubscriptionRepository, SubscriptionRepository>(); SetServiceIfNone<ISubscriptionPersistence>(new InMemorySubscriptionPersistence()); }
public T build <T>(Action <SmartInstance <T> > action) { SmartInstance <T> instance = For <T>(); action(instance); var container = new Container(r => r.For <T>().Use(instance)); return(container.GetInstance <T>()); }
public T build <T>(Action <SmartInstance <T> > action) { SmartInstance <T> instance = instanceOf <T>(); action(instance); var container = new Container(r => r.ForRequestedType <T>().TheDefault.IsThis(instance)); return(container.GetInstance <T>()); }
public void add_interceptor() { var interceptor = new ActivatorInterceptor <SimpleWidget>(w => w.Intercept()); var instance = new SmartInstance <SimpleWidget>(); instance.AddInterceptor(interceptor); new Container().GetInstance <SimpleWidget>(instance) .WasIntercepted.ShouldBeTrue(); }
private void subscriptions() { var subscriberDef = new SmartInstance <SubscriptionCache>(); subscriberDef.Singleton(); SetServiceIfNone(typeof(ISubscriptionCache), subscriberDef); SetServiceIfNone <ISubscriptionRepository, SubscriptionRepository>(); SetServiceIfNone <ISubscriptionPersistence>(new InMemorySubscriptionPersistence()); }
public void add_interceptor() { var interceptor = new ActivatorInterceptor<SimpleWidget>(w => w.Intercept()); var instance = new SmartInstance<SimpleWidget>(); instance.AddInterceptor(interceptor); new Container().GetInstance<SimpleWidget>(instance) .WasIntercepted.ShouldBeTrue(); }
/// <summary> /// Decorates all instances of TPluginType with the concrete type TDecoratorType /// </summary> public SmartInstance <TDecoratorType, TPluginType> DecorateAllWith <TDecoratorType>(Func <Instance, bool> filter = null) where TDecoratorType : TPluginType { var instance = new SmartInstance <TDecoratorType, TPluginType>(); var interceptor = new DecoratorInterceptor(typeof(TPluginType), instance); var policy = new InterceptorPolicy <TPluginType>(interceptor, filter); alter = graph => graph.Policies.Add(policy); return(instance); }
public void find_instance_can_use_missing_instance() { var graph = PluginGraph.CreateRoot(); var instance = new SmartInstance <BigThingy>().Named("red"); graph.Families[typeof(BigThingy)].MissingInstance = instance; var cloned_and_renamed = graph.FindInstance(typeof(BigThingy), "green").ShouldBeAssignableTo <ConfiguredInstance>(); cloned_and_renamed.Name.ShouldBe("green"); cloned_and_renamed.PluggedType.ShouldBe(typeof(BigThingy)); }
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 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; }
protected override IConfiguredInstance buildInstance() { var instance = new SmartInstance <AuthorizationBehavior>(); instance.Ctor <IAuthorizationNode>().Is(this); if (_failure != null) { instance.Ctor <IAuthorizationFailureHandler>().Is(_failure); } return(instance); }
internal void Register( CreatePluginFamilyExpression <TInter> @for, SmartInstance <TImpl, TInter> use, string instanceName, DILifeTime lifetime, Action <DILifeTime, LambdaInstance <TImpl, TInter> > setLifeTime) { this.@for = @for; this.use = use; this.instanceName = instanceName; this.lifetime = lifetime; this.setLifeTime = setLifeTime; }
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 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 get_builds_a_new_object_everytime_it_is_called() { var cache = new TrackingTransientCache(); var instance = new SmartInstance<Target>(); var session = new FakeBuildSession(); var t1 = cache.Get(typeof (Target), instance, session); var t2 = cache.Get(typeof (Target), instance, session); var t3 = cache.Get(typeof (Target), instance, session); t1.ShouldNotBeTheSameAs(t2); t1.ShouldNotBeTheSameAs(t3); t2.ShouldNotBeTheSameAs(t3); }
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 get_builds_a_new_object_everytime_it_is_called() { var cache = new TrackingTransientCache(); var instance = new SmartInstance <Target>(); var session = new FakeBuildSession(); var t1 = cache.Get(typeof(Target), instance, session); var t2 = cache.Get(typeof(Target), instance, session); var t3 = cache.Get(typeof(Target), instance, session); t1.ShouldNotBeTheSameAs(t2); t1.ShouldNotBeTheSameAs(t3); t2.ShouldNotBeTheSameAs(t3); }
public void remove_all_clears_the_defaul_and_removes_all_plugins_instances() { var family = new PluginFamily(typeof(IServiceProvider)); var instance = new SmartInstance <DataSet>(); family.SetDefault(instance); family.AddInstance(new NullInstance()); family.AddType(typeof(DataSet)); family.RemoveAll(); family.DefaultInstanceKey.ShouldBeNull(); family.InstanceCount.ShouldEqual(0); }
public CreatePluginFamilyExpression <PLUGINTYPE> AddConcreteType <PLUGGEDTYPE>() { if (!PluginCache.GetPlugin(typeof(PLUGGEDTYPE)).CanBeAutoFilled) { throw new StructureMapException(231); } _alterations.Add(family => { string name = PluginCache.GetPlugin(typeof(PLUGGEDTYPE)).ConcreteKey; SmartInstance <PLUGGEDTYPE> instance = new SmartInstance <PLUGGEDTYPE>().WithName(name); family.AddInstance(instance); }); return(this); }
public void dispose_and_clear_all() { var cache = new TrackingTransientCache(); var instance = new SmartInstance<Target>(); var session = new FakeBuildSession(); var t1 = cache.Get(typeof(Target), instance, session).As<Target>(); var t2 = cache.Get(typeof(Target), instance, session).As<Target>(); var t3 = cache.Get(typeof(Target), instance, session).As<Target>(); cache.DisposeAndClear(); cache.Tracked.Any().ShouldBeFalse(); t1.WasDisposed.ShouldBeTrue(); t2.WasDisposed.ShouldBeTrue(); t3.WasDisposed.ShouldBeTrue(); }
public void pushing_the_same_instance_will_throw_a_bidirectional_dependency_exception() { var session = BuildSession.ForPluginGraph(new PluginGraph()); var instance1 = new SmartInstance<StubbedGateway>(); var instance2 = new SmartInstance<ARule>(); var instance3 = new SmartInstance<AWidget>(); session.Push(instance1); session.Push(instance2); session.Push(instance3); var ex = Exception<StructureMapBuildException>.ShouldBeThrownBy(() => { session.Push(instance1); }); ex.Message.ShouldContain("Bi-directional dependency relationship detected!"); }