Ejemplo n.º 1
0
        public void EfCoreRepositoryNeedsDbContextType()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                          .UseSqlite(connection)
                          .Options;

            var dbContext = new TestObjectContextCore(options);

            var config         = new SharpRepositoryConfiguration();
            var coreRepoconfig = new EfCoreRepositoryConfiguration("default", dbContext);

            config.AddRepository(coreRepoconfig);

            try
            {
                var repos = RepositoryFactory.GetInstance <Contact, string>(config);
            } catch (Exception e)
            {
                e.Message.ShouldBe("The DbContextOptions passed to the DbContext constructor must be a DbContextOptions<DbContext>. When registering multiple DbContext types make sure that the constructor for each context type has a DbContextOptions<TContext> parameter rather than a non-generic DbContextOptions parameter.");
            }
        }
Ejemplo n.º 2
0
        public void EfCoreRepositoryFromConfigurationObject()
        {
            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                          .UseSqlite(connection)
                          .Options;

            var dbContext = new TestObjectContextCore(options);

            var config         = new SharpRepositoryConfiguration();
            var coreRepoconfig = new EfCoreRepositoryConfiguration("default", dbContext);

            coreRepoconfig.Attributes.Add("dbContextType", "SharpRepository.Tests.TestObjects.TestObjectContextCore, SharpRepository.Tests");

            config.AddRepository(coreRepoconfig);

            var repos = RepositoryFactory.GetInstance <Contact, string>(config);

            if (!(repos is EfCoreRepository <Contact, string>))
            {
                throw new Exception("Not InMemoryRepository");
            }

            if (!(repos.CachingStrategy is NoCachingStrategy <Contact, string>))
            {
                throw new Exception("not NoCachingStrategy");
            }
        }
        public void EfConfigRepositoryFactory_Using_Ioc_Should_Not_Require_ConnectionString()
        {
            var config  = new EfCoreRepositoryConfiguration("TestConfig");
            var factory = new EfCoreConfigRepositoryFactory(config);

            factory.GetInstance <Contact, string>();
        }
        public void EfConfigRepositoryFactory_Using_Ioc_Should_Return_TestObjectEntites_Without_DbContextType_Defined()
        {
            var config  = new EfCoreRepositoryConfiguration("TestConfig");
            var factory = new EfCoreConfigRepositoryFactory(config);

            var repos = factory.GetInstance <Contact, string>();

            var propInfo  = repos.GetType().GetProperty("Context", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var dbContext = (TestObjectContextCore)propInfo.GetValue(repos, null);

            dbContext.ShouldBeOfType <TestObjectContextCore>();
        }
        public void EfConfigRepositoryFactory_Using_Ioc_Should_Share_DbContext()
        {
            var config  = new EfCoreRepositoryConfiguration("TestConfig", "tmp");
            var factory = new EfCoreConfigRepositoryFactory(config);

            var repos1 = factory.GetInstance <Contact, string>();
            var repos2 = factory.GetInstance <Contact, string>();

            // use reflecton to get the protected Context property
            var propInfo1  = repos1.GetType().GetProperty("Context", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var dbContext1 = (TestObjectContextCore)propInfo1.GetValue(repos1, null);
            var propInfo2  = repos2.GetType().GetProperty("Context", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public);
            var dbContext2 = (TestObjectContextCore)propInfo2.GetValue(repos2, null);

            dbContext1.ShouldBe(dbContext2);
        }
Ejemplo n.º 6
0
        public void LoadEfRepositoryAndCachingFromConfigurationObject()
        {
            var config = new SharpRepositoryConfiguration();

            var connection = new SqliteConnection("DataSource=:memory:");

            connection.Open();

            var options = new DbContextOptionsBuilder <TestObjectContextCore>()
                          .UseSqlite(connection)
                          .Options;

            var dbContext = new TestObjectContextCore(options);

            config.AddRepository(new InMemoryRepositoryConfiguration("inMemory", "timeout"));

            var coreRepoconfig = new EfCoreRepositoryConfiguration("efCore", dbContext, "standard", "inMemoryProvider");

            coreRepoconfig.Attributes.Add("dbContextType", "SharpRepository.Tests.TestObjects.TestObjectContextCore, SharpRepository.Tests");

            config.AddRepository(coreRepoconfig);
            config.DefaultRepository = "efCore";

            config.AddCachingStrategy(new StandardCachingStrategyConfiguration("standard"));
            config.AddCachingStrategy(new TimeoutCachingStrategyConfiguration("timeout", 200));
            config.AddCachingStrategy(new NoCachingStrategyConfiguration("none"));

            config.AddCachingProvider(new InMemoryCachingProviderConfiguration("inMemoryProvider", new MemoryCache(new MemoryCacheOptions())));

            var repos = RepositoryFactory.GetInstance <Contact, string>(config);

            if (!(repos is EfCoreRepository <Contact, string>))
            {
                throw new Exception("Not InMemoryRepository");
            }

            if (!(repos.CachingStrategy is StandardCachingStrategy <Contact, string>))
            {
                throw new Exception("not StandardCachingStrategy");
            }
        }