Added IExposedServiceTypesProvider

pull/81/head
Halil İbrahim Kalkan 9 years ago
parent a63082b270
commit ca0dd01485

@ -34,7 +34,7 @@ namespace Volo.DependencyInjection
{
//TODO: Make this code extensible, so we can add other conventions!
foreach (var serviceType in FindDefaultServiceTypes(type))
foreach (var serviceType in FindServiceTypes(type))
{
if (typeof(ITransientDependency).GetTypeInfo().IsAssignableFrom(type))
{
@ -53,8 +53,14 @@ namespace Volo.DependencyInjection
}
}
private static List<Type> FindDefaultServiceTypes(Type type)
private static List<Type> FindServiceTypes(Type type)
{
var customExposedServices = type.GetTypeInfo().GetCustomAttributes().OfType<IExposedServiceTypesProvider>().SelectMany(p => p.GetExposedServiceTypes()).ToList();
if (customExposedServices.Any())
{
return customExposedServices;
}
var serviceTypes = new List<Type>();
serviceTypes.Add(type);

@ -0,0 +1,19 @@
using System;
namespace Volo.DependencyInjection
{
public class ExposeServicesAttribute : Attribute, IExposedServiceTypesProvider
{
public Type[] ExposedServiceTypes { get; }
public ExposeServicesAttribute(params Type[] exposedServiceTypes)
{
ExposedServiceTypes = exposedServiceTypes;
}
public Type[] GetExposedServiceTypes()
{
return ExposedServiceTypes;
}
}
}

@ -0,0 +1,9 @@
using System;
namespace Volo.DependencyInjection
{
public interface IExposedServiceTypesProvider
{
Type[] GetExposedServiceTypes();
}
}

@ -42,6 +42,16 @@ namespace Volo.DependencyInjection.Tests
_services.ShouldContainScoped(typeof(MyScopedClass));
}
[Fact]
public void Should_Register_For_Exposed_Services()
{
_services.AddType(typeof(MyServiceWithExposeList));
_services.ShouldContain(typeof(IMyService1), typeof(MyServiceWithExposeList), ServiceLifetime.Transient);
_services.ShouldContain(typeof(IMyService2), typeof(MyServiceWithExposeList), ServiceLifetime.Transient);
_services.ShouldNotContain(typeof(MyServiceWithExposeList));
}
public class MyTransientClass : ITransientDependency
{
@ -56,5 +66,21 @@ namespace Volo.DependencyInjection.Tests
{
}
public interface IMyService1
{
}
public interface IMyService2
{
}
[ExposeServices(typeof(IMyService1), typeof(IMyService2))]
public class MyServiceWithExposeList : IMyService1, IMyService2, ITransientDependency
{
}
}
}

@ -39,5 +39,23 @@ namespace Volo.DependencyInjection.Tests
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(ServiceLifetime.Scoped);
}
public static void ShouldContain(this IServiceCollection services, Type serviceType, Type implementationType, ServiceLifetime lifetime)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldNotBeNull();
serviceDescriptor.ImplementationType.ShouldBe(implementationType);
serviceDescriptor.ImplementationFactory.ShouldBeNull();
serviceDescriptor.ImplementationInstance.ShouldBeNull();
serviceDescriptor.Lifetime.ShouldBe(lifetime);
}
public static void ShouldNotContain(this IServiceCollection services, Type serviceType)
{
var serviceDescriptor = services.FirstOrDefault(s => s.ServiceType == serviceType);
serviceDescriptor.ShouldBeNull();
}
}
}
Loading…
Cancel
Save