public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor) { if (service == null) throw new ArgumentNullException("service"); var typedService = service as TypedService; if ((typedService == null)) yield break; var newGuid = Guid.NewGuid(); var registration = new ComponentRegistration( newGuid, new DelegateActivator(typedService.ServiceType, (c, p) => { try { return MockRepository.GenerateStub(typedService.ServiceType); } catch (Exception ex) { Type valueType = typedService.ServiceType; throw new ResolutionException(string.Format(CultureInfo.InvariantCulture, "Error while resolving {0}", valueType), ex); } }), new RootScopeLifetime(), InstanceSharing.Shared, InstanceOwnership.OwnedByLifetimeScope, new Service[] { new UniqueService(newGuid), typedService}, new Dictionary<string, object>()); yield return registration; }
/// <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(nameof(service)); if (registrationAccessor == null) throw new ArgumentNullException(nameof(registrationAccessor)); var swt = service as IServiceWithType; if (swt != null) { var serviceType = swt.ServiceType; Type elementType = null; if (serviceType.IsGenericEnumerableInterfaceType()) { elementType = serviceType.GetTypeInfo().GenericTypeArguments.First(); } else if (serviceType.IsArray) { elementType = serviceType.GetElementType(); } if (elementType != null) { var elementTypeService = swt.ChangeType(elementType); var elementArrayType = elementType.MakeArrayType(); var listType = typeof(List<>).MakeGenericType(elementType); var serviceTypeIsList = serviceType.IsGenericListOrCollectionInterfaceType(); var registration = new ComponentRegistration( Guid.NewGuid(), new DelegateActivator(elementArrayType, (c, p) => { var elements = c.ComponentRegistry.RegistrationsFor(elementTypeService); var items = elements.Select(cr => c.ResolveComponent(cr, p)).ToArray(); var result = Array.CreateInstance(elementType, items.Length); items.CopyTo(result, 0); return serviceTypeIsList ? Activator.CreateInstance(listType, result) : 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) { 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(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>(); }
public IEnumerable<IComponentRegistration> RegistrationsFor(Service service, Func<Service, IEnumerable<IComponentRegistration>> registrationAccessor) { var keyedService = service as KeyedService; if (keyedService == null || (!typeof(IInterceptor).IsAssignableFrom(keyedService.ServiceType))) { // It's not a request for the IInterceptor return Enumerable.Empty<IComponentRegistration>(); } Guid id; if (!Guid.TryParse(keyedService.ServiceKey.ToString(), out id)) { return Enumerable.Empty<IComponentRegistration>(); } var registration = new ComponentRegistration( id, new DelegateActivator(keyedService.ServiceType, (c, p) => { var attProvider = c.Resolve<IAttributeProvider>(); var contextProvider = c.Resolve<IContextProvider>(); var attributes = new List<Tuple<MethodInfo, Attribute>>(); if (!DynamicAttributeCache.ContainsKey(id) || !DynamicAttributeCache.TryGetValue(id, out attributes)) { Global.Logger.Info($"Couldn't get cached attributes for key {id}"); return new MethodInterceptorAdaptor(attProvider, contextProvider); } var dynamicAttributeProvider = new DynamicAttributeProvider(attProvider, () => attributes); return new MethodInterceptorAdaptor(dynamicAttributeProvider, contextProvider); }), new CurrentScopeLifetime(), InstanceSharing.None, InstanceOwnership.OwnedByLifetimeScope, new[] {service}, new Dictionary<string, object>()); return new IComponentRegistration[] {registration}; }