Exemplo n.º 1
0
        public void DeleteById(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var entity = fixture.Create <TestEntity>();

            using (var context = contextFactory.CreateDbContext(null))
            {
                context.Entities.Add(entity);
                context.SaveChanges();
            }

            // Act
            int deleteCount;

            using (var context = contextFactory.CreateDbContext(null))
            {
                deleteCount = builder
                              .CreateCommand <TestEntity>()
                              .Delete <DeleteParams>((e, p) => e.Id == p.Id)
                              .Log(context, output)
                              .Create(context)
                              .Execute(context.Database, new DeleteParams {
                    Id = entity.Id
                });
            }

            // Assert
            Assert.Equal(1, deleteCount);
            using (var context = contextFactory.CreateDbContext(null))
            {
                var exists = context.Entities.Any(e => e.Name == entity.Name);
                Assert.False(exists);
            }
        }
Exemplo n.º 2
0
        public CommentRepository(IDesignTimeDbContextFactory <Context> contextFactory)
        {
            EntityFactory <CommentEntity>            factory  = new EntityFactory <CommentEntity>(() => new CommentEntity());
            Func <DbContext, DbSet <CommentEntity> > getDbSet = c => ((Context)c).Comments;

            repo = new EntityRepository <Comment, CommentEntity>(factory, getDbSet, contextFactory);
        }
        //TODO: Move this to Program.cs, only when app launches/re-launches
        public static async void UseEnsureMigrations(this IApplicationBuilder builder)
        {
            using var serviceScope = builder.ApplicationServices.GetService <IServiceScopeFactory>().CreateScope();
            var tenants = await serviceScope.ServiceProvider.GetService <ITenantProvider>().GetTenants();

            if (tenants.Count == 0)
            {
                return;
            }

            IDesignTimeDbContextFactory <DbContext> dbContextFactory = (IDesignTimeDbContextFactory <DbContext>)
                                                                       serviceScope.ServiceProvider.GetService(typeof(IDesignTimeDbContextFactory <DbContext>));

            var dummyDbContext = dbContextFactory.CreateDbContext(null);
            var dbUpdateExist  = dummyDbContext.Database.GetPendingMigrations().Any();

            if (dbUpdateExist)
            {
                foreach (var tenant in tenants)
                {
                    var context = dbContextFactory.CreateDbContext(new string[] { tenant.Id.ToString(), tenant.ConnectionString });
                    if (context != null)
                    {
                        await context.Database.MigrateAsync();
                    }
                }

                PushLatestScriptToStorage(dummyDbContext.Database.GenerateCreateScript());
            }
        }
Exemplo n.º 4
0
        public void InsertOutput(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();

            // Act
            InsertResult insertResult;

            using (var context = contextFactory.CreateDbContext(null))
            {
                insertResult = builder
                               .CreateCommand <TestEntity>()
                               .Insert <UpsertParams>(p => new TestEntity {
                    Name = p.Name, Version = p.Version
                })
                               .Output(e => new InsertResult {
                    Id = e.Id, Version = e.Version
                })
                               .Log(context, output)
                               .Create(context)
                               .QueryFirst(context.Database, upsertParams);
            }

            // Assert
            Assert.NotNull(insertResult);
            Assert.Equal(upsertParams.Version, insertResult.Version);

            using (var context = contextFactory.CreateDbContext(null))
            {
                var inserted = context.Entities.Single(e => e.Id == insertResult.Id);
                Assert.Equal(upsertParams.Name, inserted.Name);
                Assert.Equal(upsertParams.Version, inserted.Version);
            }
        }
Exemplo n.º 5
0
        public void Insert(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();

            // Act
            int insertCount;

            using (var context = contextFactory.CreateDbContext(null))
            {
                output.WriteLine($"####### {context.Database.ProviderName}");
                insertCount = builder
                              .CreateCommand <TestEntity>()
                              .Insert <UpsertParams>(p => new TestEntity {
                    Name = p.Name, Version = p.Version
                })
                              .Log(context, output)
                              .Create(context)
                              .Execute(context.Database, upsertParams);
            }

            // Assert
            Assert.Equal(1, insertCount);
            using (var context = contextFactory.CreateDbContext(null))
            {
                var exists = context.Entities.Any(e => e.Name == upsertParams.Name);
                Assert.True(exists);
            }
        }
Exemplo n.º 6
0
        public TeamRepository(IDesignTimeDbContextFactory <Context> contextFactory)
        {
            EntityFactory <TeamEntity>            Entityfactory = CreateEntityFactory();
            Func <DbContext, DbSet <TeamEntity> > dbSet         = CreateFunctionThatReturnsEntityDBSetFromContext();

            repo = new EntityRepository <Team, TeamEntity>(Entityfactory, dbSet, contextFactory);
        }
Exemplo n.º 7
0
        public UserRepository(IDesignTimeDbContextFactory <Context> contextFactory)
        {
            EntityFactory <UserEntity>            factory = CreateEntityFactory();
            Func <DbContext, DbSet <UserEntity> > dbSet   = CreateFunctionThatReturnsEntityDBSetFromContext();

            repo = new EntityRepository <User, UserEntity>(factory, dbSet, contextFactory);
        }
Exemplo n.º 8
0
 public EntityRepository(EntityFactory <Entity> factory, Func <DbContext, DbSet <Entity> > getDBSetFunc, IDesignTimeDbContextFactory <DbContext> contextFactory)
 {
     this.factory        = factory;
     this.getDBSetFunc   = getDBSetFunc;
     this.contextFactory = contextFactory;
     entityUpdater       = new EntityUpdater <Entity>(contextFactory);
 }
Exemplo n.º 9
0
        public LogRepository(IDesignTimeDbContextFactory <Context> contextFactory)
        {
            Func <LogEntity>                     createLog  = () => new LogEntity();
            EntityFactory <LogEntity>            logFactory = new EntityFactory <LogEntity>(createLog);
            Func <DbContext, DbSet <LogEntity> > getDbSet   = context => ((Context)context).Logs;

            repo = new EntityRepository <LogDTO, LogEntity>(logFactory, getDbSet, contextFactory);
        }
Exemplo n.º 10
0
        public Repository(IDesignTimeDbContextFactory <ProMassSpammerModel> modelFactory)
        {
            _massCommunicationRepository = new Lazy <IMassCommunicationRepository>(() => new MassCommunicationRepository(modelFactory));

            _logRepository = new Lazy <ILogRepository>(() => new LogRepository(modelFactory));

            _recipientRepository = new Lazy <IRecipientRepository>(() => new RecipientRepository(modelFactory));
        }
Exemplo n.º 11
0
 public InstrumentService(IDesignTimeDbContextFactory <LInstContext> dbContextFactory,
                          IEventAggregator aggregator,
                          IDataService <LInstContext> lInstData)
 {
     _eventAggregator  = aggregator;
     _dbContextFactory = dbContextFactory;
     _lInstData        = lInstData;
 }
Exemplo n.º 12
0
 public UnitOfWork(IDesignTimeDbContextFactory <DatabaseContext> databaseContext)
 {
     _context       = databaseContext.CreateDbContext(args: null);
     Posts          = new PostRepository(_context);
     Categories     = new CategoryRepository(_context);
     Accounts       = new AccountRepository(_context);
     PostCategories = new PostCategoryRepository(_context);
 }
Exemplo n.º 13
0
        public void InsertOrUpdate_UpsertExistingOutput(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();
            int existingId;

            using (var context = contextFactory.CreateDbContext(null))
            {
                var entity = new TestEntity
                {
                    Name    = upsertParams.Name,
                    Version = upsertParams.Version,
                };
                context.Entities.Add(entity);
                context.SaveChanges();

                existingId = entity.Id;
            }

            // Act
            UpsertParams result;

            using (var context = contextFactory.CreateDbContext(null))
            {
                result = builder
                         .CreateCommand <TestEntity>()
                         .Insert <UpsertParams>(p => new TestEntity {
                    Name = p.Name, Version = p.Version
                })
                         .OrUpdate(
                    e => new { e.Name },
                    (e, p) => new TestEntity
                {
                    Version = e.Version + 1,
                }
                    )
                         .Output(e => new UpsertParams
                {
                    Id      = e.Id,
                    Version = e.Version,
                })
                         .Log(context, output)
                         .Create(context)
                         .QueryFirstOrDefault(context.Database, upsertParams);
            }

            // Assert
            Assert.NotNull(result);

            using (var context = contextFactory.CreateDbContext(null))
            {
                var inserted = context.Entities.Single(e => e.Name == upsertParams.Name);
                Assert.Equal(existingId, inserted.Id);
                Assert.Equal(upsertParams.Version + 1, inserted.Version);
                Assert.Equal(inserted.Id, result.Id);
                Assert.Equal(inserted.Version, result.Version);
            }
        }
Exemplo n.º 14
0
 public PluginConfigurationSource(
     string connectionString,
     IPluginConfigurationSeedData pluginConfigurationSeedData,
     IDesignTimeDbContextFactory <TDbContext> dbContextFactory)
 {
     _connectionString            = connectionString;
     _pluginConfigurationSeedData = pluginConfigurationSeedData;
     _dbContextFactory            = dbContextFactory;
 }
Exemplo n.º 15
0
 public HistorianRepository(
     ILoggerFactory loggerFactory,
     IDesignTimeDbContextFactory <HistorianDbContext> contextFactory,
     IStorageTransactionFactory <HistorianDbContext> storageTransactionFactory)
 {
     LoggerFactory             = loggerFactory;
     ContextFactory            = contextFactory;
     StorageTransactionFactory = storageTransactionFactory;
 }
Exemplo n.º 16
0
 public PluginConfigurationProvider(
     string connectionString,
     IPluginConfigurationSeedData pluginConfigurationSeedData,
     IDesignTimeDbContextFactory <TDbContext> dbContextFactory)
 {
     _connectionString            = connectionString;
     _pluginConfigurationSeedData = pluginConfigurationSeedData;
     _dbContextFactory            = dbContextFactory;
     ReloadDbConfigurationDelegates.ReloadDbConfigurationDelegate += ReloadPluginConfiguration;
 }
Exemplo n.º 17
0
 public void TestInit()
 {
     contextFactory = GetContextFactory();
     repo           = new SportRepository(contextFactory);
     boca           = CreateBocaTeam();
     river          = CreateTeamThatBelongsInTheB();
     futbol         = CreateFutbolTeam();
     rugby          = CreateRugbyTeam();
     repo.Add(futbol);
     repo.Add(rugby);
 }
Exemplo n.º 18
0
 public static IConfigurationBuilder AddPluginConfiguration <TDbContext>(
     this IConfigurationBuilder builder,
     string connectionString,
     IPluginConfigurationSeedData pluginConfigurationSeedData,
     IDesignTimeDbContextFactory <TDbContext> dbContextFactory) where TDbContext : DbContext, IPluginDbContext
 {
     return(builder.Add(new PluginConfigurationSource <TDbContext>(
                            connectionString,
                            pluginConfigurationSeedData,
                            dbContextFactory)));
 }
Exemplo n.º 19
0
        public MarketRepository(
            ILoggerFactory loggerFactory,
            IIntervalFactory intervalFactory,
            IDesignTimeDbContextFactory <HistorianDbContext> contextFactory,
            IStorageTransactionFactory <HistorianDbContext> storageTransactionFactory)
        {
            IntervalFactory           = intervalFactory;
            ContextFactory            = contextFactory;
            StorageTransactionFactory = storageTransactionFactory;

            Logger = loggerFactory.CreateLogger <MarketRepository>();
        }
Exemplo n.º 20
0
        public void InsertOrUpdate_UpsertExistingWhere(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();
            int existingId;

            using (var context = contextFactory.CreateDbContext(null))
            {
                var entity = new TestEntity
                {
                    Name    = upsertParams.Name,
                    Version = upsertParams.Version,
                };
                context.Entities.Add(entity);
                context.SaveChanges();

                existingId = entity.Id;
            }

            // Act
            int rowCount;

            using (var context = contextFactory.CreateDbContext(null))
            {
                rowCount = builder
                           .CreateCommand <TestEntity>()
                           .Insert <UpsertParams>(p => new TestEntity {
                    Name = p.Name, Version = p.Version
                })
                           .OrUpdate(
                    e => new { e.Name },
                    (e, p) => new TestEntity
                {
                    Version = e.Version + 1,
                }
                    )
                           .Where((e, p) => e.Version < 0)
                           .Log(context, output)
                           .Create(context)
                           .Execute(context.Database, upsertParams);
            }

            // Assert
            Assert.Equal(0, rowCount);

            using (var context = contextFactory.CreateDbContext(null))
            {
                var existing = context.Entities.Single(e => e.Id == existingId);
                Assert.Equal(upsertParams.Version, existing.Version);
            }
        }
Exemplo n.º 21
0
 public void TestInit()
 {
     contextFactory = GetContextFactory();
     sportRepo      = new SportRepository(contextFactory);
     teamRepo       = new TeamRepository(contextFactory);
     futbol         = CreateFutbol();
     rugby          = CreateRugby();
     boca           = CreateBocaTeam();
     river          = CreateTeamThatBelongsInTheB();
     sportRepo.Add(new Sport("Futbol"));
     teamRepo.Add(new Team(boca.Name, new Sport("Futbol")));
     teamRepo.Add(new Team(river.Name, new Sport("Futbol")));
     login = CreateLoginServices();
 }
Exemplo n.º 22
0
 protected void Initialize()
 {
     football       = CreateFootballSport();
     boca           = CreateBocaTeam();
     river          = CreateRiverTeam();
     tomba          = CreateTombaTeam();
     messi          = CreateMessiUser();
     maradona       = CreateMaradonaUser();
     bocaRiver      = CreateRiverBocaEncounter();
     riverTomba     = CreateRiverTombaEncounter();
     contextFactory = new InMemoryContextFactory();
     encounterRepo  = CreateEncounterRepo();
     teamRepo       = CreateTeamRepo();
 }
Exemplo n.º 23
0
        public static IDataProcessor GetDataProcessor(
            IDesignTimeDbContextFactory <RiversECO.DataContext.DataContext> contextFactory,
            WaterObjectType objectType)
        {
            switch (objectType)
            {
            case WaterObjectType.River:
                return(new RiversDataProcessor(contextFactory));

            case WaterObjectType.Lake:
                return(new LakesDataProcessor(contextFactory));

            default:
                return(null);
            }
        }
Exemplo n.º 24
0
        public void InsertOrUpdate_UpdateExistingWhereOutput(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();

            using (var context = contextFactory.CreateDbContext(null))
            {
                context.Entities.Add(new TestEntity
                {
                    Name    = upsertParams.Name,
                    Version = upsertParams.Version,
                });
                context.SaveChanges();
            }

            // Act
            IEnumerable <UpsertParams> results;

            using (var context = contextFactory.CreateDbContext(null))
            {
                results = builder
                          .CreateCommand <TestEntity>()
                          .Update <UpsertParams>((e, p) => new TestEntity
                {
                    Version = e.Version + 1,
                })
                          .Where((e, p) => e.Name == p.Name)
                          .Output(e => new UpsertParams {
                    Id = e.Id, Version = e.Version
                })
                          .Log(context, output)
                          .Create(context)
                          .Query(context.Database, upsertParams);
            }

            // Assert
            Assert.Single(results);

            using (var context = contextFactory.CreateDbContext(null))
            {
                var inserted = context.Entities.Single(e => e.Name == upsertParams.Name);
                Assert.Equal(upsertParams.Version + 1, inserted.Version);

                var result = results.Single();
                Assert.Equal(upsertParams.Version + 1, result.Version);
            }
        }
Exemplo n.º 25
0
        private static void Initialize()
        {
            Console.WriteLine("Initializing...");
            var options = DataContextFactory.GetConfiguration();

            switch (options.Provider)
            {
            case DatabaseProviders.MySql:
                _factory = new MySqlDataContextFactory();
                break;

            case DatabaseProviders.MsSql:
                _factory = new MsSqlDataContextFactory();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 26
0
        public void InsertOrUpdate_UpdateExistingWhere(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();

            using (var context = contextFactory.CreateDbContext(null))
            {
                context.Entities.Add(new TestEntity
                {
                    Name    = upsertParams.Name,
                    Version = upsertParams.Version,
                });
                context.SaveChanges();
            }

            // Act
            int updateCount;

            using (var context = contextFactory.CreateDbContext(null))
            {
                updateCount = builder
                              .CreateCommand <TestEntity>()
                              .Update <UpsertParams>((e, p) => new TestEntity
                {
                    Version = e.Version + 1,
                })
                              .Where((e, p) => e.Name == p.Name)
                              .Log(context, output)
                              .Create(context)
                              .Execute(context.Database, upsertParams);
            }

            // Assert
            Assert.Equal(1, updateCount);

            using (var context = contextFactory.CreateDbContext(null))
            {
                var inserted = context.Entities.Single(e => e.Name == upsertParams.Name);
                Assert.Equal(upsertParams.Version + 1, inserted.Version);
            }
        }
Exemplo n.º 27
0
        public void InsertOrUpdate_UpdateWithNullable(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();

            using (var context = contextFactory.CreateDbContext(null))
            {
                context.Entities.Add(new TestEntity
                {
                    Name    = upsertParams.Name,
                    Version = upsertParams.Version,
                });
                context.SaveChanges();
            }

            upsertParams.NullableVersion = null;

            // Act
            int rowCount;

            using (var context = contextFactory.CreateDbContext(null))
            {
                rowCount = builder
                           .CreateCommand <TestEntity>()
                           .Update <UpsertParams>((e, p) => new TestEntity
                {
                    Version = 1,
                })
                           .Where((e, p) => e.Version == p.NullableVersion || p.NullableVersion == null)
                           .Log(context, output)
                           .Create(context)
                           .Execute(context.Database, upsertParams);
            }

            // Assert
            Assert.NotEqual(0, rowCount);
        }
Exemplo n.º 28
0
        public void InsertOrUpdate_UpsertNew(IDesignTimeDbContextFactory <TestDbContext> contextFactory)
        {
            // Arrange
            var upsertParams = fixture.Create <UpsertParams>();

            // Act
            int rowCount;

            using (var context = contextFactory.CreateDbContext(null))
            {
                rowCount = builder
                           .CreateCommand <TestEntity>()
                           .Insert <UpsertParams>(p => new TestEntity {
                    Name = p.Name, Version = p.Version
                })
                           .OrUpdate(
                    e => new { e.Name },
                    (e, p) => new TestEntity
                {
                    Version = e.Version + 1,
                }
                    )
                           .Log(context, output)
                           .Create(context)
                           .Execute(context.Database, upsertParams);
            }

            // Assert
            Assert.Equal(1, rowCount);

            using (var context = contextFactory.CreateDbContext(null))
            {
                var inserted = context.Entities.Single(e => e.Name == upsertParams.Name);
                Assert.Equal(upsertParams.Version, inserted.Version);
            }
        }
Exemplo n.º 29
0
        /// <summary>
        /// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        /// </summary>
        /// <param name="app">Injected <see cref="IApplicationBuilder"/></param>
        /// <param name="env">Injected <see cref="IHostingEnvironment"/></param>
        /// <param name="contextFactory">The application <see cref="IDesignTimeDbContextFactory{TContext}"/></param>
        public void Configure(
            IApplicationBuilder app,
            IHostingEnvironment env,
            IDesignTimeDbContextFactory <PrioritizeMeDbContext> contextFactory)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();

                // Automatically migrate the application to the latest
                using (PrioritizeMeDbContext migrationContext = contextFactory.CreateDbContext(new string[0]))
                {
                    migrationContext.Database.Migrate();
                }
            }

            app.UseCors(MyAllowSpecificOrigins);

            app.UseHttpsRedirection();
            app.UseMvc();

            app.UseSwagger(c =>
            {
                c.PreSerializeFilters.Add((swagger, httpReq) => swagger.Host = httpReq.Host.Value);
            });

            app.UseSwaggerUI(
                c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
            });
        }
Exemplo n.º 30
0
 public KmStandViewRepository(IDesignTimeDbContextFactory <ReadModelDbContext> contextFactory)
 {
     _contextFactory = contextFactory;
     _context        = contextFactory.CreateDbContext(Array.Empty <string>());
 }