Browse Source

Added tests and made ConventionalRegistrars dictionary concurrent.

pull/81/head
Halil İbrahim Kalkan 10 years ago
parent
commit
aebc6c092d
  1. 19
      src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionConventionalRegistrationExtensions.cs
  2. 111
      test/Volo.DependencyInjection.Tests/Microsoft/Extensions/DependencyInjection/AbpConventionalDependencyInjectionExtensions_Tests.cs
  3. 19
      test/Volo.DependencyInjection.Tests/Microsoft/Extensions/DependencyInjection/AbpDependencyInjectionSpecifications.cs

19
src/Volo.DependencyInjection/Microsoft/Extensions/DependencyInjection/ServiceCollectionConventionalRegistrationExtensions.cs

@ -1,4 +1,5 @@
using System.Collections.Generic; using System.Collections.Concurrent;
using System.Collections.Generic;
using Volo.DependencyInjection; using Volo.DependencyInjection;
using Volo.ExtensionMethods.Collections.Generic; using Volo.ExtensionMethods.Collections.Generic;
@ -11,11 +12,11 @@ namespace Microsoft.Extensions.DependencyInjection
public static class ServiceCollectionConventionalRegistrationExtensions public static class ServiceCollectionConventionalRegistrationExtensions
{ {
private static readonly Dictionary<IServiceCollection, List<IConventionalRegistrar>> ConventionalRegistrars; private static readonly ConcurrentDictionary<IServiceCollection, List<IConventionalRegistrar>> ConventionalRegistrars;
static ServiceCollectionConventionalRegistrationExtensions() static ServiceCollectionConventionalRegistrationExtensions()
{ {
ConventionalRegistrars = new Dictionary<IServiceCollection, List<IConventionalRegistrar>>(); ConventionalRegistrars = new ConcurrentDictionary<IServiceCollection, List<IConventionalRegistrar>>();
} }
public static IServiceCollection AddConventionalRegistrar(this IServiceCollection services, IConventionalRegistrar registrar) public static IServiceCollection AddConventionalRegistrar(this IServiceCollection services, IConventionalRegistrar registrar)
@ -31,16 +32,12 @@ namespace Microsoft.Extensions.DependencyInjection
private static List<IConventionalRegistrar> GetOrCreateRegistrarList(IServiceCollection services) private static List<IConventionalRegistrar> GetOrCreateRegistrarList(IServiceCollection services)
{ {
var registrars = ConventionalRegistrars.GetOrDefault(services); return ConventionalRegistrars.GetOrAdd(
if (registrars == null) services,
{ () => new List<IConventionalRegistrar>
registrars = ConventionalRegistrars[services] = new List<IConventionalRegistrar>
{ {
new DefaultConventionalRegistrar() new DefaultConventionalRegistrar()
}; });
}
return registrars;
} }
} }
} }

111
test/Volo.DependencyInjection.Tests/Microsoft/Extensions/DependencyInjection/AbpConventionalDependencyInjectionExtensions_Tests.cs

@ -1,4 +1,5 @@
using Shouldly; using System.Linq;
using Shouldly;
using Volo.DependencyInjection; using Volo.DependencyInjection;
using Xunit; using Xunit;
@ -43,16 +44,92 @@ namespace Microsoft.Extensions.DependencyInjection
_services.ShouldContainScoped(typeof(MyScopedClass)); _services.ShouldContainScoped(typeof(MyScopedClass));
} }
[Fact]
public void Should_Register_Scoped_With_Dependency_Attribute()
{
//Act
_services.AddType(typeof(MyScopedClassWithDependencyAttribute));
//Assert
_services.ShouldContainScoped(typeof(MyScopedClassWithDependencyAttribute));
}
[Fact]
public void Dependency_Attribute_Should_Override_Interface_Lifetimes()
{
//Act
_services.AddType(typeof(MyScopedClassWithDependencyAttribute2));
//Assert
_services.ShouldContainScoped(typeof(MyScopedClassWithDependencyAttribute2));
}
[Fact] [Fact]
public void Should_Register_For_Exposed_Services() public void Should_Register_For_Exposed_Services()
{ {
//Act
_services.AddType(typeof(MyServiceWithExposeList)); _services.AddType(typeof(MyServiceWithExposeList));
//Assert
_services.ShouldContain(typeof(IMyService1), typeof(MyServiceWithExposeList), ServiceLifetime.Transient); _services.ShouldContain(typeof(IMyService1), typeof(MyServiceWithExposeList), ServiceLifetime.Transient);
_services.ShouldContain(typeof(IMyService2), typeof(MyServiceWithExposeList), ServiceLifetime.Transient); _services.ShouldContain(typeof(IMyService2), typeof(MyServiceWithExposeList), ServiceLifetime.Transient);
_services.ShouldNotContain(typeof(MyServiceWithExposeList)); _services.ShouldNotContain(typeof(MyServiceWithExposeList));
} }
[Fact]
public void Should_Register_Multiple_Implementation_For_Same_Service()
{
//Act
_services.AddTypes(typeof(FirstImplOfMyService), typeof(SecondImplOfMyService));
//Assert
//Check descriptons in service collection
var descriptions = _services.Where(s => s.ServiceType == typeof(IMyService)).ToList();
descriptions.Count.ShouldBe(2);
descriptions[0].ImplementationType.ShouldBe(typeof(FirstImplOfMyService));
descriptions[1].ImplementationType.ShouldBe(typeof(SecondImplOfMyService));
//Check from service provider
var serviceProvider = _services.BuildServiceProvider();
//Default service should be second one
serviceProvider.GetRequiredService<IMyService>().ShouldBeOfType(typeof(SecondImplOfMyService));
//Should also get all services
var instances = serviceProvider.GetServices<IMyService>().ToList();
instances.Count.ShouldBe(2);
instances[0].ShouldBeOfType(typeof(FirstImplOfMyService));
instances[1].ShouldBeOfType(typeof(SecondImplOfMyService));
}
[Fact]
public void Should_Not_Register_Second_Implementation_For_Same_Service_If_Second_Is_Marked_As_TryRegister()
{
//Act
_services.AddTypes(typeof(FirstImplOfMyService), typeof(TryRegisterImplOfMyService));
//Assert
//Check descriptons in service collection
var descriptions = _services.Where(s => s.ServiceType == typeof(IMyService)).ToList();
descriptions.Count.ShouldBe(1);
descriptions[0].ImplementationType.ShouldBe(typeof(FirstImplOfMyService));
//Check from service provider
var serviceProvider = _services.BuildServiceProvider();
//Default service should be second one
serviceProvider.GetRequiredService<IMyService>().ShouldBeOfType(typeof(FirstImplOfMyService));
//Should also get all services
var instances = serviceProvider.GetServices<IMyService>().ToList();
instances.Count.ShouldBe(1);
instances[0].ShouldBeOfType(typeof(FirstImplOfMyService));
}
[Fact] [Fact]
public void AddObjectAccessor_Test() public void AddObjectAccessor_Test()
{ {
@ -90,6 +167,18 @@ namespace Microsoft.Extensions.DependencyInjection
} }
[Dependency(ServiceLifetime.Scoped)]
public class MyScopedClassWithDependencyAttribute
{
}
[Dependency(ServiceLifetime.Scoped)] //Attribute overrides interface
public class MyScopedClassWithDependencyAttribute2 : ITransientDependency
{
}
public interface IMyService1 public interface IMyService1
{ {
@ -99,13 +188,31 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
} }
[ExposeServices(typeof(IMyService1), typeof(IMyService2))] [ExposeServices(typeof(IMyService1), typeof(IMyService2))]
public class MyServiceWithExposeList : IMyService1, IMyService2, ITransientDependency public class MyServiceWithExposeList : IMyService1, IMyService2, ITransientDependency
{ {
} }
public interface IMyService : ITransientDependency
{
}
public class FirstImplOfMyService : IMyService
{
}
public class SecondImplOfMyService : IMyService
{
}
[Dependency(TryRegister = true)]
public class TryRegisterImplOfMyService : IMyService
{
}
public class MyEmptyClass public class MyEmptyClass
{ {

19
test/Volo.DependencyInjection.Tests/Microsoft/Extensions/DependencyInjection/AbpDependencyInjectionSpecifications.cs

@ -1,19 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Volo.DependencyInjection;
namespace Microsoft.Extensions.DependencyInjection
{
public class AbpDependencyInjectionSpecifications
{
//TODO: Tests...
public class MyScopedClass : IScopedDependency
{
}
}
}
Loading…
Cancel
Save