Servicestack将IDbConnection注入静态类

时间:2014-09-24 07:02:29

标签: c# dependency-injection servicestack inversion-of-control

我正在使用servicestack 4.如何将数据库连接注入静态类?

的伪代码:

public static class SomeRepository
{
    public static IDbConnection Db { get; set; }

    public static List<SomeEntity> DoSomething()
    {
        return Db.Select<SomeEntity>();
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用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>();
    }
}

希望有所帮助。