用于 C# ASP.NET(非 MVC)的 Ninject 2.0 依赖注入

发布于 2024-09-11 18:00:12 字数 1286 浏览 5 评论 0原文

我已经找了一段时间了。

我对依赖注入并不陌生,并且已经在多个项目 MVC 和喜欢,但我想尝试一下 Ninject,以免错过其中的乐趣。

我正在尝试将 Ninject 与我正在更新的现有网络应用程序一起使用。

我在 Ninject 提供的博客和 wiki 上找不到,说实话我有点不耐烦,所以可能错过了,而且 google 的前几页似乎已经过时了,或者谈论的是如何将 MVC 与 Ninject 结合使用。

到目前为止,我有以下内容并且它可以工作,但我希望有人能够指出一个侵入性较小的选项,涉及将 ServiceModule 调用到内核并从 Web 应用程序注入具有所需绑定的属性。

到目前为止我所拥有的是一个 ServiceModule:

public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        Bind<IPreRegistrationService>().To<PreRegistrationService>()
            .WithConstructorArgument("connectionString",connectionString);
    }
}

然后在我的页面中我定义了一个私有变量:

private IPreRegistrationService xfemPreRegistrationService = null;

然后在页面加载事件中:

    IKernel kernel = new StandardKernel(new ServiceModule());
    xfemPreRegistrationService = kernel.Get<IPreRegistrationService>();

所以这有效,但我想要的是进入一个我定义的阶段

[Inject]
public IPreRegistrationService xfemPreRegistrationService { get; set; }

:一页,剩下的就是魔法。

干杯

I have been searching for a while.

I'm not new to dependency injection and have used StructureMap with several projects MVC and the like, but I felt like giving Ninject a go, so as not to miss out on the fun.

I am trying to use Ninject with an existing web app which I am bringing up-to-date.

I couldn't find on the blogs and wiki provided by Ninject, I am a little impatient to be honest so may have missed it, and the first few pages of google appear to be out of date or talking about using MVC with Ninject.

So far I have the following and it works, but I was hoping someone could point out a less intrusive option, regarding calling the ServiceModule to the Kernel and injecting a property with the desired bind from the web app.

What I have so far is a ServiceModule:

public class ServiceModule : NinjectModule
{
    public override void Load()
    {
        string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        Bind<IPreRegistrationService>().To<PreRegistrationService>()
            .WithConstructorArgument("connectionString",connectionString);
    }
}

Then in my page I have defined a private variable:

private IPreRegistrationService xfemPreRegistrationService = null;

Then in the page load event:

    IKernel kernel = new StandardKernel(new ServiceModule());
    xfemPreRegistrationService = kernel.Get<IPreRegistrationService>();

So this works, but what I would like is to move on to a phase where all I define is:

[Inject]
public IPreRegistrationService xfemPreRegistrationService { get; set; }

on a page and the rest is magic.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

恋你朝朝暮暮 2024-09-18 18:00:12

感谢 这篇 stackoverflow 帖子 我发现了扩展 Ninject.Web

我发现的问题是你需要开始使用 Ninject。 Web 和我不能,因为我已经定义了一个 PageBase 来处理证券等。

所以,我能看到的唯一方法是采取 来自项目的 KernelContainer 类(因为 KernelContainer 被定义为内部):

然后从全局 asax 调用 OnApplicationStart:

KernelContainer.Kernel = new StandardKernel(new ServiceModule());

// Request injections for the application itself.
KernelContainer.Inject(this);

然后从 OnInit 方法在我的 PageBase 中调用:

KernelContainer.Inject(this);

这使我能够达到简单放置的目标

[Inject]
public IPreRegistrationService xfemPreRegistrationService { get; set; }

:需要的

Thanks to this stackoverflow post I found out about the extension Ninject.Web

Problem I found was you need to start off using Ninject.Web and I could not as I already have a PageBase defined to handle securities and the such.

So, the only way I could see was to take the KernelContainer class from the project (as KernelContainer is defined as internal):

Then call from the global asax OnApplicationStart:

KernelContainer.Kernel = new StandardKernel(new ServiceModule());

// Request injections for the application itself.
KernelContainer.Inject(this);

Then in my PageBase from the OnInit method:

KernelContainer.Inject(this);

This has allowed me to reach my target of simply putting:

[Inject]
public IPreRegistrationService xfemPreRegistrationService { get; set; }

where needed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文