I have dapper generic repository class, since .net core does not support distributed transaction I have decided to send already opened connection object to generic method instead of injecting separately in the repository.
我有dapper通用存储库类,因为.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;
}
}
I need to change this to static class .below is the new repository.
我需要将其更改为静态类.below是新的存储库。
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;
}
}
Is this static method for generic repository correct? ..Pls suggest me
这种通用存储库的静态方法是否正确? ..Pls建议我
1 个解决方案
#1
1
My understanding is that extension methods act on an instance of a class. In your Get() method, that instance is the connection parameter. However, it seems like you are overwriting the connection instance in your Get() method. This may not be desired.
我的理解是扩展方法作用于类的实例。在Get()方法中,该实例是连接参数。但是,您似乎在覆盖Get()方法中的连接实例。这可能不是所希望的。
You probably don't want to create an extension method. Perhaps you just want a static method that can accept an IComplianceConnection parameter.
您可能不想创建扩展方法。也许你只想要一个可以接受IComplianceConnection参数的静态方法。
public static IEnumerable<T> Get(IComplianceConnection complianceConnection, string query, object arguments)
{
IList<T> entities;
using (var connection = complianceConnection.OpenConnection())
{
entities = connection.Query<T>(query, arguments, commandType: CommandType.StoredProcedure).ToList();
}
return entities;
}
Hope this helps!
希望这可以帮助!
#1
1
My understanding is that extension methods act on an instance of a class. In your Get() method, that instance is the connection parameter. However, it seems like you are overwriting the connection instance in your Get() method. This may not be desired.
我的理解是扩展方法作用于类的实例。在Get()方法中,该实例是连接参数。但是,您似乎在覆盖Get()方法中的连接实例。这可能不是所希望的。
You probably don't want to create an extension method. Perhaps you just want a static method that can accept an IComplianceConnection parameter.
您可能不想创建扩展方法。也许你只想要一个可以接受IComplianceConnection参数的静态方法。
public static IEnumerable<T> Get(IComplianceConnection complianceConnection, string query, object arguments)
{
IList<T> entities;
using (var connection = complianceConnection.OpenConnection())
{
entities = connection.Query<T>(query, arguments, commandType: CommandType.StoredProcedure).ToList();
}
return entities;
}
Hope this helps!
希望这可以帮助!