I'm developing a multi-database application. There is one admin database and many customer databases which are identical in structure.
I also have a generic RepositoryBase which looks like the following:
public abstract class RepositoryBase<T> where T : IEntityModel
{
public IDbConnection DbConnection { get; set; }
/* ... further properties ... */
protected RepositoryBase(IDbConnection dbConnection)
{
DbConnection = dbConnection;
}
public async Task Insert(T entity)
{
await DbConnection.InsertAsync(entity);
}
public async Task<T> Get(int id)
{
var entityKey = CreateEntityKey(id);
return await DbConnection.GetAsync(entityKey);
}
/* ... UPDATE, DELETE etc. ... */
}
At certain points in the software I'm using this class for inserting both to the Admin DB and the customer DBs.
Now, I need advice on how should I change the database names when I'm using one of these base repository methods? (note: in every request I'm getting the database name from the Admin DB based on the logged in user and injecting it into the DI)
My possibilities:
- add database name as a parameter in these methods
- where used (e.g. in a service) change the database of the current repository instance
- get rid of these base methods and implement them individually
- other?
Which approach is the best do you think? Thank you!