0
由于.NET核心不支持分布式事务,因此我有精巧的通用存储库类。我决定将已打开的连接对象发送到泛型方法,而不是单独注入到存储库中。通用存储库的静态成员/类
public class Repository<T> where T: class
{
protected readonly IComplianceConnection Connection;
public Repository(IComplianceConnection connection)
{
Connection = connection;
}
public IEnumerable<T> Get(string query, object arguments)
{
IList<T> entities;
using (var connection = Connection.OpenConnection())
{
entities = connection.Query<T>(query, arguments, commandType: CommandType.StoredProcedure).ToList();
}
return entities;
}
}
我需要将其更改为静态类。下面是新的存储库。
public static class Repository<T> where T: class
{
public static IEnumerable<T> Get(this IDbConnection connection, string query, object arguments)
{
IList<T> entities;
entities = connection.Query<T>(query, arguments, commandType: CommandType.StoredProcedure).ToList();
return entities;
}
}
此通用存储库的静态方法是否正确? ..Pls建议我
1.我不认为使用是“合适的”,也许连接已经建立??。 2.为什么你不把这个方法放在DbContext上? – Max
您可否提供示例代码? – Ljt