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>();
        }
示例#2
0
        public void simple_ObjectDef_by_value()
        {
            var service = new SimpleService();

            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForValue(service));
            });

            container.Get <IService>().ShouldBeTheSameAs(service);
        }
        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>();
        }
示例#4
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));
        }
示例#5
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");
        }
示例#6
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 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);
        }
        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>();
        }
示例#9
0
        public void will_set_properties_that_are_explicitly_set_on_ObjectDef()
        {
            var node = Process.For <OutsideBehavior>();

            node.AddAfter(Process.For <InsideBehavior>());

            var def = node.As <IContainerModel>().ToObjectDef();

            var behavior = ContainerFacilitySource
                           .BuildBehavior(new ServiceArguments(), def, x => { })
                           .As <OutsideBehavior>();

            behavior.InsideBehavior.ShouldBeOfType <InsideBehavior>();
        }
        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>());
        }
示例#11
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);
        }
        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);
        }
示例#14
0
        public void ObjectDef_with_one_explicit_and_one_implicit_dependency()
        {
            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IThing), ObjectDef.ForType <ThingOne>());

                var def = ObjectDef.ForType <GuyWithServiceAndThing>();
                def.DependencyByType <IThing>(ObjectDef.ForType <ThingTwo>());

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

            var guyWithServiceAndThing = container.Get <GuyWithServiceAndThing>();

            guyWithServiceAndThing.Service.ShouldBeOfType <SimpleService>(); // auto-wired
            guyWithServiceAndThing.Thing.ShouldBeOfType <ThingTwo>();        // explicitly set to be ThingTwo, even though auto-wiring would have put ThingOne here
        }
        public void should_be_a_singleton_because_an_ObjectDef_says_that_it_should_be()
        {
            var container = ContainerFacilitySource.New(x =>
            {
                var objectDef         = ObjectDef.ForType <SimpleService>();
                objectDef.IsSingleton = true;

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

            // Use this static method to know whether or not a class
            // should be scoped as a singleton or by Http request
            // SimpleService is NOT normally a singleton, but we can make
            // it be so by telling the ObjectDef to make it so
            ServiceRegistry.ShouldBeSingleton(typeof(SimpleService))
            .ShouldBeFalse();

            container.Get <IService>().ShouldBeTheSameAs(container.Get <IService>());
        }
示例#16
0
        public void auto_wiring_applies_even_when_another_dependency_is_set_explicitly()
        {
            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IThing), ObjectDef.ForType <ThingOne>());

                var def = ObjectDef.ForType <GuyWithServiceAndThing>();
                def.DependencyByType <IThing>(ObjectDef.ForType <ThingTwo>());

                var highLevelDef = ObjectDef.ForType <HighLevelObject>();
                highLevelDef.DependencyByType <GuyWithServiceAndThing>(def);

                x.Register(typeof(IHighLevelObject), highLevelDef);
            });

            var guyWithServiceAndThing = container.Get <GuyWithServiceAndThing>();

            guyWithServiceAndThing.Service.ShouldBeOfType <SimpleService>();     // auto-wired
            guyWithServiceAndThing.Thing.ShouldBeOfType <ThingOne>();            // auto-wired, even though explicitly set to ThingTwo for HighLevelObject
        }
示例#17
0
        public void three_deep_explicitly_configured_dep_tree()
        {
            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IThing), ObjectDef.ForType <ThingOne>());

                var def = ObjectDef.ForType <GuyWithServiceAndThing>();
                def.DependencyByType <IThing>(ObjectDef.ForType <ThingTwo>());

                var highLevelDef = ObjectDef.ForType <HighLevelObject>();
                highLevelDef.DependencyByType <GuyWithServiceAndThing>(def);

                x.Register(typeof(IHighLevelObject), highLevelDef);
            });

            var guyWithServiceAndThing = container.Get <IHighLevelObject>().Guy;

            guyWithServiceAndThing.Service.ShouldBeOfType <SimpleService>(); // auto-wired
            guyWithServiceAndThing.Thing.ShouldBeOfType <ThingTwo>();        // explicitly set to be ThingTwo, even though auto-wiring would have put ThingOne here
        }
示例#18
0
        public void explicit_registration_of_an_ienumerable_argument()
        {
            var container = ContainerFacilitySource.New(x =>
            {
                x.Register(typeof(IService), ObjectDef.ForType <ExceptionCaseService>());
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
                x.Register(typeof(IService), ObjectDef.ForType <DifferentService>());


                var def = ObjectDef.ForType <ThingThatUsesLotsOfServices>();
                def.EnumerableDependenciesOf <IService>().Add(ObjectDef.ForType <OddballService>());
                def.EnumerableDependenciesOf <IService>().Add(ObjectDef.ForType <DifferentService>());


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

            container.Get <ThingThatUsesLotsOfServices>()
            .Services.Select(x => x.GetType())
            .ShouldHaveTheSameElementsAs(typeof(OddballService), typeof(DifferentService));
        }
示例#19
0
 public void has_the_IServiceFactory_registered()
 {
     ContainerFacilitySource.New(x => { })
     .Get <IServiceFactory>().ShouldBeOfType <AutofacContainerFacility>();
 }
示例#20
0
 public void can_retrieve_a_concrete_class()
 {
     ContainerFacilitySource.New(x => { })
     .Get <SomeSettings>().ShouldNotBeNull();
 }