Beispiel #1
0
        /// <summary>
        /// Enqueue ab idle connection
        /// </summary>
        /// <param name="dbContext">DB context</param>
        /// <returns>True(Enqueue successfully)/False(Exceeds the max connection and the connection will be disposed)</returns>
        public static bool Enqueue(PgDbContext dbContext)
        {
            lock (enqueueLock)
            {
                if (dbContextQueue.Count < MaxConnections)
                {
                    dbContextQueue.Enqueue(dbContext);
                    Debug.WriteLine($"[DbContextFactory] Enqueue succeed, current stored connections: {dbContextQueue.Count}");
                    return(true);
                }
                else
                {
                    Debug.WriteLine($"[DbContextFactory] Enqueue failed, current stored connections: {dbContextQueue.Count}");

                    if (dbContext != null)
                    {
                        dbContext.Database.CloseConnection();
                        dbContext.Dispose();
                        dbContext = null;
                    }

                    return(false);
                }
            }
        }
Beispiel #2
0
        /// <summary>
        /// Dequeue a preserved connection or create a new one
        /// </summary>
        /// <param name="db">Database</param>
        /// <returns>DB context</returns>
        public static PgDbContext Dequeue(string dbName)
        {
            if (string.IsNullOrEmpty(dbName))
            {
                throw new ArgumentNullException("dbName");
            }

            if (ConnectionStrings == null || !ConnectionStrings.ContainsKey(dbName))
            {
                // Warning log
                //Debug.WriteLine($"{dbName} does not have the connection string in {nameof(DbContextFactory)}");
                return(null);
            }

            if (dbContextQueue.TryDequeue(out PgDbContext output))
            {
                Debug.WriteLine($"[DbContextFactory] Dequeue succeed, current stored connections: {dbContextQueue.Count}");
                return(output);
            }
            else
            {
                Debug.WriteLine($"[DbContextFactory] Dequeue failed, current stored connections: {dbContextQueue.Count}");

                // Create new DB context
                var connStr        = ConnectionStrings[dbName];
                var optionsBuilder = new DbContextOptionsBuilder <PgDbContext>();
                optionsBuilder.UseNpgsql(connStr);
                var newDbContext = new PgDbContext(optionsBuilder.Options);
                // newDbContext.ChangeTracker.AutoDetectChangesEnabled = false; // Optional

                return(newDbContext);
            }
        }
Beispiel #3
0
 public async Task <ApiUserInfo> GetUser([FromRoute] string name)
 {
     return(this.dbcontext.Set <User>().Include(u => u.Metadata).Where(x => x.Name.Equals(name)).Select(x =>
                                                                                                        new ApiUserInfo
     {
         Name = x.Name,
         Phone = x.Phone,
         CardNo = x.CardNo,
         Secret = PgDbContext.DbSymDecrypt(x.Secret),
         Metadata = x.Metadata
     }).FirstOrDefault());
 }
Beispiel #4
0
        public async Task <IActionResult> AddUser([FromBody] User user)
        {
            await this.dbcontext.Set <User>().AddAsync(this.dbcontext.Set <User>().Select(x => new User
            {
                Name     = user.Name,
                Password = user.Password,
                Phone    = user.Phone,
                CardNo   = user.CardNo,
                Secret   = PgDbContext.DbSymEncrypt("some secret"),
                Metadata = new SysMetadata
                {
                    CreateBy = "Admin",
                    CreateOn = DateTimeOffset.UtcNow
                }
            }).First());

            await this.dbcontext.SaveChangesAsync();

            return(this.Ok());
        }
Beispiel #5
0
 public AccountRepository(PgDbContext dbContext)
     : base(dbContext)
 {
 }
Beispiel #6
0
 public SubscriberService(PgDbContext pgDbContext, ICapPublisher capPublisher)
 {
     _pgDbContext  = pgDbContext;
     _capPublisher = capPublisher;
 }
Beispiel #7
0
 public CharacterRepository(PgDbContext dbContext)
     : base(dbContext)
 {
 }
Beispiel #8
0
 public PublishController(ICapPublisher capPublisher, PgDbContext pgDbContext)
 {
     _capPublisher = capPublisher;
     _pgDbContext  = pgDbContext;
 }
 public CategoryRepository(PgDbContext dbContext)
 {
     _dbContext = dbContext;
 }
Beispiel #10
0
        public async Task <IActionResult> SignIn([FromBody] ApiUser user)
        {
            var matchedUser =
                this.dbcontext.Set <User>().FirstOrDefault(x => x.Name.Equals(user.Name) && PgDbContext.DbHashMatch(user.Password, x.Password));

            if (matchedUser != null)
            {
                return(this.Ok());
            }
            else
            {
                return(this.BadRequest());
            }
        }
Beispiel #11
0
 public PermissionsRepository(PgDbContext dbContext)
 {
     this.dbContext = dbContext;
 }
Beispiel #12
0
 public ProductRepository(PgDbContext context)
     : base(context)
 {
 }
Beispiel #13
0
 public UserRoleRepository(PgDbContext dbContext)
 {
     this.dbContext = dbContext;
 }
Beispiel #14
0
 public UnitOfWorkActionFilter(PgDbContext dbContext) => _dbContext = dbContext;
Beispiel #15
0
 public ShopRepository(PgDbContext context) : base(context)
 {
 }
Beispiel #16
0
 public PgRepository(PgDbContext pgDbContext)
 {
     this.pgDbContext = pgDbContext;
 }
Beispiel #17
0
 public TransactionBehaviour(PgDbContext dbContext,
                             ILogger <TransactionBehaviour <TRequest, TResponse> > logger)
 {
     _dbContext = dbContext ?? throw new ArgumentException(nameof(PgDbContext));
     _logger    = logger ?? throw new ArgumentException(nameof(ILogger));
 }
Beispiel #18
0
 public PgUnitOfWork(PgDbContext context)
 {
     _context    = context;
     Sensors     = new PgSensorRepository(_context);
     Controllers = new PgControllerRepository(_context);
 }
Beispiel #19
0
 public ProductRepository(PgDbContext db) : base(db)
 {
 }
Beispiel #20
0
 public PgSensorRepository(PgDbContext context)
 {
     _context = context;
 }
Beispiel #21
0
 public UnitOfWork(PgDbContext pgContext)
 {
     _pgContext = pgContext;
 }
Beispiel #22
0
 public DemoController(PgDbContext dbcontext, IOptions <AppSettings> configuration)
 {
     this.dbcontext = dbcontext;
 }
Beispiel #23
0
 public RepositoryBase(PgDbContext context)
 {
     this.context = context;
 }
Beispiel #24
0
 public RacesRepository(PgDbContext context) : base(context)
 {
 }
Beispiel #25
0
 public WeaponItemPropertyRepository(PgDbContext context) : base(context)
 {
 }
Beispiel #26
0
 public GenericRepository(PgDbContext pgContext)
 {
     _pgContext = pgContext;
 }
Beispiel #27
0
 public WeaponRepository(PgDbContext context) : base(context)
 {
 }
Beispiel #28
0
 public PgControllerRepository(PgDbContext context)
 {
     _context = context;
 }
 public Repository(PgDbContext db)
 {
     Db    = db;
     DbSet = db.Set <T>();
 }