我想将 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>();
}