protected WebApplicationFactory <TEntryPoint> GetTestWebApplicationFactory <TEntryPoint>(
     WebApplicationFactory <TEntryPoint> appFactory, PaymentGatewayDbContext receiverDbContext)
     where TEntryPoint : class
 {
     return(appFactory.WithWebHostBuilder(builder =>
     {
         builder.ConfigureTestServices(services =>
         {
             services.AddSingleton(receiverDbContext);
         });
     }));
 }
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (disposing)
            {
                _paymentGatewayDbContext?.Dispose();
                _paymentGatewayDbContext = null;

                // This will automatically delete the database
                _dbConnection?.Dispose();
                _dbConnection = null;
            }

            _disposed = true;
        }
        public ServiceSetup()
        {
            var options = SqliteInMemory.CreateOptions <PaymentGatewayDbContext>();

            Context    = new PaymentGatewayDbContext(options);
            UnitOfWork = new UnitOfWork(Context);

            IServiceCollection services = new ServiceCollection();

            services.AddSingleton <IConfiguration>(new ConfigurationBuilder().Build())
            .AddDbContext <PaymentGatewayDbContext>(o => o.UseSqlite("DataSource=:memory:", x => { }));

            services.AddLogging();

            var provider = services.BuildServiceProvider();

            Context.Database.OpenConnection();
            Context.Database.EnsureCreated();
        }
        private static Payment CreateTestPayment(PaymentGatewayDbContext dbContext)
        {
            var payment = new Payment
            {
                Amount                = 1000,
                CreditCardCvv         = 345,
                CreditCardExpiryMonth = 11,
                CreditCardExpiryYear  = 2022,
                CreditCardNumber      = "8584739205746324",
                Currency              = "GBP",
                TransactionId         = Guid.NewGuid(),
                TransactionStatusCode = TransactionStatusCode.Successful
            };

            dbContext.Payments.Add(payment);

            dbContext.SaveChanges();

            return(payment);
        }
示例#5
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            try
            {
                services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
                _logger.LogInformation("Added MVC to services.");

                services.AddPayments();
                _logger.LogInformation("Added Payments to services.");

                // Entity framework core was chosen as the ORM because it boasts great performance in the majority of the cases.
                // In cases where performance is critical and EF core can't cope, we can always just use raw SQL commands through it.
                // Also, it implements the repository pattern and the unit of work pattern so we don't have to handle that ourselves.
                // Which means that we can switch the database provider whenever we want (as long as EF Core supports it).
                // For the scope of this assignment, I've chosen to just use SqlLite in memory. That would of course never be used in
                // production, but it can be switched to SQL Server or other providers effortlessly and the app will just work with no further code changes.

                // This is the code that would normally be used to add the db context to the services (using a different provider would be very similar).
                // services.AddDbContext<PaymentGatewayDbContext>(options => options.UseSqlite(_configuration.GetConnectionString("SqlLiteConnection")));

                // Unfortunately, this doesn't work with SqlLite in-memory since the asp.net core framework adds the db context as a scoped dependency,
                // which means that for each request it spins up a new instance of the class. Which results in the sql connection closing and the database
                // disappearing (SqlLite in-memory retains the data only as long as the connection remains open). So we have to get a bit creative here and
                // do an ugly hack to get this to work. I'm manually creating the db context and injecting it as a singleton. This will keep the connection
                // open until the app closes. This also means that we can't work with migrations like we normally would.
                _sqliteConnection = new SqliteConnection(_configuration.GetConnectionString("SqlLiteConnection"));
                _sqliteConnection.Open();
                var builder = new DbContextOptionsBuilder <PaymentGatewayDbContext>();
                builder.UseSqlite(_sqliteConnection);
                _paymentGatewayDbContext = new PaymentGatewayDbContext(builder.Options);
                _paymentGatewayDbContext.Database.EnsureCreated();
                services.AddSingleton(_paymentGatewayDbContext);
                _logger.LogInformation("Added DB context to services.");
            }
            catch (Exception e)
            {
                _logger.LogCritical(e, "Error when seting up the gateway.");
                throw;
            }
        }
        public PaymentGatewayDbContext GetPaymentGatewayDbContext()
        {
            if (_paymentGatewayDbContext != null)
            {
                return(_paymentGatewayDbContext);
            }

            if (_dbConnection == null)
            {
                _dbConnection = new SqliteConnection(_configuration.GetConnectionString("SqlLiteConnection"));
                _dbConnection.Open();
            }

            var builder = new DbContextOptionsBuilder <PaymentGatewayDbContext>();

            builder.UseSqlite(_dbConnection);

            _paymentGatewayDbContext = new PaymentGatewayDbContext(builder.Options);
            _paymentGatewayDbContext.Database.EnsureCreated();

            return(_paymentGatewayDbContext);
        }
示例#7
0
 public PaymentsService(ILogger <PaymentsService> logger, PaymentGatewayDbContext dbContext)
 {
     _logger    = logger;
     _dbContext = dbContext;
 }
 public PaymentRepository(PaymentGatewayDbContext context)
 {
     _context = context ?? throw new ArgumentNullException(nameof(context));
 }
示例#9
0
 public PaymentDetailsRepository(PaymentGatewayDbContext dbContext) => _dbContext = dbContext;
 public CurrencyRepository(PaymentGatewayDbContext dbContext) => _dbContext = dbContext;
示例#11
0
 public IdempotencyKeyRepository(PaymentGatewayDbContext dbContext) => _dbContext = dbContext;
示例#12
0
 public UnitOfWork(PaymentGatewayDbContext dbContext)
 {
     _dbContext = dbContext;
 }
示例#13
0
 public PaymentRepository(PaymentGatewayDbContext dbContext)
 {
     _dbContext = dbContext;
 }