/// <summary>
        /// Retrieve registrations for an unregistered service, to be used
        /// by the container.
        /// </summary>
        /// <param name="service">The service that was requested.</param>
        /// <param name="registrationAccessor">A function that will return existing registrations for a service.</param>
        /// <returns>Registrations providing the service.</returns>
        public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
        {
            if (service == null) throw new ArgumentNullException("service");
            if (registrationAccessor == null) throw new ArgumentNullException("registrationAccessor");

            var ts = service as IServiceWithType;
            if (ts != null && ts.ServiceType.IsDelegate())
            {
                var resultType = ts.ServiceType.FunctionReturnType();
                var resultTypeService = ts.ChangeType(resultType);
                var hasArguments = ts.ServiceType.IsGenericType && ts.ServiceType.GetGenericArguments().Length > 1;

                return registrationAccessor(resultTypeService)
                    .Where(r => r.Target != null && (hasArguments || r.Target.Sharing == InstanceSharing.None))
                    .Select(r =>
                    {
            #if WINDOWS_PHONE
                        var factory = new Util.WindowsPhone.Wp7FactoryGenerator(ts.ServiceType, r, ParameterMapping.Adaptive);
            #else
                        var factory = new Lazy<FactoryGenerator>(() => new FactoryGenerator(ts.ServiceType, ParameterMapping.Adaptive));
            #endif
                        var rb = RegistrationBuilder.ForDelegate(ts.ServiceType, (c, p) => factory.Value.GenerateFactory(resultTypeService, r.Target, c, p))
                            .InstancePerLifetimeScope()
                            .ExternallyOwned()
                            .As(service)
                            .Targeting(r);

                        return rb.CreateRegistration();
                    });
            }

            return Enumerable.Empty<IComponentRegistration>();
        }
        /// <summary>
        /// Retrieve registrations for an unregistered service, to be used
        /// by the container.
        /// </summary>
        /// <param name="service">The service that was requested.</param>
        /// <param name="registrationAccessor">A function that will return existing registrations for a service.</param>
        /// <returns>Registrations providing the service.</returns>
        public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor)
        {
            if (service == null) throw new ArgumentNullException("service");
            if (registrationAccessor == null) throw new ArgumentNullException("registrationAccessor");

            var ts = service as IServiceWithType;
            if (ts != null && ts.ServiceType.IsDelegate())
            {
                var resultType = ts.ServiceType.FunctionReturnType();
                var resultTypeService = ts.ChangeType(resultType);

                return registrationAccessor(resultTypeService)
                    .Select(r =>
                    {
            #if WINDOWS_PHONE
                        var factory = new Util.WindowsPhone.Wp7FactoryGenerator(ts.ServiceType, r, ParameterMapping.Adaptive);
            #else
                        var factory = new FactoryGenerator(ts.ServiceType, r, ParameterMapping.Adaptive);
            #endif
                        var rb = RegistrationBuilder.ForDelegate(ts.ServiceType, factory.GenerateFactory)
                            .InstancePerLifetimeScope()
                            .ExternallyOwned()
                            .As(service)
                            .Targeting(r);

                        return rb.CreateRegistration();
                    });
            }

            return Enumerable.Empty<IComponentRegistration>();
        }
Пример #3
0
        /// <summary>
        /// Retrieve registrations for an unregistered service, to be used
        /// by the container.
        /// </summary>
        /// <param name="service">The service that was requested.</param>
        /// <param name="registrationAccessor">A function that will return existing registrations for a service.</param>
        /// <returns>Registrations providing the service.</returns>
        public IEnumerable <IComponentRegistration> RegistrationsFor(Service service, Func <Service, IEnumerable <IComponentRegistration> > registrationAccessor)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }
            if (registrationAccessor == null)
            {
                throw new ArgumentNullException("registrationAccessor");
            }

            var ts = service as IServiceWithType;

            if (ts != null && ts.ServiceType.IsDelegate())
            {
                var resultType        = ts.ServiceType.FunctionReturnType();
                var resultTypeService = ts.ChangeType(resultType);
                var hasArguments      = ts.ServiceType.IsGenericType && ts.ServiceType.GetGenericArguments().Length > 1;

                return(registrationAccessor(resultTypeService)
                       .Where(r => r.Target != null && (hasArguments || r.Target.Sharing == InstanceSharing.None))
                       .Select(r =>
                {
#if WINDOWS_PHONE
                    var factory = new Util.WindowsPhone.Wp7FactoryGenerator(ts.ServiceType, r, ParameterMapping.Adaptive);
#else
                    var factory = new Lazy <FactoryGenerator>(() => new FactoryGenerator(ts.ServiceType, ParameterMapping.Adaptive));
#endif
                    var rb = RegistrationBuilder.ForDelegate(ts.ServiceType, (c, p) => factory.Value.GenerateFactory(resultTypeService, r.Target, c, p))
                             .InstancePerLifetimeScope()
                             .ExternallyOwned()
                             .As(service)
                             .Targeting(r)
                             .InheritRegistrationOrderFrom(r);

                    return rb.CreateRegistration();
                }));
            }

            return(Enumerable.Empty <IComponentRegistration>());
        }
        /// <summary>
        /// Retrieve registrations for an unregistered service, to be used
        /// by the container.
        /// </summary>
        /// <param name="service">The service that was requested.</param>
        /// <param name="registrationAccessor">A function that will return existing registrations for a service.</param>
        /// <returns>Registrations providing the service.</returns>
        public IEnumerable <IComponentRegistration> RegistrationsFor(Service service, Func <Service, IEnumerable <IComponentRegistration> > registrationAccessor)
        {
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }
            if (registrationAccessor == null)
            {
                throw new ArgumentNullException("registrationAccessor");
            }

            var ts = service as IServiceWithType;

            if (ts != null && ts.ServiceType.IsDelegate())
            {
                var resultType        = ts.ServiceType.FunctionReturnType();
                var resultTypeService = ts.ChangeType(resultType);

                return(registrationAccessor(resultTypeService)
                       .Select(r =>
                {
#if WINDOWS_PHONE
                    var factory = new Util.WindowsPhone.Wp7FactoryGenerator(ts.ServiceType, r, ParameterMapping.Adaptive);
#else
                    var factory = new FactoryGenerator(ts.ServiceType, r, ParameterMapping.Adaptive);
#endif
                    var rb = RegistrationBuilder.ForDelegate(ts.ServiceType, factory.GenerateFactory)
                             .InstancePerLifetimeScope()
                             .ExternallyOwned()
                             .As(service)
                             .Targeting(r);

                    return rb.CreateRegistration();
                }));
            }

            return(Enumerable.Empty <IComponentRegistration>());
        }