我有一个使用Ninject(NuGet安装)的ASP.NET MVC 3项目。我想了解如何使用它将依赖关系注入到非MVC对象中。如何在ASP.NET MVC应用程序中为非MVC对象使用Ninject
我有一些看起来类似于下面的代码。我如何使用Ninject在下面的对象中获取IStore的具体实例?
public class SomeObject
{
private static IStore _store;
public static IStore CurrentStore
{
get
{
if (_store == null)
{
// Get Instance of _store.
}
return _store;
}
}
}
在Global.asax中:
protected Application_BeginRequest()
{
IStore store = SomeObject.CurrentStore;
}
在NinjectWebCommon.cs:
private static void RegisterServices(IKernel kernel)
{
// Module that binds a concrete type of IStore.
kernel.Load<WebModule>();
}
此方法工作正常,但它意味着我必须检查_store为null并将其值设置在我的模块中,该值将在每个请求中运行。有更清洁的方法吗? – 2012-04-13 05:05:16