Giter Site home page Giter Site logo

dynamic-connection-string's Introduction

Dynamic-Connection-string

Shows how to use a connection string at runtime, using Clean Architecture

1. Expose Method to change Connection String in ICompanyDbContext (Application Layer)

using CA.Domain;
using Microsoft.EntityFrameworkCore;

namespace CA.Application.Common.Interfaces
{
    public interface ICompanyDbContext
    {
        DbSet<Employee> Employees { get; set; }

        void SetConnectionString(string connectionString);
    }
}

2. Implement SetConnectionString method in CompanyDbContext (Persistence Layer)

Nuget to install: Microsoft.EntityFrameworkCore.Relational

using CA.Application.Common.Interfaces;
using CA.Domain;
using Microsoft.EntityFrameworkCore;

namespace CA.Persistence
{
    public class CompanyDbContext : DbContext, ICompanyDbContext
    {
        public DbSet<Employee> Employees { get; set; }

        public CompanyDbContext(DbContextOptions<CompanyDbContext> dbContextOptions)
            : base(dbContextOptions)
        {
        }

        public void SetConnectionString(string connectionString)
        {
            Database.GetDbConnection().ConnectionString = connectionString;
        }
    }
}

3. Pass ConnectionString through Controller (Web Layer)

[HttpGet("api/v1/employees/{connectionString}")]
public async Task<ActionResult> Index(string connectionString)
{
    //Just hardcoding for example
    connectionString = @"Data Source=DESKTOP-S0OLDPR\SQLEXPRESS;Initial Catalog=VidlyDB;Integrated Security=True";
    var result =  await _mediator.Send(new GetAllEmployeesQuery() { ConnectionString = connectionString });

    return Ok(result);
}

4. Change Connection String at Runtime (Application Layer)

using CA.Application.Common.Interfaces;
using CA.Domain;
using MediatR;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace CA.Application.CompanyContext.Queries.GetAllEmployees
{
    public class GetAllEmployeesHandler : IRequestHandler<GetAllEmployeesQuery, List<Employee>>
    {
        private readonly ICompanyDbContext _companyDbContext;

        public GetAllEmployeesHandler(ICompanyDbContext companyDbContext)
        {
            //Note: Connection is not yet opened here
            _companyDbContext = companyDbContext;
            
        }

        public async Task<List<Employee>> Handle(GetAllEmployeesQuery request, CancellationToken cancellationToken)
        {
            //Set new connection string
            _companyDbContext.SetConnectionString(request.ConnectionString);

            //Connection would only be opend when ToListAsync is called
            return await _companyDbContext.Employees.ToListAsync();
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.