vendredi 15 janvier 2021

How to Store Record in two databases?

I need Architectural help on one the problem I am trying to solve.

I have data coming via Web Service call 'ManagerWebService' followed by I need to store Manager record in two databases where 'Manager' table structure is even different in both databases

So to solve this problem, I have introduced two sets of DbContext with its own table/ entity properties and then an Abstract base class which is call by query/ command classes to actual implement the data / LINQ script

Abstract Command Class

public abstract class BaseCommand<T>
{
    private readonly Settings _settings;

    protected CoreDbContext db;
    protected EcpDbContext ecpDb;


    protected BaseCommand(IOptions<Settings> settings)
    {
        this._settings = settings.Value;
        
        this.db = new CoreDbContext(_settings.SqlConnectionString);
        this.ecpDb = new EcpDbContext(_settings.EcpSqlConnectionString);

    }

    public abstract Task<T> Execute();
}

let say, now I want to create manager record in two database following class will create relevant detail and that where I believe need help.

In 'InitiateProcessing' Method I am trying to create two database record by passing individual dbContext but this is not working

Create Manager Class

public class CreateAreaManager : BaseCommand<bool>, ICreateAreaManager
{

 public CreateAreaManager(IOptions<Settings> settings)
        : base(settings)
    {
   
    }

  public override async Task<bool> Execute()
    {
            // try to create two database record in 2 database by passing individual dbContext but this is not working
            await InitiateProcessing(db);
            await InitiateProcessing(ecpDb);
    }  

   private async Task InitiateProcessing<T>(T dbContext) where T:class  // this is not working because then listed method in CoreDbContext and EcpDbContext become not accessible for example dbContext.<EntityName> throw error
    {

     // code to store record in database...

Aucun commentaire:

Enregistrer un commentaire