【发布时间】:2017-10-28 19:40:33
【问题描述】:
我想有一个数据访问层的接口,并为各种数据库(即MongoDb,SQL Server等)实现它
public interface IDataAccess
{
Task InsertEntityAsync<T>(string collectionName, T entity) where T : IData;
// the rest
}
对于特定的数据库:
public class MongoDbDataAccess : IDataAccess
{
public Task InsertEntityAsync<T>(string collectionName, T entity) where T : IData
{
throw new NotImplementedException();
}
}
例如,我可以让T 代替StudentEntity 类型,然后在InsertEntityAsync() 方法中将其转换为特定数据库接受的类型。
但是我希望我的方法是通用的,所以如果我传递StudentEntity,该方法首先将其转换为StudentDocument,然后将其保存在db中,如果我传递UniversityEntity,该方法将其转换为UniversityDocument然后保存它,你就明白了。
如何有一个通用的方法将每个数据转换为数据库接受的相应类型?
【问题讨论】:
-
你的接口的意图是成为你可能使用的每个可能的数据库框架的接口,还是你可能在给定数据库上使用的每个实体类型的接口?
-
@ChrisThompson 他们俩
-
我不太了解反对意见。这怎么太宽泛了?!