我在应用程序中使用 Dapper ORM。我创建了一个带有 Dapper 方法的接口,以便快速浏览此应用程序中正在使用 Dapper 的哪些功能,并且可以通过实现它轻松地被其他 ORM 替换。
public interface IDapperRepository
{
IEnumerable<T> GetAll<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class;
T GetById<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class;
}
class DapperRepository : IDapperRepository
{
public IEnumerable<T> GetAll<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class
{
//implementation
}
public T GetById<T>(string query, object cmdParams = null, CommandType cmdType = CommandType.Text) where T : class
{
//implementation
}
}
从 DAL 层:
public class UserRep : IUserRep
{
private readonly IDapperRepository _iDapperRepository;
public UserRep()
{
_iDapperRepository = new DapperRepository();
}
public IEnumerable<UserBO> GetAll()
{
return _iDapperRepository.GetAll<UserBO>("select * from users");
}
//Other methods
}
在用户列表页面中,_iUserRep.GetAll() 被控制器调用。
从上面的代码中,通过调用 _iUserRep.GetAll() 或存储库类中的任何其他方法,DapperRepository 类被实例化。我的问题是,因为我在 DapperRepository 类中只有实用程序方法,所以删除 IDapperRepository 并使用“静态”方法将 DapperRepository 修改为“静态”是个好主意,这样我就可以在不实例化它的情况下调用这些方法。我想知道这样做是否会带来任何性能提升。
此外,感谢任何改进此设计的输入。