using Autofac; namespace NitroxModel.Core; [TestClass] public class DependencyInjectionTests { [TestInitialize] public void Init() { NitroxServiceLocator.InitializeDependencyContainer(new DependencyInjectionTestsAutoFacRegistrar()); NitroxServiceLocator.BeginNewLifetimeScope(); } [TestMethod] public void ShouldResolveDependencyPolymorphically() { // Arrange IRootDependency polymorphicallyResolvedDependency = NitroxServiceLocator.LocateService(); // Assert polymorphicallyResolvedDependency.Should().NotBeNull(); polymorphicallyResolvedDependency.Should().BeOfType(); } [TestMethod] public void ShouldResolveConcreteType() { // Arrange DependencyWithRootDependency directConcreteTypeDependency = NitroxServiceLocator.LocateService(); // Assert directConcreteTypeDependency.Should().NotBeNull(); directConcreteTypeDependency.RootDependency.Should().NotBeNull(); directConcreteTypeDependency.RootDependency.Should().BeOfType(); } [TestMethod] public void ShouldResolveGenericDependencies() { // Arrange IServicer servicerA = NitroxServiceLocator.LocateService>(); IServicer servicerB = NitroxServiceLocator.LocateService>(); // Assert servicerA.Should().NotBeNull(); servicerA.Should().BeOfType(); Invoking(() => servicerA.PerformService(null)).Should().Throw(); servicerB.Should().NotBeNull(); servicerB.Should().BeOfType(); Invoking(() => servicerB.PerformService(null)).Should().Throw(); } [TestMethod] public void ShouldResolveGenericDependenciesFromManuallyConstructedTypeInstances() { // Arrange Type servicerInstanceType = typeof(IServicer<>); Type recipientAType = typeof(ServiceRecipientA); Type recipientBType = typeof(ServiceRecipientB); Type servicerAType = servicerInstanceType.MakeGenericType(recipientAType); Type servicerBType = servicerInstanceType.MakeGenericType(recipientBType); // Act IServicer servicerA = (BaseServiceProvider)NitroxServiceLocator.LocateService(servicerAType); IServicer servicerB = (BaseServiceProvider)NitroxServiceLocator.LocateService(servicerBType); // Assert servicerA.Should().NotBeNull(); servicerA.Should().BeOfType(); Invoking(() => servicerA.PerformService(null)).Should().Throw(); servicerB.Should().NotBeNull(); servicerB.Should().BeOfType(); Invoking(() => servicerB.PerformService(null)).Should().Throw(); } private class DependencyInjectionTestsAutoFacRegistrar : IAutoFacRegistrar { public void RegisterDependencies(ContainerBuilder containerBuilder) { containerBuilder.RegisterType().As(); containerBuilder.RegisterType(); containerBuilder.RegisterAssemblyTypes(Assembly.GetAssembly(GetType())) .AsClosedTypesOf(typeof(IServicer<>)); } } } public interface IRootDependency { } public class RootDependency : IRootDependency { } public class DependencyWithRootDependency { public IRootDependency RootDependency { get; } public DependencyWithRootDependency(IRootDependency rootDependency) { RootDependency = rootDependency; } } public interface IServiced { } public interface IServicer where T : IServiced { void PerformService(T serviced); } public class ServiceRecipientA : IServiced { } public class ServiceRecipientB : IServiced { } public abstract class BaseServiceProvider : IServicer where TServiced : IServiced { public abstract void PerformService(TServiced serviced); } public class ServiceAProvider : BaseServiceProvider { public override void PerformService(ServiceRecipientA serviced) { throw new NotImplementedException(); } } public class ServiceBProvider : BaseServiceProvider { public override void PerformService(ServiceRecipientB serviced) { throw new NotImplementedException(); } }