예제 #1
0
 public LazyListFactoryTests()
 {
     _lazyLoadResolverMock = new Mock <ILazyLoadResolver <IEnumerable <Stub> > >();
     _disposableScopeMock  = new Mock <IDisposable>();
     _serviceProviderMock  = new Mock <IServiceProvider>();
     _lazyListFactory      = new LazyListFactory(_disposableScopeMock.Object, _serviceProviderMock.Object);
 }
예제 #2
0
        public void GivenServiceCollectionWhenAddLazyListShouldInitializeFactory()
        {
            var services = new ServiceCollection();

            services.AddLazyList(typeof(StubLazyLoadResolver <>).Assembly);

            Func <IList <Stub> > action = () => LazyListFactory.CreateList <Stub>(1);

            action.Should().NotThrow();
        }
예제 #3
0
        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();
        }
예제 #4
0
        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> >();
        }
예제 #5
0
        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);
        }
예제 #6
0
 public void Dispose()
 {
     LazyListFactory.RegisterInstance(null);
     _lazyListFactory.Dispose();
     _disposableScopeMock.Verify(x => x.Dispose(), Times.Once());
 }
예제 #7
0
        public void GivenLazyListFactoryWhenStaticCreateAndNotInitShouldThrow()
        {
            Func <IList <Stub> > action = () => LazyListFactory.CreateList <Stub>(1);

            action.Should().Throw <InvalidOperationException>();
        }
예제 #8
0
 public void Dispose()
 {
     LazyListFactory.RegisterInstance(null);
 }