/// <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> /// <remarks> /// If the source is queried for service s, and it returns a component that implements both s and s', then it /// will not be queried again for either s or s'. This means that if the source can return other implementations /// of s', it should return these, plus the transitive closure of other components implementing their /// additional services, along with the implementation of s. It is not an error to return components /// that do not implement <paramref name="service"/>. /// </remarks> 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"); int contravariantParameterIndex; var swt = service as IServiceWithType; if (swt == null || !IsCompatibleInterfaceType(swt.ServiceType, out contravariantParameterIndex)) return Enumerable.Empty<IComponentRegistration>(); var args = swt.ServiceType.GetGenericArguments(); var definition = swt.ServiceType.GetGenericTypeDefinition(); var contravariantParameter = args[contravariantParameterIndex]; var possibleSubstitutions = GetTypesAssignableFrom(contravariantParameter); var variations = possibleSubstitutions .Select(s => SubstituteArrayElementAt(args, s, contravariantParameterIndex)) .Where(a => definition.IsCompatibleWithGenericParameterConstraints(a)) .Select(a => definition.MakeGenericType(a)); var variantRegistrations = variations .SelectMany(v => registrationAccessor(swt.ChangeType(v))) .Where(r => !r.Metadata.ContainsKey(IsContravariantAdapter)); return variantRegistrations .Select(vr => RegistrationBuilder .ForDelegate((c, p) => c.ResolveComponent(service, vr, p)) .Targeting(vr) .As(service) .WithMetadata(IsContravariantAdapter, true) .CreateRegistration()); }
/// <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> /// Initializes a new instance of the <see cref="PreparingEventArgs"/> class. /// </summary> /// <param name="service">Service which is preparing</param> /// <param name="context">The context.</param> /// <param name="component">The component.</param> /// <param name="parameters">The parameters.</param> public PreparingEventArgs(Service service, IComponentContext context, IComponentRegistration component, IEnumerable<Parameter> parameters) { _service = Enforce.ArgumentNotNull(service, "service"); _context = Enforce.ArgumentNotNull(context, "context"); _component = Enforce.ArgumentNotNull(component, "component"); _parameters = Enforce.ArgumentNotNull(parameters, "parameters"); }
/// <summary> /// Create an instance of <see cref="LightweightAdapterActivatorData"/>. /// </summary> /// <param name="fromService">The service that will be adapted from.</param> /// <param name="adapter">The adapter function.</param> public LightweightAdapterActivatorData( Service fromService, Func<IComponentContext, IEnumerable<Parameter>, object, object> adapter) { _fromService = fromService; _adapter = adapter; }
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"); Type constructedImplementationType; IEnumerable<Service> services; if (OpenGenericServiceBinder.TryBindServiceType(service, _registrationData.Services, _activatorData.ImplementationType, out constructedImplementationType, out services)) { var swt = (IServiceWithType)service; var fromService = _activatorData.FromService.ChangeType(swt.ServiceType); return registrationAccessor(fromService) .Select(cr => RegistrationBuilder.CreateRegistration( Guid.NewGuid(), _registrationData, new ReflectionActivator( constructedImplementationType, _activatorData.ConstructorFinder, _activatorData.ConstructorSelector, AddDecoratedComponentParameter(swt.ServiceType, cr, _activatorData.ConfiguredParameters), _activatorData.ConfiguredProperties), services)); } return Enumerable.Empty<IComponentRegistration>(); }
/// <summary> /// Resolve an instance of the provided registration within the context. /// </summary> /// <param name="context">provided context</param> /// <param name="service">Service which is trying to be resolved</param> /// <param name="registration">The registration.</param> /// <param name="parameters">Parameters for the instance.</param> /// <returns> /// The component instance. /// </returns> /// <exception cref="ComponentNotRegisteredException"/> /// <exception cref="Revenj.Extensibility.Autofac.Core.DependencyResolutionException"/> public static object ResolveComponent(this IComponentContext context, Service service, IComponentRegistration registration, IEnumerable<Parameter> parameters) { var lookup = context.ResolveLookup(service, registration, parameters); try { return lookup.Factory(); } catch (DependencyResolutionException dre) { dre.Lookups.Push(lookup); throw; } }
/// <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 swt = service as IServiceWithType; if (swt != null) { var serviceType = swt.ServiceType; Type elementType = null; if (serviceType.IsGenericTypeDefinedBy(typeof(IEnumerable<>))) { elementType = serviceType.GetGenericArguments()[0]; } else if (serviceType.IsArray) { elementType = serviceType.GetElementType(); } if (elementType != null) { var elementTypeService = swt.ChangeType(elementType); var elementArrayType = elementType.MakeArrayType(); #if WINDOWS_PHONE //.MakeArrayType() doesn't work for some types on WP7 if (elementArrayType == null) elementArrayType = typeof (IEnumerable<>).MakeGenericType(elementType); #endif var registration = new ComponentRegistration( Guid.NewGuid(), new DelegateActivator(elementArrayType, (c, p) => { var elements = c.ComponentRegistry.RegistrationsFor(elementTypeService); var items = elements.Select(cr => c.ResolveComponent(service, cr, p)).ToArray(); var result = Array.CreateInstance(elementType, items.Length); items.CopyTo(result, 0); return result; }), new CurrentScopeLifetime(), InstanceSharing.None, InstanceOwnership.ExternallyOwned, new[] { service }, new Dictionary<string, object>()); return new IComponentRegistration[] { registration }; } } 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) { var ts = service as TypedService; if (ts == null || !ts.ServiceType.IsClass || ts.ServiceType.IsSubclassOf(typeof(Delegate)) || ts.ServiceType.IsAbstract || !_predicate(ts.ServiceType) || registrationAccessor(service).Any()) return Enumerable.Empty<IComponentRegistration>(); return new[] { RegistrationBuilder.ForType(ts.ServiceType).CreateRegistration() }; }
public static bool TryBindServiceType( Service service, IEnumerable<Service> configuredOpenGenericServices, Type openGenericImplementationType, out Type constructedImplementationType, out IEnumerable<Service> constructedServices) { var swt = service as IServiceWithType; if (swt != null && swt.ServiceType.IsGenericType) { var definitionService = (IServiceWithType)swt.ChangeType(swt.ServiceType.GetGenericTypeDefinition()); var serviceGenericArguments = swt.ServiceType.GetGenericArguments(); if (configuredOpenGenericServices.Cast<IServiceWithType>().Any(s => s.Equals(definitionService))) { var implementorGenericArguments = TryMapImplementationGenericArguments( openGenericImplementationType, swt.ServiceType, definitionService.ServiceType, serviceGenericArguments); if (!implementorGenericArguments.Any(a => a == null) && openGenericImplementationType.IsCompatibleWithGenericParameterConstraints(implementorGenericArguments)) { var constructedImplementationTypeTmp = openGenericImplementationType.MakeGenericType(implementorGenericArguments); // This needs looking at var implementedServices = (from IServiceWithType s in configuredOpenGenericServices let genericService = s.ServiceType.MakeGenericType(serviceGenericArguments) where genericService.IsAssignableFrom(constructedImplementationTypeTmp) select s.ChangeType(genericService)).ToArray(); if (implementedServices.Length > 0) { constructedImplementationType = constructedImplementationTypeTmp; constructedServices = implementedServices; return true; } } } } constructedImplementationType = null; constructedServices = null; return false; }
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"); Type constructedImplementationType; IEnumerable<Service> services; if (OpenGenericServiceBinder.TryBindServiceType(service, _registrationData.Services, _activatorData.ImplementationType, out constructedImplementationType, out services)) { yield return RegistrationBuilder.CreateRegistration( Guid.NewGuid(), _registrationData, new ReflectionActivator( constructedImplementationType, _activatorData.ConstructorFinder, _activatorData.ConstructorSelector, _activatorData.ConfiguredParameters, _activatorData.ConfiguredProperties), services); } }
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"); if (_registrationData.Services.Contains(service)) { return registrationAccessor(_activatorData.FromService) .Select(r => { var rb = RegistrationBuilder .ForDelegate((c, p) => _activatorData.Adapter(c, p, c.ResolveComponent(service, r, Enumerable.Empty<Parameter>()))) .Targeting(r); rb.RegistrationData.CopyFrom(_registrationData, true); return rb.CreateRegistration(); }); } return new IComponentRegistration[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.IsGenericTypeDefinedBy(typeof(Owned<>))) return Enumerable.Empty<IComponentRegistration>(); var ownedInstanceType = ts.ServiceType.GetGenericArguments()[0]; var ownedInstanceService = ts.ChangeType(ownedInstanceType); return registrationAccessor(ownedInstanceService) .Select(r => { var rb = RegistrationBuilder.ForDelegate(ts.ServiceType, (c, p) => { var lifetime = c.Resolve<ILifetimeScope>().BeginLifetimeScope(ownedInstanceService); try { var value = lifetime.ResolveComponent(service, r, p); return Activator.CreateInstance(ts.ServiceType, new[] { value, lifetime }); } catch { lifetime.Dispose(); throw; } }) .ExternallyOwned() .As(service) .Targeting(r); return rb.CreateRegistration(); }); }
/// <summary> /// Resolve an instance of the provided registration within the context. /// </summary> /// <param name="service">Service which is trying to be resolved</param> /// <param name="registration">The registration.</param> /// <param name="parameters">Parameters for the instance.</param> /// <returns> /// The component instance. /// </returns> /// <exception cref="ComponentNotRegisteredException"/> /// <exception cref="DependencyResolutionException"/> public IInstanceLookup ResolveLookup(Service service, IComponentRegistration registration, IEnumerable<Parameter> parameters) { return _rootLifetimeScope.ResolveLookup(service, registration, parameters); }
/// <summary> /// Create a new GeneratedFactoryActivatorData /// </summary> /// <param name="delegateType">The type of the factory.</param> /// <param name="productService">The service used to provide the products of the factory.</param> public GeneratedFactoryActivatorData(Type delegateType, Service productService) { _delegateType = Enforce.ArgumentNotNull(delegateType, "delegateType"); _productService = Enforce.ArgumentNotNull(productService, "productService"); }
/// <summary> /// Resolve an instance of the provided registration within the context. /// </summary> /// <param name="service">Service which is trying to be resolved</param> /// <param name="registration">The registration.</param> /// <param name="parameters">Parameters for the instance.</param> /// <returns> /// The component instance. /// </returns> /// <exception cref="ComponentNotRegisteredException"/> /// <exception cref="DependencyResolutionException"/> public IInstanceLookup ResolveLookup(Service service, IComponentRegistration registration, IEnumerable <Parameter> parameters) { return(_rootLifetimeScope.ResolveLookup(service, registration, parameters)); }