Ninject 和母版页注入
首先,我使用 Ninject 2.0
我有我的母版页,我想注入它,但我不太确定如何注入。我尝试创建一个派生自 System.Web.Mvc.ViewMasterPage 的类,然后创建我的属性:
[Inject]
public ICacheService<List<Category>> Categories { get; set; }
[Inject]
public IConfigurationSettings Config { get; set; }
当我运行我的页面时,这两个属性都没有设置并且均为空。关于为什么或如何执行此操作的示例有什么想法吗?谢谢
-- 更新
所以我做了更多的研究,看来我需要自己解析类中的对象,因为 Ninject 不会拦截类的创建。所以现在我的问题是如何为我的内核创建解析器?上面的代码位于类库中,因此我没有对内核的引用。我尝试了以下操作:(稍作修改 http://www.codethinked.com/post/2009/10/07/Our-Biggest-Enemy -Isne28099t-Developers-Who-Refuse-To-Move-Forward-It-is-Developers-Who-Pretend-To-Move-Forward.aspx )
public class KernelResolver
{
private static IKernel _kernel;
public KernelResolver(IKernel kernel)
{
_kernel = kernel;
}
public static T Resolve<T>()
{
return _kernel.Get<T>();
}
}
然后注册:
Bind<KernelResolver>().ToSelf()
然而内核为空...我只是需要查看一些示例,但我找不到任何示例,或者可能是我很困惑,以至于我不知道我在寻找什么:\
非常感谢任何帮助!
First off, I'm using Ninject 2.0
I have my master page which I would like to inject into but I'm not quite sure how. What I tried was created a class that derives from System.Web.Mvc.ViewMasterPage and then I create my properties:
[Inject]
public ICacheService<List<Category>> Categories { get; set; }
[Inject]
public IConfigurationSettings Config { get; set; }
When I run my page neither of the properties get set and are both null. Any ideas on why or example on how to do this? Thanks
-- Update
So I've done more research and it seems I need to resolve the objects within the class myself because Ninject does not intercept the creation of the class. So now my question is how do I create a resolver for my kernel? The above code is within a class library so I don't have a reference to the kernel. I tried the following: (slightly modified from http://www.codethinked.com/post/2009/10/07/Our-Biggest-Enemy-Isne28099t-Developers-Who-Refuse-To-Move-Forward-It-is-Developers-Who-Pretend-To-Move-Forward.aspx )
public class KernelResolver
{
private static IKernel _kernel;
public KernelResolver(IKernel kernel)
{
_kernel = kernel;
}
public static T Resolve<T>()
{
return _kernel.Get<T>();
}
}
and then registered:
Bind<KernelResolver>().ToSelf()
Yet kernel is null... I just need to see some examples but I can't find any or it could be that I'm so confused that I don't know what I'm looking for :\
Any help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我首先会问:为什么要向母版页中注入任何内容?如果您使用 MVC,您确实希望使用模型通过控制器将数据传递到视图(包括母版页)。这就是 MVC 框架的本质,尽管可能过于简单化了。
Ninject 或其他 IoC 容器可以为您做的是帮助创建控制器类并将依赖项注入到它们中 - 事实上,ASP.Net 中 IoC 容器的集成点通常接管控制器工厂的职责。
将适当的依赖项注入控制器可以帮助控制器确定要传递到视图(包括母版页)的适当模型或模型状态。
我喜欢将 MVC 模式(就此而言,ASP.Net MVC)视为将所有决策(即控制)的责任放在控制器类中。控制器执行的决策可能由大量输入(例如配置、用户输入、环境等)决定,但在某些时候,模型会被控制器传递到视图。我认为模型包含系统主题的状态。在我看来,这些视图,尤其是 ASP.Net MVC 中的视图,最好被视为没有任何逻辑,更像是“脚本”,而不是某些对象模型中的真正类或一流公民。
也许您有充分的理由让您的视图母版页变得“更智能”,但通常情况下,您想要注入的内容类型最好注入到您的控制器中,并将必要的数据传递到您的视图中。我很想了解更多有关您想要实现的目标的信息。
注意:您可以轻松设置 Ninject 与 MVC 集成,以注入所有控制器的依赖项。查看此 StackOverflow 问题/答案主题以获取详细信息。
I'll start by asking: why do you want to inject anything into a master page? If you are using MVC, you really want to use models to pass data to your views (including the master page) by way of a controller. That's the essence of an MVC framework, if perhaps over-simplified.
What Ninject or other IoC containers can do for you is help create your controller classes and inject dependencies into them -- indeed the integration point for IoC containers in ASP.Net is typically taking over the responsibility of the controller factory.
Injecting appropriate dependencies into your controller may assist the controller in determining the appropriate model or state of the model to pass to your views (including the master page).
I like to think of the MVC pattern (and, for that matter, ASP.Net MVC) as placing the responsibility of all the decision making (i.e. control) in the controller classes. The decisions carried out by controllers may be shaped by numerous inputs (e.g. configuration, user input, environment, etc) but at some point, a model is passed to a view by the controller. I think of the model containing the state of the subject matter of the system. The views, in my opinion, especially in ASP.Net MVC are best viewed as being void of any logic and more like "scripts" than true classes or first-class cititzens in some object model.
Perhaps you have a valid reason for making your view master page "smarter", but typically, the types of things you're looking to inject are best injected to your controller and the requisite data passed to your view. I'd be curious to hear more about what you're trying to accomplish.
Note: you can easily setup Ninject integration with MVC to have all your controllers' dependencies injected. Check out this StackOverflow question/answer thread for the details.
KernelResolver 类实现了服务定位器模式(向您的依赖项询问[中心]内容),这通常是最后的手段,而不是主要方法。你真正想要的是让你的依赖注入工作。
您绝对不会
Bind
KernelResolver
到任何东西 - 它充当整体容器,并且系统(例如,MVC 的工厂)需要连接起来才能提供帮助。[Inject]
属性本身并没有什么神奇的作用 - 当有人请求 Ninject 注入该类型的对象时,它们会被 Ninject 使用。您正在寻找的是如何告诉 MVC 在正确的时间调用 Ninject 的示例,我将让您搜索该示例(ninject“asp.net mvc”示例?)。
The
KernelResolver
class implements a service locator pattern (asking something [central] for your dependencies), which is generally more a last resort than a primary approach. What you really want is to get your dependency injection working.You definitely dont
Bind
KernelResolver
to anything - it acts as the overall container, and the system (e.g., MVC's factories) need to be hooked up to get it to help out.The
[Inject]
attributes don't do anything magic on their own - they're used by Ninject when someone requests it to inject an object of that type.What you're looking for is an example of how you tell MVC to call Ninject at the right times, which I'll let you search for (ninject "asp.net mvc" example?).