0
我想使用StructureMap創建OrmLite的基本IoC,但是我犯了一些錯誤。使用StructureMap設置OrmLite
在OrmLite的網站,他們舉一個簡單的例子,如何注入它:
container.Register<IDbConnectionFactory>(c =>
OrmLiteConnectionFactory(":memory:", SqliteDialect.Provider)); //InMemory Sqlite DB
所以我想在我的StructureMap新創建WebAPI2應用程序中使用它。
我已經安裝了StructureMap.WebAPI2的NuGet,我有以下配置:
container.Configure(x => {
x.For<IAuthenticationService>().Use<AuthenticationService>();
x.For<IDbConnectionFactory>()
.Use<OrmLiteConnectionFactory>().Ctor<string>("connectionString").Is("Server=(localdb)\v11.0;Integrated Security=true;")
.Ctor<IOrmLiteDialectProvider>("dialectProvider").Is(SqlServerOrmLiteDialectProvider.Instance);
});
而且在這種情況下IAuthenticationServer正在正常實例化。但是,當我嘗試訪問DbConnection它沒有被注入,它是空的
public IDbConnectionFactory DbFactory { get; set; } //injected by IOC
IDbConnection db;
IDbConnection Db
{
get
{
return db ?? (db = DbFactory.Open());
}
}
這就是我試圖訪問數據庫的方式。並且在getter DbFactory中爲null。我該如何解決這個問題?