public LazyListFactoryTests() { _lazyLoadResolverMock = new Mock <ILazyLoadResolver <IEnumerable <Stub> > >(); _disposableScopeMock = new Mock <IDisposable>(); _serviceProviderMock = new Mock <IServiceProvider>(); _lazyListFactory = new LazyListFactory(_disposableScopeMock.Object, _serviceProviderMock.Object); }
public void GivenServiceCollectionWhenAddLazyListShouldInitializeFactory() { var services = new ServiceCollection(); services.AddLazyList(typeof(StubLazyLoadResolver <>).Assembly); Func <IList <Stub> > action = () => LazyListFactory.CreateList <Stub>(1); action.Should().NotThrow(); }
public void GivenLazyListFactoryWhenStaticCreateShouldReturnInstance() { _serviceProviderMock.Setup(x => x.GetService(typeof(ILazyLoadResolver <IEnumerable <Stub> >))) .Returns(_lazyLoadResolverMock.Object); LazyListFactory.RegisterInstance(_lazyListFactory); var list = LazyListFactory.CreateList <Stub>(1); list.Should().NotBeNull(); list.Should().BeOfType <LazyList <Stub> >(); _serviceProviderMock.VerifyAll(); }
public void GivenServiceCollectionWhenAddLazyListShouldRegisterOpenTypeGenericsAndSpecificTypes() { var services = new ServiceCollection(); services.AddLazyList(typeof(StubLazyLoadResolver <>).Assembly); var stubs = LazyListFactory.CreateList <Stub>(1); stubs.Should().NotBeNull(); stubs.Should().BeOfType <LazyList <Stub> >(); }
public static void AddLazyList(this IServiceCollection services, params Assembly[] assemblies) { var resolvers = assemblies .SelectMany(x => x.DefinedTypes) .Where(x => x.IsClass && !x.IsAbstract && x.GetInterfaces().Any(y => y.IsGenericType && y.GetGenericTypeDefinition() == typeof(ILazyLoadResolver <>))) .ToList(); foreach (var resolver in resolvers) { if (resolver.GenericTypeParameters.Any()) { services.AddScoped(typeof(ILazyLoadResolver <>), resolver); } else { var interfaces = resolver.GetInterfaces().Where(x => x.GetGenericTypeDefinition() == typeof(ILazyLoadResolver <>)) .ToList(); foreach (var interfaceType in interfaces) { services.AddScoped(interfaceType, resolver); } } } services.AddSingleton <ILazyListFactory>(provider => { var scope = provider.CreateScope(); return(new LazyListFactory(scope, scope.ServiceProvider)); }); var factory = services.BuildServiceProvider().GetService <ILazyListFactory>(); LazyListFactory.RegisterInstance(factory); }
public void Dispose() { LazyListFactory.RegisterInstance(null); _lazyListFactory.Dispose(); _disposableScopeMock.Verify(x => x.Dispose(), Times.Once()); }
public void GivenLazyListFactoryWhenStaticCreateAndNotInitShouldThrow() { Func <IList <Stub> > action = () => LazyListFactory.CreateList <Stub>(1); action.Should().Throw <InvalidOperationException>(); }
public void Dispose() { LazyListFactory.RegisterInstance(null); }