public void CreateDelegateBuilder() { var builder = new ServiceDefintions() .AddSingleton <IInterceptorConfiguration>(i => { var c = new InterceptorConfiguration(); c.Interceptors.AddType <AddOne2>(); c.Interceptors.AddType <AddOne2>(whitelists: m => m != null); c.Interceptors.AddType <AddOne2>(blacklists: m => m != null); return(c); }) .AddTransient <IInterceptorConfigurationHandler>(i => { return(new InterceptorAttributeConfigurationHandler(null)); }) .AddTransient <IInterceptorCreatorFactory, InterceptorCreatorFactory>() .AddSingleton <IInterceptDelegateBuilder>(i => { return(i.GetRequiredService <IInterceptorCreatorFactory>().Build()); }) .BuildServiceProvider() .GetRequiredService <IInterceptDelegateBuilder>(); Assert.NotNull(builder); }
public void ScopedWhenObjectAndGetIEnumerableIntWithNewScoped() { var service = new ServiceDefintions() .AddScoped <IEnumerable <int>, List <int> >() .BuildServiceProvider(); var resultList = service.GetService <IEnumerable <int> >(); Assert.Empty(resultList); var resultList1 = service.GetRequiredService <IEnumerable <int> >(); Assert.Empty(resultList1); Assert.Same(resultList, resultList1); var provider = service.GetRequiredService <IServiceScopeFactory>().CreateScopeProvider(); using (provider as IDisposable) { var resultList2 = provider.GetRequiredService <IEnumerable <int> >(); Assert.Empty(resultList2); Assert.NotSame(resultList, resultList2); Assert.NotSame(resultList1, resultList2); } Assert.Throws <ObjectDisposedException>(() => provider.GetRequiredService <IEnumerable <int> >()); }
public static IServiceDefintions ToServiceDefintions(this IServiceCollection services) { var defintions = new ServiceDefintions(); foreach (var service in services) { defintions.Add(service.ToServiceDefintion()); } return(defintions); }
public void WhenHasPropertyInjector() { var service = new ServiceDefintions() .AddScoped <TestPropertyInjectorClass>() .AddSingleton <ScopedTest>() .BuildServiceProvider(); var result0 = service.GetRequiredService <ScopedTest>(); var result1 = service.GetRequiredService <TestPropertyInjectorClass>(); Assert.Same(result0, result1.Scoped); }
public void WhenHasPropertyInjectorButNoService() { var service = new ServiceDefintions() .AddScoped <TestPropertyInjectorClass>() .BuildServiceProvider(); var result0 = service.GetService <ScopedTest>(); Assert.Null(result0); var result1 = service.GetRequiredService <TestPropertyInjectorClass>(); Assert.Null(result1.Scoped); }
public void SingletonWhenTypeFactory() { var service = new ServiceDefintions() .AddSingleton <SingletonTest, SingletonTest>(i => this) .BuildServiceProvider(); var result0 = service.GetService(typeof(SingletonTest)); Assert.Same(this, result0); var result1 = service.GetService(typeof(SingletonTest)); Assert.Same(this, result1); Assert.Same(result0, result1); }
public void TransientWhenServiceType() { var service = new ServiceDefintions() .AddTransient <TransientTest, TransientTest>() .BuildServiceProvider(); var result0 = service.GetService(typeof(TransientTest)); Assert.NotSame(this, result0); var result1 = service.GetService(typeof(TransientTest)); Assert.NotSame(this, result1); Assert.NotSame(result0, result1); }
public void SingletonWhenObjectAndGetIEnumerableInt() { var service = new ServiceDefintions() .AddSingleton <IEnumerable <int>, List <int> >() .BuildServiceProvider(); var resultList = (IEnumerable <int>)service.GetService(typeof(IEnumerable <int>)); Assert.Empty(resultList); var resultList1 = (IEnumerable <int>)service.GetService(typeof(IEnumerable <int>)); Assert.Empty(resultList1); Assert.Same(resultList, resultList1); }
public void SingletonWhenIEnumerableInt() { var service = new ServiceDefintions() .AddSingleton <IEnumerable <int>, List <int> >() .BuildServiceProvider(); var result0 = (List <int>)service.GetService(typeof(IEnumerable <int>)); Assert.NotNull(result0); var result1 = (List <int>)service.GetService(typeof(IEnumerable <int>)); Assert.NotNull(result1); Assert.Same(result0, result1); }
public void ScopedWhenServiceType() { var service = new ServiceDefintions() .AddScoped <ScopedTest, ScopedTest>() .BuildServiceProvider(); var result0 = service.GetService(typeof(ScopedTest)); Assert.NotSame(this, result0); var result1 = service.GetService(typeof(ScopedTest)); Assert.NotSame(this, result1); Assert.Same(result0, result1); }
public void TransientWhenFactory() { var service = new ServiceDefintions() .AddTransient(i => this) .BuildServiceProvider(); var result0 = service.GetService(typeof(TransientTest)); Assert.Same(this, result0); var result1 = service.GetService(typeof(TransientTest)); Assert.Same(this, result1); Assert.Same(result0, result1); }
public void TransientWhenTypeFactory() { var service = new ServiceDefintions() .AddTransient <NamedTransientTest, NamedTransientTest>(i => this, "T") .AddSingleton <NamedTransientTest, NamedTransientTest>(i => new NamedTransientTest()) .BuildServiceProvider(); var result0 = service.GetService(typeof(NamedTransientTest), "T"); Assert.Same(this, result0); var result1 = service.GetService(typeof(NamedTransientTest), "T"); Assert.Same(this, result1); Assert.Same(result0, result1); }
public void SingletonWhenGenericServiceTypeAndHasGenericParameters() { var service = new ServiceDefintions() .AddSingleton <SingletonTest, SingletonTest>() .AddSingleton(typeof(TestSingletonGenericClass <>)) .BuildServiceProvider(); var result0 = (TestSingletonGenericClass <SingletonTest>)service.GetService(typeof(TestSingletonGenericClass <SingletonTest>)); Assert.NotNull(result0); Assert.NotSame(this, result0.Singleton); var result1 = (TestSingletonGenericClass <SingletonTest>)service.GetService(typeof(TestSingletonGenericClass <SingletonTest>)); Assert.NotNull(result1); Assert.NotSame(this, result1.Singleton); Assert.Same(result0, result1); }
public void ScopedWhenServiceTypeAndHasParameters() { var service = new ServiceDefintions() .AddScoped <ScopedTest, ScopedTest>() .AddScoped <TestScopedClass>() .BuildServiceProvider(); var result0 = (TestScopedClass)service.GetService(typeof(TestScopedClass)); Assert.NotNull(result0); Assert.NotSame(this, result0.Scoped); var result1 = (TestScopedClass)service.GetService(typeof(TestScopedClass)); Assert.NotNull(result1); Assert.NotSame(this, result1.Scoped); Assert.Same(result0, result1); }
public void TransientWhenServiceTypeAndHasParameters() { var service = new ServiceDefintions() .AddTransient <TransientTest, TransientTest>() .AddTransient <TestTransientClass>() .BuildServiceProvider(); var result0 = (TestTransientClass)service.GetService(typeof(TestTransientClass)); Assert.NotNull(result0); Assert.NotSame(this, result0.Transient); var result1 = (TestTransientClass)service.GetService(typeof(TestTransientClass)); Assert.NotNull(result1); Assert.NotSame(this, result1.Transient); Assert.NotSame(result0, result1); }
public void TransientWhenGenericServiceTypeAndHasGenericParameters() { var service = new ServiceDefintions() .AddTransient <TransientTest, TransientTest>("T") .AddTransient(typeof(TestTransientGenericClass <>), "T") .AddSingleton <TransientTest, TransientTest>() .AddSingleton(typeof(TestTransientGenericClass <>)) .BuildServiceProvider(); var result0 = (TestTransientGenericClass <TransientTest>)service.GetService(typeof(TestTransientGenericClass <TransientTest>), "T"); Assert.NotNull(result0); Assert.NotSame(this, result0.Transient); var result1 = (TestTransientGenericClass <TransientTest>)service.GetService(typeof(TestTransientGenericClass <TransientTest>), "T"); Assert.NotNull(result1); Assert.NotSame(this, result1.Transient); Assert.NotSame(result0, result1); }
public void SingletonWhenObjectAndGetIEnumerableObject() { var service = new ServiceDefintions() .AddSingleton(this) .BuildServiceProvider(); var resultList = (IEnumerable <SingletonTest>)service.GetService(typeof(IEnumerable <SingletonTest>)); var result0 = resultList.ToArray(); Assert.Single(resultList); Assert.Same(this, result0[0]); var resultList1 = (IEnumerable <SingletonTest>)service.GetService(typeof(IEnumerable <SingletonTest>)); var result1 = resultList1.ToArray(); Assert.Single(resultList1); Assert.Same(this, result1[0]); Assert.NotSame(resultList, resultList1); Assert.Same(result0[0], result1[0]); }
public void TransientWhenObjectAndGetIEnumerableObject() { var service = new ServiceDefintions() .AddTransient(i => this, "T") .AddSingleton(i => new NamedTransientTest()) .BuildServiceProvider(); var resultList = (IEnumerable <NamedTransientTest>)service.GetService(typeof(IEnumerable <NamedTransientTest>), "T"); var result0 = resultList.ToArray(); Assert.Single(resultList); Assert.Same(this, result0[0]); var resultList1 = (IEnumerable <NamedTransientTest>)service.GetService(typeof(IEnumerable <NamedTransientTest>), "T"); var result1 = resultList1.ToArray(); Assert.Single(resultList1); Assert.Same(this, result1[0]); Assert.NotSame(resultList, resultList1); Assert.Same(result0[0], result1[0]); }
public void ScopedWhenObjectAndGetIEnumerableList() { var service = new ServiceDefintions() .AddScoped(i => this) .BuildServiceProvider(); var resultList = service.GetRequiredServices <IEnumerable <ScopedTest> >(); var result00 = resultList.ToArray(); var result0 = result00[0].ToArray(); Assert.Single(resultList); Assert.Single(result0); Assert.Same(this, result0[0]); var resultList1 = service.GetServices <IEnumerable <ScopedTest> >(); var result11 = resultList1.ToArray(); var result1 = result11[0].ToArray(); Assert.Single(resultList1); Assert.Single(result1); Assert.Same(this, result1[0]); Assert.NotSame(resultList, resultList1); Assert.Same(result0[0], result1[0]); }
public static ServiceDefintion ToServiceDefintion(this ServiceDescriptor descriptor) { return(ServiceDefintions.Define(descriptor.ServiceType, GetImplementationType(descriptor), GetLifetime(descriptor.Lifetime), GetImplementationFactory(descriptor))); }