Пример #1
0
        public void Can_specify_all_to_throw_if_Any_property_is_unresolved()
        {
            var container = new Container();

            container.Register <ClientWithServiceAndStringProperty>(made: PropertiesAndFields.All(ifUnresolved: IfUnresolved.Throw));

            Assert.Throws <ContainerException>(() =>
                                               container.Resolve <ClientWithServiceAndStringProperty>());
        }
Пример #2
0
        public void Indexer_properties_should_be_ignored_by_All_properties_discovery()
        {
            var container = new Container();

            container.Register <FooWithIndexer>(made:
                                                PropertiesAndFields.All(ifUnresolved: IfUnresolved.Throw));

            Assert.DoesNotThrow(() =>
                                container.Resolve <FooWithIndexer>());
        }
        public void Test()
        {
            var container = new Container();

            container.Register <FooWithIndexer>(made: PropertiesAndFields.All(ifUnresolved: IfUnresolved.Throw));

            Assert.DoesNotThrow(() =>
                                container.Resolve <FooWithIndexer>()
                                );
        }
Пример #4
0
        /// <summary>Registers controllers types in container with InWebRequest reuse.</summary>
        /// <param name="container">Container to register controllers to.</param>
        /// <param name="controllerAssemblies">(optional) Uses <see cref="BuildManager.GetReferencedAssemblies"/> by default.</param>
        public static void RegisterMvcControllers(this IContainer container, IEnumerable <Assembly> controllerAssemblies = null)
        {
            controllerAssemblies = controllerAssemblies ?? GetReferencedAssemblies();
            container.RegisterMany(controllerAssemblies, type => type.IsAssignableTo(typeof(IController)),
                                   Reuse.InWebRequest, PropertiesAndFields.All(false, false, false, withInfo: (m, r) =>
            {
                if (m.DeclaringType != typeof(Controller) && m.DeclaringType != typeof(ControllerBase))
                {
                    return(PropertyOrFieldServiceInfo.Of(m).WithDetails(
                               ServiceDetails.Of(ifUnresolved: IfUnresolved.ReturnDefault), r));
                }

                return(null);
            }));
        }
Пример #5
0
        public void Can_resolve_only_properties()
        {
            var container = new Container();

            container.Register <IService, Service>();
            container.Register <IService, AnotherService>(serviceKey: "another");

            container.RegisterMany <ClientWithPropsAndFields>(
                made: PropertiesAndFields.All(withFields: false).OverrideWith(PropertiesAndFields.Of.Name("PInternal", serviceKey: "another")));

            var client = container.Resolve <ClientWithPropsAndFields>();

            Assert.That(client.P, Is.InstanceOf <Service>());
            Assert.That(client.PWithBackingInternalProperty, Is.InstanceOf <AnotherService>());
            Assert.That(client.F, Is.Null);
            Assert.That(client.PWithBackingField, Is.Null);
            Assert.That(client.PNonResolvable, Is.Null);
        }
Пример #6
0
        /// <summary>Registers controllers found in provided assemblies with <see cref="Reuse.InWebRequest"/>.</summary>
        /// <param name="container">Container.</param>
        /// <param name="config">Http configuration.</param>
        /// <param name="assemblies">Assemblies to look for controllers.</param>
        public static void RegisterWebApiControllers(this IContainer container, HttpConfiguration config,
                                                     IEnumerable <Assembly> assemblies = null)
        {
            var assembliesResolver = assemblies == null
                 ? config.Services.GetAssembliesResolver()
                 : new GivenAssembliesResolver(assemblies.ToList());

            var controllerTypeResolver = config.Services.GetHttpControllerTypeResolver();
            var controllerTypes        = controllerTypeResolver.GetControllerTypes(assembliesResolver);

            container.RegisterMany(controllerTypes, Reuse.InWebRequest,
                                   PropertiesAndFields.All(false, false, false, withInfo: (m, r) =>
            {
                if (m.DeclaringType != typeof(ApiController))
                {
                    return(PropertyOrFieldServiceInfo.Of(m).WithDetails(
                               ServiceDetails.Of(ifUnresolved: IfUnresolved.Throw), r));
                }

                return(null);
            }),
                                   nonPublicServiceTypes: true);
        }