- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想在不使用 new 的情况下注入(inject) AuthenticationService:
IAuthenticationService authenticationService = null;
if (HttpContext.Current != null && HttpContext.Current.Session["LoggedUser"] == null)
{
HttpContext.Current.Session["LoggedUser"] = new AuthenticationService();
}
authenticationService = (AuthenticationService)HttpContext.Current.Session["LoggedUser"];
我正在考虑使用 kernel.Get(),但我不知道注入(inject) IKernel 是否是一个好习惯。我也在考虑使用工厂,但我不知道如何将它与 Ninject 结合起来。
你有什么建议?
最佳答案
您不应将 IKernel 注入(inject)到类中,如果您正确利用了 Ninject 提供的 IOC 容器,则没有必要这样做。您可以为服务设置类似于以下内容的绑定(bind):
kernel.Bind<IAuthenticationService>().To<AuthenticationService>();
请注意,根据您对 Ninject 的设置方式,这可能会发生在几个不同的地方。如果您提供更多代码,我可以详细说明这是怎么回事。对于许多人来说,它位于 NinjectWebCommon.cs 类中。
然后在任何你想注入(inject)的类中IAuthenticationService
into,只需传入 IAuthenticationService
像下面这样:
public class WhateverClass
{
private IAuthenticationService _authenticationService;
public WhateverClass(IAuthenticationService authenticationService)
{
_authenticationService = authenticationService;
}
//some other properties or methods that make use of authentication service here
}
关于c# - 我可以使用 Ninject 将 IKernel 注入(inject)类吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46309572/
我在几个代码示例中看到人们使用 IKernel 而不是使用 IWindsorContainer。为什么是这样? 这是一个例子:http://docs.castleproject.org/(S(kwaa
当我尝试使用下面的解决方案将 Ninject 3 与 MVC 4 RC Web Api 项目一起使用时,问题就开始了: http://www.peterprovost.org/blog/2012/06
总的来说,我是 IOC 的新手,我有点难以理解我正在尝试做的事情是否有意义。我有一个 Web 表单应用程序,我想在其中创建一个模块来为我定义一些绑定(bind)。这些绑定(bind)将用于将存储库注入
调用container.Release时没有触发事件,下面的测试总是失败: public void ComponentDestroyedEvent() { var wasDestroyed = f
我经常使用并喜欢 Ninject。 我想知道为什么没有"BasicKernel"在 Ninject.Portable 中? 实现 IKernel 是否需要任何 PCL 不包含的调用? 我说的是简单场景
我目前正在学习 Ninject 和依赖项注入(inject),在我当前的设置中,我将 IKernel 传递到一些地方,以便其他类可以实例化某些类或获取工厂实例。 目前我已经将我的工厂绑定(bind)为
我想在不使用 new 的情况下注入(inject) AuthenticationService: IAuthenticationService authenticationService = null
我是一名优秀的程序员,十分优秀!