English 中文(简体)
如何使用Ninject
原标题:How to Use Ninject

我今天一直在试图利用Ninject,并提出了几个问题。 首先,我需要把Inject属性运用到我想用于注射的所有建筑。 这似乎真的是me的吗? 难道我需要创建一种 Ker子,然后把我从任何地方传入一个注射式的阶层?

最佳回答

First of all do I need to use the Inject attribute on all constructors that I want to use injection for. This seems like a really lame design?

没有任何人实际上必须这样做。 自您与协会合作以来。 NET MVC 你可以安装Ninject。 MVC3Nget书。 这将使你开始使用<代码>。 缩略语 您可使用<代码>Registerservices方法登记与Ninject的接口/类别。 所有依赖这些接口的管制人员将自动由Ninject解决,不需要Inject属。

Do I need to create a Kernel then use that everywhere I pass in an injected class?

页: 1 服务定位模式,而不是依赖性注射——你想要在建筑人的理想位置上通过,而不是在特定的班子里使用油轮解决问题。 在解决问题时,应该有一个核心组成基础,即上文提及的“条码”“Registerservices

http://stevescodingblog.co.uk/dependency-injection-beginners-guide/“beginner s tutorial on Depend injection with Ninject and MVC3。

问题回答

从Ninject开始的最佳途径是开办小型项目。 查阅<代码>new。

在你提出申请的中间点,你在另一班子内重新开课。 这意味着你重新创建<>独立>。 依赖性注射指 绕过这些依赖性,通常是通过建筑商,而不是“embe/strong>。

Say you have a class like this, used to automatically create a specific type of note in Word. (This is similar to a project I ve done at work recently.)

class NoteCreator
{
    public NoteHost Create()
    {
        var docCreator = new WordDocumentCreator();
        docCreator.CreateNewDocument();
        [etc.]

WordDocumentCreator is a class that handles the specifics of creating a new document in Microsoft Word (create an instance of Word, etc.). My class, NoteCreator, depends on WordDocumentCreator to perform its work.

困难在于,如果我们有一天决定向上层文字处理员移动,我就不得不找到<代码>的所有地点。 WordDocumentCreator 即时发布,将其改为即时发送。 WordPerfectDocumentCreator.

现在想象一下,我会改变我的班子,这样看:

class NoteCreator
{
    WordDocumentCreator docCreator;

    public NoteCreator(WordDocumentCreator docCreator)  // constructor injection
    {
        this.docCreator = docCreator;
    }

    public NoteHost Create()
    {
        docCreator.CreateNewDocument();
        [etc.]

我的代码做了大量改动;在<代码>Create方法范围内完成的所有我手法都删除了“new的线。 但现在我只想说我的依赖。 让我们作出更小的改变:

class NoteCreator
{
    IDocumentCreator docCreator;

    public NoteCreator(IDocumentCreator docCreator)  // change to interface
    {
        this.docCreator = docCreator;
    }

    public NoteHost Create()
    {
        docCreator.CreateNewDocument();
        [etc.]

http://www.ohchr.org。 现在,我可在任何类别上通过该接口,而所有<代码>>>说明/编号必须使用其知道的方法。

www.un.org/Depts/DGACM/index_spanish.htm 现在是“<”部分。 我现在应该对我的评估有汇编错误,因为有些地方我正在创建<条码>:注Creator,其中有一张无参数的构件,已不复存在<>。 现在,我也需要从上删除依附。 换言之,我通过<>上面程序,但现在Im将其应用于创建新的<代码>说明/编号>的类别。 当你开始提取依赖物时,你发现,这些依赖物倾向于“穿透”你申请的根基,即只有<>>。 您应提及您的DI集装箱的地方(例如Ninject)。

我需要做的其他事情是configure Ninject。 关键部分是这样的一个类别:

class MyAppModule : NinjectModule
{
    public override void Load()
    {
        Bind<IDocumentCreator>()
            .To<WordDocumentCreator>();

这说明 Ninject,当我试图设立一个类别,其中一部分在行文上,需要一个<编码>IDocumentCreator时,它就应当建立一个<代码>WordDocumentCreator并加以使用。 Ninject进程就是这样:

  • Create the application s MainWindow. Its constructor requires a NoteCreator.
  • OK, so create a NoteCreator. But its constructor requires an IDocumentCreator.
  • My configuration says that for an IDocumentCreator, I should use WordDocumentCreator. So create a WordDocumentCreator.
  • Now I can pass the WordDocumentCreator to the NoteCreator.
  • And now I can pass that NoteCreator to the MainWindow.

这一系统的优点是三重。

首先,如果你不能混淆一些东西,你就会知道,因为你的物品一经提出申请就立即制造了。 Ninject将给你一个有帮助的错误信息,说你可以解决(例如)。

第二,如果管理层后来授权使用一个上层字处理器,那么你们都必须这样做。

  • Write a WordPerfectDocumentCreator that implements IDocumentCreator.
  • Change MyAppModule above, binding IDocumentCreator to WordPerfectDocumentCreator instead.

Third, if I want to test my NoteCreator, I don t have to pass in a real WordDocumentCreator (or whatever I m using). I can pass in a fake one. That way I can write a test that assumes my IDocumentCreator works correctly, and only tests the moving parts in NoteCreator itself. My fake IDocumentCreator will do nothing but return the correct response, and my test will make sure that NoteCreator does the right thing.

关于如何以这种方式安排您申请的更多信息,请查阅Mark Seemann的最近书,。 Dependency Injection in .NET. 不幸的是,它没有涵盖Ninject,但它的确涵盖了其他一些国际移民发展框架,它讨论了如何以上述方式安排你的申请。

另外,Michael Feathers还观看了Working Effectively With Legacy Code。 他就上述测试方面进行了会谈:如何打破接口,通过假肢,以孤立行为,使其受到考验。

这里有docs,包括tro。 鉴于你在问到,我感到非常合适。 如果你试图在不读完Ninject语的情况下停止使用,那么你只是放弃了。

www.un.org/chinese/ga/president

I can also highly recommend Mark Seemann s Dependency Injection in .Net as a companion book for DI based architecture (even though it doesnt directly cover Ninject).





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...