Servicestack IDbConnection注入到静态类中

本文关键字:静态类 注入 IDbConnection Servicestack | 更新日期: 2023-09-27 18:24:14

我使用的是servicestack 4。如何将数据库连接注入静态类?

伪代码:

public static class SomeRepository
{
    public static IDbConnection Db { get; set; }
    public static List<SomeEntity> DoSomething()
    {
        return Db.Select<SomeEntity>();
    }
}

Servicestack IDbConnection注入到静态类中

您可以使用HostContext.TryResolve<IDbConnectionFactory>().OpenDbConnection()

public static class SomeRepository
{
    public static IDbConnection Db = HostContext.TryResolve<IDbConnectionFactory>().OpenDbConnection();
    public static List<SomeEntity> DoSomething()
    {
        return Db.Select<SomeEntity>();
    }
}

希望能有所帮助。