I want to have one interface for data access layer, and implement it for various databases (i.e. MongoDb, SQL Server, etc)
public interface IDataAccess
{
Task InsertEntityAsync<T>(string collectionName, T entity) where T : IData;
// the rest
}
and for a specific database:
public class MongoDbDataAccess : IDataAccess
{
public Task InsertEntityAsync<T>(string collectionName, T entity) where T : IData
{
throw new NotImplementedException();
}
}
I could make T
to be instead of type StudentEntity
for example, and then inside InsertEntityAsync()
method convert it to a type accepted by that specific database.
But I want my method be generic, so if I pass StudentEntity
, the method convert it to StudentDocument
first then save it in db, if I pass UniversityEntity
, the method convert it to UniversityDocument
then save it, and you get the idea.
How to have a generic method to convert each data to a corresponding accepted type by the database?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…