LightInject入门 [英] Getting Started with LightInject

查看:149
本文介绍了LightInject入门的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我喜欢LightInject上的基准;他们疯了!要走的路,我应该写一本有关.Net性能的书,我是认真的.

I love the benchmarks on LightInject; they are insane! Way to go, you should write a book on .Net performance, I'm serious.

我看到了文档.

我安装了dll.顺利执行了该步骤.

I got the dll installed. Followed that step ok.

然后文档的下一步假定我有一个container对象.

Then the next step of the documentation presumes I have a container object.

container.Register<IFoo, Foo>();
var instance = container.GetInstance<IFoo>();
Assert.IsInstanceOfType(instance, typeof(Foo));

哇!理所当然,我可能不是盒子里最敏锐的绘儿乐,但是我现在该怎么办?我应该创建什么类和方法来设置"它,以便我可以遵循其余的示例? (我想我最好对其进行设置,使其在整个项目中都可以使用)

Whoops! I may not be the sharpest crayola in the box, granted, but what do I do now? What class and methods should I create to "set it up" so I can follow the rest of the examples? (I guess I better set it up so that it works in the whole project)

顺便说一句:那时候在文档中添加这些步骤(如果不是显式的话),然后参考其他手册页",是否会出错?也许有多种获取容器的方法;我还不了解我需要哪一个.此时,我只是在文档中寻找在90%的情况下都可以使用"的示例,并链接到更专业的案例.

As an aside: Would it be wrong to add those steps in the doc at that point, if not explicitly, then by reference to other "man pages"? Maybe there are various ways of getting a container; I don't know enough to know which one I need. At this point in the documentation I was just looking for the "this will work in 90% of the situations" example, and links to more specialized cases.

谢谢!

推荐答案

您应该很好. IFoo是您的界面,而Foo是具体的实现.您应该能够做任何您想做的事.该教程只是向您展示DI所需的内容.例如,在IFoo中创建方法DoStuff,在Foo中实现它,然后调用它:'instance.DoStuff();'

You should be good to go. IFoo is your interface and Foo is the concrete implementation. You should be able to do whatever you want. The tutorial is just showing you what's needed for the DI. For example, create method DoStuff in your IFoo, implement it in Foo and then call it: 'instance.DoStuff();'

类似的东西:

using LightInject;
using System;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new ServiceContainer();
            container.Register<IFoo, Foo>();
            container.Register<IBar, Bar>();
            var foo = container.GetInstance<IFoo>();
            foo.DoFooStuff();
        }
    }

    public interface IFoo
    {
        void DoFooStuff();
    }

    public class Foo : IFoo
    {
        // this property is automatically populated!
        public IBar MyBar { get; set; }

        public void DoFooStuff()
        {
            MyBar.DoBarStuff();
            Console.WriteLine("Foo is doing stuff.");
        }
    }

    public interface IBar
    {
        void DoBarStuff();
    }

    public class Bar : IBar
    {
        public void DoBarStuff()
        {
            Console.WriteLine("Bar is doing stuff.");
        }
    }
}

这篇关于LightInject入门的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆