public void New_FactoryWithFactoryService_ShouldSucceed() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <TestServiceTwoParams <IFactory <string, TestServiceOneParam <string> >, double> >(); serviceCollection.AddTransient <TestServiceOneParam <string> >(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService <IFactory <double, TestServiceTwoParams <IFactory <string, TestServiceOneParam <string> >, double> > >(); var instance = factory.New(1); instance.Arg2.Should().Be(1); var instance2 = factory.New(2); instance2.Arg2.Should().Be(2); var factory2 = instance.Arg1; var instance3 = factory2.New("a"); instance3.Arg.Should().Be("a"); var instance4 = factory2.New("b"); instance4.Arg.Should().Be("b"); }
public void GetRequiredService_FactoryOfOneArgForInterfaceOfRegisteredType_ShouldThrow() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <TestServiceOneParam <string> >(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); Func <IFactory <string, ITestInterfaceOneParam <string> > > factory = () => serviceProvider.GetRequiredService <IFactory <string, ITestInterfaceOneParam <string> > >(); factory.Should().Throw <InvalidOperationException>() .WithMessage($"Unable to resolve service for type '{typeof(ITestInterfaceOneParam<string>)}' while attempting to activate '{typeof(IFactory<string, ITestInterfaceOneParam<string>>)}'."); }
public void New_FactoryOfOneArgForRegisteredInterface_ShouldSucceed() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <ITestInterfaceOneParam <string>, TestServiceOneParam <string> >(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService <IFactory <string, ITestInterfaceOneParam <string> > >(); factory.New("a").Arg.Should().Be("a"); factory.New("b").Arg.Should().Be("b"); }
public void GetRequiredService_FactoryOfOneArgForTypeWithTwoMatchingConstructors_ShouldThrow() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <TwoConstructors>(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); Func <IFactory <int, TwoConstructors> > factory = () => serviceProvider.GetRequiredService <IFactory <int, TwoConstructors> >(); factory.Should().Throw <InvalidOperationException>() .WithMessage($"In order to resolve a parameterised factory for service type '{typeof(TwoConstructors)}', the implementation type '{typeof(TwoConstructors)}' must contain just one constructor whose last parameter is assignable from the factory argument type '{typeof(int)}'."); }
public void GetRequiredService_FactoryOfTwoArgsForTypeWithWrongConstructor_ShouldThrow() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <WrongConstructor>(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); Func <IFactory <string, double, WrongConstructor> > factory = () => serviceProvider.GetRequiredService <IFactory <string, double, WrongConstructor> >(); factory.Should().Throw <InvalidOperationException>() .WithMessage($"In order to resolve a parameterised factory for service type '{typeof(WrongConstructor)}', the implementation type '{typeof(WrongConstructor)}' must contain a constructor whose last 2 parameters are assignable from the factory argument types '{typeof(string)}', '{typeof(double)}'."); }
public void New_FactoryOfOneArgOfAssignableType_ShouldSucceed() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <TestServiceOneParam <IEnumerable <string> > >(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService <IFactory <string[], TestServiceOneParam <IEnumerable <string> > > >(); factory.New(new[] { "a" }).Arg.Single().Should().Be("a"); factory.New(new[] { "b" }).Arg.Single().Should().Be("b"); }
public void GetRequiredService_FactoryOfOneArgForTypeRegisteredAsNonTransient_ShouldThrow() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddScoped <TestServiceOneParam <string> >(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); Func <IFactory <string, TestServiceOneParam <string> > > factory = () => serviceProvider.GetRequiredService <IFactory <string, TestServiceOneParam <string> > >(); factory.Should().Throw <InvalidOperationException>() .WithMessage($"In order to resolve a parameterised factory for service type '{typeof(TestServiceOneParam<string>)}', the implementation type '{typeof(TestServiceOneParam<string>)}' must be registered with Transient lifestyle."); }
public void New_FactoryOfTwoArgWithMissingService_ShouldThrow() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <WrongConstructor>(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService <IFactory <string, int, WrongConstructor> >(); Func <WrongConstructor> instance = () => factory.New("a", 1); instance.Should().Throw <InvalidOperationException>() .WithMessage($"Unable to resolve service for type '{typeof(double)}' while attempting to activate '{typeof(WrongConstructor)}'."); }
public void ShouldResolveFactoryRespectingLifestyle() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddSingleton <TestSingleton>(); serviceCollection.AddTransient <TestTransient>(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var transientFactory = serviceProvider.GetRequiredService <IFactory <TestTransient> >(); var singletonFactory = serviceProvider.GetRequiredService <IFactory <TestSingleton> >(); transientFactory.New().Should().NotBeSameAs(transientFactory.New()); serviceProvider.GetRequiredService <TestTransient>().Should().NotBeSameAs(transientFactory.New()); singletonFactory.New().Should().BeSameAs(singletonFactory.New()); serviceProvider.GetRequiredService <TestSingleton>().Should().BeSameAs(singletonFactory.New()); }
public void New_FactoryOfTwoArgsForRegisteredInterface_ShouldSucceed() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <ITestInterfaceTwoParams <string, double>, TestServiceTwoParams <string, double> >(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService <IFactory <string, double, ITestInterfaceTwoParams <string, double> > >(); var x = factory.New("a", 1.0); x.Arg1.Should().Be("a"); x.Arg2.Should().Be(1.0); var y = factory.New("b", 2.0); y.Arg1.Should().Be("b"); y.Arg2.Should().Be(2.0); }
public void New_FactoryOfThreeArgsForRegisteredType_ShouldSucceed() { var serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddTransient <TestServiceThreeParams>(); serviceCollection.AddFactoryFacility(); var serviceProvider = serviceCollection.BuildServiceProvider(); var factory = serviceProvider.GetRequiredService <IFactory <string, double, bool, TestServiceThreeParams> >(); var x = factory.New("a", 1.0, false); x.Arg1.Should().Be("a"); x.Arg2.Should().Be(1.0); x.Arg3.Should().BeFalse(); var y = factory.New("b", 2.0, true); y.Arg1.Should().Be("b"); y.Arg2.Should().Be(2.0); y.Arg3.Should().BeTrue(); }