1

我想将 Ninject 与 ServiceStack ORMLite 一起使用,但我不确定如何配置它。

我的存储库中有以下内容:

private readonly IDbConnectionFactory _dbFactory;

public TaskRepository(IDbConnectionFactory dbFactory)
{
    _dbFactory = dbFactory;
}

public IEnumerable<Task> GetAll()
{
    using (IDbConnection _db = _dbFactory.OpenDbConnection())
    {
        return _db.Select<Task>();
    }
}

我只是不确定如何注册它以使用我的连接字符串。我一定OrmLiteConnectionFactory喜欢IDbConnectionFactory这样:

kernel.Bind<IDbConnectionFactory>().To<OrmLiteConnectionFactory>().InScope(x => x.Request);

我还在方法中创建了一个新实例,OrmLiteConnectionFactory如下Configure所示AppHost

var ormLite = new OrmLiteConnectionFactory(
    ConfigurationManager.ConnectionStrings["DefaultConnection"]
    .ConnectionString, SqlServerDialect.Provider);

但是当我尝试使用服务时,我得到:ConnectionString must be set

编辑

这是我的注册方式OrmLiteConnectionFactory

里面Configure我有:

var ormLite = new OrmLiteConnectionFactory(
ConfigurationManager.ConnectionStrings["AngularApp"]
.ConnectionString, SqlServerDialect.Provider);

// Create Tables and Seed Data
CreateSeedData(ormLite);

IKernel kernel = new StandardKernel();

// Register dependencies in method
RegisterDependencies(kernel);

RegisterDependencies好像:

private void RegisterDependencies(IKernel kernel)
{
    kernel.Bind<IDbConnectionFactory>().To<OrmLiteConnectionFactory>()
                                       .InSingletonScope();
    kernel.Bind<ITaskRepository>().To<TaskRepository>();
}
4

1 回答 1

3

IDbConnectionFactory 是一个数据库连接工厂,所以它应该是一个单例,即你想注入配置的实例而不是为每个请求创建一个新的实例,例如:

kernel.Bind<IDbConnectionFactory>().ToMethod(c => 
    new OrmLiteConnectionFactory(
        ConfigurationManager.ConnectionStrings["DefaultConnection"]
        .ConnectionString, SqlServerDialect.Provider))
    .InSingletonScope();
于 2013-05-24T20:51:29.413 回答