Exemplo n.º 1
0
        public void uses_explicit_output_cache_if_one_is_specified()
        {
            theNode.OutputCache = ObjectDef.ForType <FakeOutputCache>();

            toObjectDef().FindDependencyDefinitionFor <IOutputCache>()
            .ShouldEqual(theNode.OutputCache);
        }
Exemplo n.º 2
0
 protected override ObjectDef buildObjectDef()
 {
     return(ObjectDef.ForType <AuthorizationBehavior>(x =>
     {
         x.EnumerableDependenciesOf <IAuthorizationPolicy>().AddRange(_policies);
     }));
 }
Exemplo n.º 3
0
        public ObjectDef ToViewFactoryObjectDef()
        {
            var def = ObjectDef.ForType <ViewFactory>();

            def.DependencyByValue(_descriptor);

            return(def);
        }
Exemplo n.º 4
0
        public void as_singleton()
        {
            var def = ObjectDef.ForType <SettingsProvider>();

            def.AsSingleton().ShouldBeTheSameAs(def);

            def.IsSingleton.ShouldBeTrue();
        }
Exemplo n.º 5
0
        public void simple_ObjectDef_by_type()
        {
            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
            });

            container.Get <IService>().ShouldBeOfType <SimpleService>();
        }
Exemplo n.º 6
0
        public void has_any()
        {
            theGraph.HasAny(typeof(IFoo)).ShouldBeFalse();

            theGraph.AddService(typeof(IFoo), ObjectDef.ForType <Foo>());

            theGraph.HasAny(typeof(IFoo)).ShouldBeTrue();
        }
        protected override ObjectDef buildObjectDef()
        {
            var def = ObjectDef.ForType <ExceptionHandlerBehavior>();

            def.DependencyByValue(_chain);

            return(def);
        }
Exemplo n.º 8
0
        public FubuTransportServiceRegistry()
        {
            var eventAggregatorDef = FubuTransport.UseSynchronousLogging
                ? ObjectDef.ForType <SynchronousEventAggregator>()
                : ObjectDef.ForType <EventAggregator>();

            eventAggregatorDef.IsSingleton = true;
            SetServiceIfNone(typeof(IEventAggregator), eventAggregatorDef);



            var stateCacheDef = new ObjectDef(typeof(SagaStateCacheFactory));

            stateCacheDef.IsSingleton = true;
            SetServiceIfNone(typeof(ISagaStateCacheFactory), stateCacheDef);

            SetServiceIfNone <IChainInvoker, ChainInvoker>();
            SetServiceIfNone <IEnvelopeSender, EnvelopeSender>();
            AddService <IMessageSerializer, XmlMessageSerializer>();

            AddService <IActivator, FubuTransportationActivator>();

            SetServiceIfNone <IServiceBus, ServiceBus>();

            SetServiceIfNone <IEnvelopeSerializer, EnvelopeSerializer>();
            SetServiceIfNone <IHandlerPipeline, HandlerPipeline>();


            AddService <ILogListener, EventAggregationListener>();

            if (FubuTransport.ApplyMessageHistoryWatching || FubuMode.InTestingMode())
            {
                AddService <IListener, MessageWatcher>();

                var def = ObjectDef.ForType <MessagingSession>();
                def.IsSingleton = true;
                SetServiceIfNone(typeof(IMessagingSession), def);
                AddService <ILogListener, MessageRecordListener>();
            }


            if (FubuMode.InTestingMode())
            {
                AddService <IActivator, TransportCleanupActivator>();
            }

            AddService <IEnvelopeHandler, DelayedEnvelopeHandler>();
            AddService <IEnvelopeHandler, ResponseEnvelopeHandler>();
            AddService <IEnvelopeHandler, ChainExecutionEnvelopeHandler>();
            AddService <IEnvelopeHandler, NoSubscriberHandler>();

            SetServiceIfNone <IMessageExecutor, MessageExecutor>();
            SetServiceIfNone <IOutgoingSender, OutgoingSender>();

            SetServiceIfNone <IAsyncHandling, AsyncHandling>();

            subscriptions();
        }
Exemplo n.º 9
0
        protected override ObjectDef buildObjectDef()
        {
            var objectDef = ObjectDef.ForType <ConnegBehavior>();

            objectDef.Dependency(typeof(IConnegInputHandler), buildInputHandlerDef());
            objectDef.Dependency(typeof(IConnegOutputHandler), buildOutputHandlerDef());

            return(objectDef);
        }
Exemplo n.º 10
0
        public void auto_wiring_applies_when_the_dependency_is_not_set_explicitly()
        {
            var container = ContainerFacilitySource.New(x =>
            {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
            });

            container.Get <GuyWithService>().Service.ShouldBeOfType <SimpleService>();
        }
Exemplo n.º 11
0
        public void use_the_closed_type_if_it_exists()
        {
            var facility = ContainerFacilitySource.New(x =>
            {
                x.Register(typeof(IService <>), new ObjectDef(typeof(SimpleService <>)));
                x.Register(typeof(IService <IThing>), ObjectDef.ForType <ThingService>());
            });

            facility.Get <IService <IThing> >().ShouldBeOfType <ThingService>();
        }
Exemplo n.º 12
0
        private ObjectDef buildConditionalInvokerDef(ObjectDef objectDef)
        {
            var invokerDef             = ObjectDef.ForType <ConditionalBehaviorInvoker>();
            var conditionalBehaviorDef = invokerDef
                                         .DependencyByType <IConditionalBehavior, ConditionalBehavior>();

            conditionalBehaviorDef.DependencyByType <IActionBehavior>(objectDef);
            conditionalBehaviorDef.DependencyByType <IConditional>(_conditionalDef);
            return(invokerDef);
        }
Exemplo n.º 13
0
        public ObjectDef ToViewFactoryObjectDef()
        {
            var def = ObjectDef.ForType <ViewFactory>();

            def
            .DependencyByType(typeof(IViewEntrySource), typeof(ViewEntrySource))
            .DependencyByValue(_descriptor);

            return(def);
        }
Exemplo n.º 14
0
        private void subscriptions()
        {
            var subscriberDef = ObjectDef.ForType <SubscriptionCache>();

            subscriberDef.IsSingleton = true;
            SetServiceIfNone(typeof(ISubscriptionCache), subscriberDef);

            SetServiceIfNone <ISubscriptionRepository, SubscriptionRepository>();

            SetServiceIfNone <ISubscriptionPersistence>(new InMemorySubscriptionPersistence());
        }
Exemplo n.º 15
0
        protected override ObjectDef buildObjectDef()
        {
            ObjectDef def = ObjectDef.ForType <TopicBehavior>();

            def.DependencyByValue(typeof(Topic), Topic);
            ObjectDef viewDef = Topic.File.ToViewToken().ToViewFactoryObjectDef();

            def.Dependency(typeof(IViewFactory), viewDef);

            return(def);
        }
        public void get_instance_by_name()
        {
            var services = ContainerFacilitySource.Services(x =>
            {
                x.Register(typeof(IThing), ObjectDef.ForType <ThingOne>().Named("One"));
                x.Register(typeof(IThing), ObjectDef.ForType <ThingTwo>().Named("Two"));
            });

            services.GetInstance <IThing>("One").ShouldBeOfType <ThingOne>();
            services.GetInstance <IThing>("Two").ShouldBeOfType <ThingTwo>();
        }
Exemplo n.º 17
0
        public void explicit_registration_of_a_primitive_argument()
        {
            var container = ContainerFacilitySource.New(x => {
                var def = ObjectDef.ForType <GuyWithPrimitive>();
                def.DependencyByValue("Jeremy");

                x.Register(typeof(GuyWithPrimitive), def);
            });

            container.Get <GuyWithPrimitive>()
            .Name.ShouldEqual("Jeremy");
        }
Exemplo n.º 18
0
        public void implicitly_auto_wires_all_implementations_of_a_service_if_not_explicitly_overridden()
        {
            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IService), ObjectDef.ForType <DifferentService>());
                x.Register(typeof(IService), ObjectDef.ForType <ExceptionCaseService>());
            });

            container.Get <ThingThatUsesLotsOfServices>()
            .Services.Select(x => x.GetType())
            .ShouldHaveTheSameElementsAs(typeof(SimpleService), typeof(DifferentService), typeof(ExceptionCaseService));
        }
Exemplo n.º 19
0
        /// <summary>
        /// Adds an authorization rule of type IAuthorizationRule<TModel>
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TRule"></typeparam>
        /// <returns></returns>
        public ObjectDef AddPolicy <TModel, TRule>() where TRule : IAuthorizationRule <TModel> where TModel : class
        {
            var topDef = ObjectDef.ForType <AuthorizationPolicy <TModel> >();

            _policies.Add(topDef);

            var ruleObjectDef = ObjectDef.ForType <TRule>();

            topDef.DependencyByType <IAuthorizationRule <TModel> >(ruleObjectDef);

            return(ruleObjectDef);
        }
Exemplo n.º 20
0
        private static void registerCompiler(IServiceRegistry registry)
        {
            var compilerDef      = registry.SetServiceIfNone(typeof(ICoffeeCompiler), typeof(SassCoffeeCompiler));
            var innerCompilerDef = ObjectDef.ForType <CoffeeScriptCompiler>();

            var instanceProvider = ObjectDef.ForType <InstanceProvider <IJavaScriptRuntime> >();
            Func <IJavaScriptRuntime> runtimeFunc = () => new IEJavaScriptRuntime();

            instanceProvider.DependencyByValue(typeof(Func <IJavaScriptRuntime>), runtimeFunc);
            innerCompilerDef.Dependency(typeof(IInstanceProvider <IJavaScriptRuntime>), instanceProvider);
            compilerDef.Dependency(typeof(CoffeeScriptCompiler), innerCompilerDef);
        }
Exemplo n.º 21
0
        public void can_get_all_registered_implementations_of_a_service()
        {
            var container = ContainerFacilitySource.New(x =>
            {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IService), ObjectDef.ForType <DifferentService>());
                x.Register(typeof(IService), ObjectDef.ForType <ExceptionCaseService>());
            });

            container.GetAll <IService>()
            .Select(x => x.GetType())
            .ShouldHaveTheSameElementsAs(typeof(SimpleService), typeof(DifferentService), typeof(ExceptionCaseService));
        }
        public void get_instance_by_type()
        {
            var services = ContainerFacilitySource.Services(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IThing), ObjectDef.ForType <ThingOne>());
            });

            services.GetInstance <IService>().ShouldBeOfType <SimpleService>();
            services.GetInstance <IThing>().ShouldBeOfType <ThingOne>();

            services.GetInstance(typeof(IService)).ShouldBeOfType <SimpleService>();
            services.GetInstance(typeof(IThing)).ShouldBeOfType <ThingOne>();
        }
Exemplo n.º 23
0
        protected override ObjectDef buildObjectDef()
        {
            var def = ObjectDef.ForType <AuthorizationBehavior>();

            def.DependencyByValue <IAuthorizationNode>(this);

            if (_failure != null)
            {
                def.Dependency(typeof(IAuthorizationFailureHandler), _failure);
            }

            return(def);
        }
Exemplo n.º 24
0
        public void nested_container_scoping_within_a_request()
        {
            var instance1 = ContainerFacilitySource.BuildBehavior(new ServiceArguments(), ObjectDef.ForType <Behavior1>(),
                                                                  x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
            }).As <Behavior1>();

            // "IService" is not a singleton, therefore, there should *only* be one created
            // and shared throughout the entire request.
            // This is vital for services like IFubuRequest
            instance1.Services.GetInstance <IService>().ShouldBeTheSameAs(instance1.Service);
            instance1.Service.ShouldBeTheSameAs(instance1.Guy.Service);
        }
Exemplo n.º 25
0
        public void fill_type()
        {
            theGraph.AddService(typeof(IFoo), ObjectDef.ForType <Foo>());

            theGraph.FillType(typeof(IFoo), typeof(Foo));
            theGraph.FillType(typeof(IFoo), typeof(Foo));
            theGraph.FillType(typeof(IFoo), typeof(Foo));
            theGraph.FillType(typeof(IFoo), typeof(Foo));
            theGraph.ServicesFor(typeof(IFoo)).Count().ShouldEqual(1);

            theGraph.FillType(typeof(IFoo), typeof(Foo2));
            theGraph.ServicesFor(typeof(IFoo)).Select(x => x.Type)
            .ShouldHaveTheSameElementsAs(typeof(Foo), typeof(Foo2));
        }
Exemplo n.º 26
0
        public PollingServicesRegistry()
        {
            // NEED MORE.
            SetServiceIfNone <ITimer, DefaultTimer>();
            AddService <IDeactivator, PollingJobDeactivator>();
            SetServiceIfNone <IPollingJobLogger, PollingJobLogger>();

            var def = ObjectDef.ForType <PollingJobs>();

            def.IsSingleton = true;
            SetServiceIfNone(typeof(IPollingJobs), def);

            SetServiceIfNone(typeof(PollingJobLatch), ObjectDef.ForValue(new PollingJobLatch()).AsSingleton());
        }
Exemplo n.º 27
0
        public void should_be_a_singleton_because_of_Cache_suffix()
        {
            var container = ContainerFacilitySource.New(x => {
                // Any concrete class suffixed with "Cache" is supposed to be a
                // singleton
                x.Register(typeof(IService), ObjectDef.ForType <SingletonCache>());
            });

            // Use this static method to know whether or not a class
            // should be scoped as a singleton or by Http request
            ServiceRegistry.ShouldBeSingleton(typeof(SingletonCache))
            .ShouldBeTrue();

            container.Get <IService>().ShouldBeTheSameAs(container.Get <IService>());
        }
        public void can_inject_arguments_into_the_behavior_factory()
        {
            var standInCurrentHttpRequest = new StandInCurrentHttpRequest();
            var inMemoryFubuRequest       = new InMemoryFubuRequest();

            var arguments = new ServiceArguments()
                            .With <ICurrentHttpRequest>(standInCurrentHttpRequest)
                            .With <IFubuRequest>(inMemoryFubuRequest);

            var behavior = ContainerFacilitySource
                           .BuildBehavior(arguments, ObjectDef.ForType <GuyWhoNeedsRequest>(), x => { })
                           .As <GuyWhoNeedsRequest>();

            behavior.Http.ShouldBeTheSameAs(standInCurrentHttpRequest);
            behavior.Request.ShouldBeTheSameAs(inMemoryFubuRequest);
        }
Exemplo n.º 29
0
        public void ObjectDef_with_one_explicit_dependency_defined_by_value()
        {
            var container = ContainerFacilitySource.New(x =>
            {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());

                var objectDef = ObjectDef.ForType <GuyWithService>();
                objectDef.DependencyByType <IService>(ObjectDef.ForValue(new DifferentService()));

                x.Register(typeof(GuyWithService), objectDef);
            });

            // The default IService is SimpleService, but the default ObjectDef (first one) explicitly
            // set up its IService dependency to be a "DifferentService"
            container.Get <GuyWithService>().Service.ShouldBeOfType <DifferentService>();
        }
        public void if_not_a_singleton_it_should_be_request_scoped()
        {
            var id = Guid.NewGuid();

            var facility = ContainerFacilitySource.New(x =>
            {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());

                x.Register(typeof(IActionBehavior), ObjectDef.ForType <Behavior1>().Named(id.ToString()));
            });

            var instance1 = facility.BuildBehavior(new ServiceArguments(), id);
            var instance2 = facility.BuildBehavior(new ServiceArguments(), id);

            instance1.ShouldNotBeTheSameAs(instance2);
        }