/// <summary> /// Initializes a new instance of <see cref="ServiceDescriptor"/> with the specified <paramref name="implementationType"/>. /// </summary> /// <param name="serviceType">The <see cref="Type"/> of the service.</param> /// <param name="implementationType">The <see cref="Type"/> implementing the service.</param> /// <param name="lifecycle">The <see cref="LifecycleKind"/> of the service.</param> public ServiceDescriptor([NotNull] Type serviceType, [NotNull] Type implementationType, LifecycleKind lifecycle) : this(serviceType, lifecycle) { ImplementationType = implementationType; }
public ServiceDescriptor Describe( Type serviceType, Type implementationType, object implementationInstance, LifecycleKind lifecycle) { var serviceTypeName = serviceType.FullName; var implementationTypeName = _configuration.Get(serviceTypeName); if (!String.IsNullOrEmpty(implementationTypeName)) { try { implementationType = Type.GetType(implementationTypeName); } catch (Exception ex) { throw new Exception(string.Format("TODO: unable to locate implementation {0} for service {1}", implementationTypeName, serviceTypeName), ex); } } return(new ServiceDescriptor { ServiceType = serviceType, ImplementationType = implementationType, ImplementationInstance = implementationInstance, Lifecycle = lifecycle }); }
private ServiceDescriptor Describe <TService, TImplementation>(LifecycleKind lifecycle) { return(Describe( typeof(TService), typeof(TImplementation), lifecycle: lifecycle)); }
/// <summary> /// Initializes a new instance of <see cref="ServiceDescriptor"/> with the specified <paramref name="factory"/>. /// </summary> /// <param name="serviceType">The <see cref="Type"/> of the service.</param> /// <param name="factory">A factory used for creating service instances.</param> /// <param name="lifecycle">The <see cref="LifecycleKind"/> of the service.</param> public ServiceDescriptor([NotNull] Type serviceType, [NotNull] Func <IServiceProvider, object> factory, LifecycleKind lifecycle) : this(serviceType, lifecycle) { ImplementationFactory = factory; }
/// <summary> /// Initializes a new instance of <see cref="ServiceDescriptor"/> with the specified <paramref name="factory"/>. /// </summary> /// <param name="serviceType">The <see cref="Type"/> of the service.</param> /// <param name="factory">A factory used for creating service instances.</param> /// <param name="lifecycle">The <see cref="LifecycleKind"/> of the service.</param> public ServiceDescriptor([NotNull] Type serviceType, [NotNull] Func<IServiceProvider, object> factory, LifecycleKind lifecycle) : this(serviceType, lifecycle) { ImplementationFactory = factory; }
private ServiceDescriptor Describe <TService, TImplementation>(LifecycleKind lifecycle) { return(Describe( typeof(TService), typeof(TImplementation), null, // implementationInstance lifecycle)); }
private static IServiceCollection Add(IServiceCollection collection, Type service, Func <IServiceProvider, object> implementationFactory, LifecycleKind lifeCycle) { var descriptor = new ServiceDescriptor(service, implementationFactory, lifeCycle); return(collection.Add(descriptor)); }
private static IServiceCollection Add(IServiceCollection collection, Type service, Type implementationType, LifecycleKind lifeCycle) { var descriptor = new ServiceDescriptor(service, implementationType, lifeCycle); return(collection.Add(descriptor)); }
private ServiceDescriptor Describe( Type serviceType, Type implementationType, LifecycleKind lifecycle) { return(Describe( serviceType, implementationType, null, // implementationInstance lifecycle)); }
public void AddWithFactoryAddsServiceWithRightLifecyle(Action <IServiceCollection> addAction, LifecycleKind lifeCycle) { // Arrange var collection = new ServiceCollection(); // Act addAction(collection); // Assert var descriptor = Assert.Single(collection); Assert.Equal(typeof(IFakeService), descriptor.ServiceType); Assert.Same(_factory, descriptor.ImplementationFactory); Assert.Equal(lifeCycle, descriptor.Lifecycle); }
public void AddWithTypeAddsServiceWithRightLifecyle(Action <IServiceCollection> addTypeAction, Type expectedServiceType, Type expectedImplementationType, LifecycleKind lifeCycle) { // Arrange var collection = new ServiceCollection(); // Act addTypeAction(collection); // Assert var descriptor = Assert.Single(collection); Assert.Equal(expectedServiceType, descriptor.ServiceType); Assert.Equal(expectedImplementationType, descriptor.ImplementationType); Assert.Equal(lifeCycle, descriptor.Lifecycle); }
private static IRegistrationBuilder <object, T, U> ConfigureLifecycle <T, U>( this IRegistrationBuilder <object, T, U> registrationBuilder, LifecycleKind lifecycleKind) { switch (lifecycleKind) { case LifecycleKind.Singleton: registrationBuilder.SingleInstance(); break; case LifecycleKind.Scoped: registrationBuilder.InstancePerLifetimeScope(); break; case LifecycleKind.Transient: registrationBuilder.InstancePerDependency(); break; } return(registrationBuilder); }
private static ITypeLifetimeManager GetLifetimeManager(LifecycleKind lifecycle) { switch (lifecycle) { case LifecycleKind.Singleton: return(new ContainerControlledLifetimeManager()); case LifecycleKind.PerRequest: return(new PerRequestLifetimeManager()); case LifecycleKind.Scoped: return(new HierarchicalLifetimeManager()); case LifecycleKind.Transient: return(new TransientLifetimeManager()); case LifecycleKind.Default: return(null); default: throw new NotSupportedException(lifecycle.ToString()); } }
public ServiceDescriptor Describe(Type serviceType, Func <IServiceProvider, object> implementationFactory, LifecycleKind lifecycle) { var implementationType = GetRemappedImplementationType(serviceType); if (implementationType != null) { return(new ServiceDescriptor(serviceType, implementationType, lifecycle)); } return(new ServiceDescriptor(serviceType, implementationFactory, lifecycle)); }
private ServiceDescriptor(Type serviceType, LifecycleKind lifecycle) { Lifecycle = lifecycle; ServiceType = serviceType; }
public ServiceDescriptor Describe(Type serviceType, Type implementationType, LifecycleKind lifecycle) { implementationType = GetRemappedImplementationType(serviceType) ?? implementationType; return(new ServiceDescriptor(serviceType, implementationType, lifecycle)); }
public void Descriptor_ThrowsIfReplacedServiceCanotBeFound(DescribeAction action, LifecycleKind lifeCycle) { // Arrange var expected = Resources.FormatCannotLocateImplementation("Type-Does-NotExist", typeof(IFakeService).FullName); var configuration = new Configuration(); configuration.Add(new MemoryConfigurationSource()); configuration.Set(typeof(IFakeService).FullName, "Type-Does-NotExist"); var serviceDescriber = new ServiceDescriber(configuration); // Act and Assert var ex = Assert.Throws <InvalidOperationException>(() => action(serviceDescriber)); Assert.Equal(expected, ex.Message); }
public void Descriptor_ReplacesServicesFromConfiguration(DescribeAction action, LifecycleKind lifeCycle) { // Arrange var configuration = new Configuration(); configuration.Add(new MemoryConfigurationSource()); configuration.Set(typeof(IFakeService).FullName, typeof(FakeOuterService).AssemblyQualifiedName); var serviceDescriber = new ServiceDescriber(configuration); // Act var descriptor = action(serviceDescriber); // Assert Assert.Equal(typeof(IFakeService), descriptor.ServiceType); Assert.Equal(typeof(FakeOuterService), descriptor.ImplementationType); Assert.Equal(lifeCycle, descriptor.Lifecycle); Assert.Null(descriptor.ImplementationFactory); Assert.Null(descriptor.ImplementationInstance); }
public static IEnumerable <object[]> TestServiceDescriptors(LifecycleKind lifecycle) { Func <object, object, bool> compare; if (lifecycle == LifecycleKind.Transient) { // Expect service references to be different for transient descriptors compare = (service1, service2) => service1 != service2; } else { // Expect service references to be the same for singleton and scoped descriptors compare = (service1, service2) => service1 == service2; } // Implementation Type Descriptor yield return(new object[] { new[] { new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), lifecycle) }, typeof(IFakeService), compare, }); // Closed Generic Descriptor yield return(new object[] { new[] { new ServiceDescriptor(typeof(IFakeOpenGenericService <string>), typeof(FakeService), lifecycle) }, typeof(IFakeOpenGenericService <string>), compare, }); // Open Generic Descriptor yield return(new object[] { new[] { new ServiceDescriptor(typeof(IFakeService), typeof(FakeService), lifecycle), new ServiceDescriptor(typeof(IFakeOpenGenericService <>), typeof(FakeOpenGenericService <>), lifecycle), }, typeof(IFakeOpenGenericService <IFakeService>), compare, }); // Factory Descriptor yield return(new object[] { new[] { new ServiceDescriptor(typeof(IFakeService), _ => new FakeService(), lifecycle) }, typeof(IFakeService), compare, }); if (lifecycle == LifecycleKind.Singleton) { // Instance Descriptor yield return(new object[] { new[] { new ServiceDescriptor(typeof(IFakeService), new FakeService()) }, typeof(IFakeService), compare, }); } }
public static void RegisterAllTypes(this IUnityContainer container, Type type, LifecycleKind lifecycle) { foreach (Type impl in Assembly.GetAssembly(type).GetTypes().Where(x => x.IsClass && !x.IsAbstract && !x.IsGenericType)) { Type intf = impl.GetInterface("I" + impl.Name); if (intf != null) { var cycle = GetLifetimeManager(lifecycle); if (cycle != null) { container.RegisterType(intf, impl, cycle); } else { container.RegisterType(intf, impl); } } } }