Beispiel #1
0
        public void CreateChildScope_CustomScopeRegistration_TypeRegistration_AsExpected()
        {
            Bootstrapp(new ServiceCollection());

            using (var s = DIManager.BeginScope())
            {
                var i = s.Resolve <IScopeTest>();
                i.Should().BeNull();
                using (var sChild = s.CreateChildScope())
                {
                    i = sChild.Resolve <IScopeTest>();
                    i.Should().BeNull();
                }
                using (var sChild = s.CreateChildScope(e => e.RegisterType <ScopeTest>()))
                {
                    i = sChild.Resolve <IScopeTest>();
                    i.Should().NotBeNull();
                    i.Data.Should().Be("ctor");
                }
            }
        }
Beispiel #2
0
        public void AutofacScope_CreateChildScope_CustomScopeRegistration_InstanceRegistration_AsExpected()
        {
            Bootstrapp(new ContainerBuilder());

            using (var s = DIManager.BeginScope())
            {
                var i = s.Resolve <IScopeTest>();
                i.Should().BeNull();
                using (var sChild = s.CreateChildScope())
                {
                    i = sChild.Resolve <IScopeTest>();
                    i.Should().BeNull();
                }
                using (var sChild = s.CreateChildScope(e => e.Register(new ScopeTest("instance"))))
                {
                    i = sChild.Resolve <IScopeTest>();
                    i.Should().NotBeNull();
                    i.Data.Should().Be("instance");
                }
            }
        }
        public void UseRabbitMQServer_WithIoC_Should_Add_RabbitMQClient_Within_Registrations()
        {
            new Bootstrapper()
            .UseAutofacAsIoC(c => { })
            .UseRabbitMQServer(new Server.RabbitMQServerConfiguration("test", "localhost", "guest", "guest", QueueConfiguration.Empty))
            .Bootstrapp();

            using (var scope = DIManager.BeginScope())
            {
                var client = scope.Resolve <RabbitMQClient>();
                client.Should().NotBeNull();
                using (var connection = client.GetConnection())
                {
                    connection.IsOpen.Should().BeTrue();
                }
            }

            using (var connection = RabbitMQClient.Instance.GetConnection())
            {
                connection.IsOpen.Should().BeTrue();
            }
        }
Beispiel #4
0
        public void UseEFCoreAsMainRepository_With_DbContext_Instance_Should_Register_DbContext_In_IoC()
        {
            try
            {
                new Bootstrapper()
                .UseAutofacAsIoC(c => { })
                .UseEFCoreAsMainRepository(new TestDbContext())
                .Bootstrapp();

                using (var scope = DIManager.BeginScope())
                {
                    scope.Resolve <TestDbContext>().Should().NotBeNull();
                    scope.Resolve <EFRepository <WebSite> >().Should().NotBeNull();
                    scope.Resolve <IDataReaderRepository <WebSite> >().Should().NotBeNull();
                    scope.Resolve <IDatabaseRepository <WebSite> >().Should().NotBeNull();
                    scope.Resolve <IDataUpdateRepository <WebSite> >().Should().NotBeNull();
                }
            }
            finally
            {
                DisableIoC();
            }
        }
Beispiel #5
0
        public async Task UseEFCoreAsMainRepository_Options_Should_BeTaken_Into_Account()
        {
            try
            {
                new Bootstrapper()
                .UseAutofacAsIoC()
                .UseEFCoreAsMainRepository(opt => opt.UseSqlite($"Filename={DbName}"), new EFCoreOptions
                {
                    DisableLogicalDeletion = true
                })
                .Bootstrapp();


                using (var scope = DIManager.BeginScope())
                {
                    var repo = scope.Resolve <EFRepository <WebSite> >();

                    repo.MarkForInsert(new WebSite
                    {
                        Url = "https://blogs.msdn.net"
                    });

                    await repo.SaveAsync();
                }

                using (var scope = DIManager.BeginScope())
                {
                    var repo = scope.Resolve <EFRepository <WebSite> >();
                    var ws   = await repo.GetAsync().FirstOrDefaultAsync();

                    repo.MarkForDelete(ws, false); //Force it just to be sure
                    await repo.SaveAsync();
                }

                using (var scope = DIManager.BeginScope())
                {
                    var ctx = scope.Resolve <TestDbContext>();
                    ctx.Set <WebSite>().Count().Should().Be(0);
                }

                DeleteAll();

                using (var scope = DIManager.BeginScope())
                {
                    var repo = scope.Resolve <RepositoryBase>();

                    repo.MarkForInsert(new WebSite
                    {
                        Url = "https://blogs.msdn.net"
                    });

                    await repo.SaveAsync();
                }

                using (var scope = DIManager.BeginScope())
                {
                    var repo = scope.Resolve <RepositoryBase>();
                    var ws   = await repo.GetAsync <WebSite>().FirstOrDefaultAsync();

                    repo.MarkForDelete(ws, false); //Force it just to be sure
                    await repo.SaveAsync();
                }

                using (var scope = DIManager.BeginScope())
                {
                    var ctx = scope.Resolve <TestDbContext>();
                    ctx.Set <WebSite>().Count().Should().Be(0);
                }
            }
            finally
            {
                DisableIoC();
                if (File.Exists(DbName))
                {
                    File.Delete(DbName);
                }
            }
        }
Beispiel #6
0
        public IScopeFactory CreateBuilder(IServiceCollection services)
        {
            bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceProvider>(true));
            bootstrapper.AddIoCRegistration(new TypeRegistration <CQELightServiceScopeFactory>(true));

            foreach (var item in services)
            {
                if (item.ServiceType != null)
                {
                    if (item.ImplementationType != null)
                    {
                        bootstrapper.AddIoCRegistration(new TypeRegistration(item.ImplementationType,
                                                                             item.Lifetime == ServiceLifetime.Singleton ? RegistrationLifetime.Singleton : RegistrationLifetime.Transient,
                                                                             TypeResolutionMode.OnlyUsePublicCtors, item.ServiceType));
                    }
                    else if (item.ImplementationFactory != null)
                    {
                        bootstrapper.AddIoCRegistration(new FactoryRegistration(() => item.ImplementationFactory(
                                                                                    new CQELightServiceProvider(DIManager.BeginScope().Resolve <IScopeFactory>())), item.ServiceType));
                    }
                    else if (item.ImplementationInstance != null)
                    {
                        bootstrapper.AddIoCRegistration(new InstanceTypeRegistration(item.ImplementationInstance, item.ServiceType));
                    }
                }
            }
            bootstrapper.Bootstrapp();
            return(DIManager._scopeFactory);
        }