Example #1
0
        public void close_an_open_generic_type_from_registration_if_nothing_explicit_is_added()
        {
            var facility = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService <>), new ObjectDef(typeof(SimpleService <>)));
            });

            facility.Get <IService <string> >().ShouldBeOfType <SimpleService <string> >();
        }
Example #2
0
        public void simple_ObjectDef_by_type()
        {
            var container = ContainerFacilitySource.New(x => {
                x.Register(typeof(IService), ObjectDef.ForType <SimpleService>());
            });

            container.Get <IService>().ShouldBeOfType <SimpleService>();
        }
Example #3
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>();
        }
Example #4
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);
        }
Example #5
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>();
        }
Example #6
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));
        }
Example #7
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");
        }
Example #8
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 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 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);
        }
Example #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>();
        }
Example #12
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>());
        }
Example #14
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
        }
Example #15
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
        }
Example #16
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));
        }
Example #17
0
 public void has_the_IServiceFactory_registered()
 {
     ContainerFacilitySource.New(x => { })
     .Get <IServiceFactory>().ShouldBeOfType <AutofacContainerFacility>();
 }
Example #18
0
 public void can_retrieve_a_concrete_class()
 {
     ContainerFacilitySource.New(x => { })
     .Get <SomeSettings>().ShouldNotBeNull();
 }