Getting Started with LightInject
Asked Answered
P

1

7

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

I see the documentation.

I got the dll installed. Followed that step ok.

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)

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.

Thanks!

Prismatoid answered 12/3, 2015 at 19:12 Comment(4)
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();Metallography
Do you know what dependency injection is? If so, just replace your bootstrapping method with the container set-up.Goldcrest
@PaulSasik, so container is not a class in LightInject?Prismatoid
@Prismatoid Ah, I think you're missing this: var container = new ServiceContainer(); See my updated answer.Metallography
M
9

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();'

Something like:

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.");
        }
    }
}
Metallography answered 12/3, 2015 at 19:22 Comment(8)
Thanks. Where does container come from? That is my question I believe.Prismatoid
Great, thanks again. What do you recommend for me to house the container? A static field in a class, will that work ok? What is typically done to make sure it's created right off the bat, lives as long as the app lives, and is available throughout the app?Prismatoid
Yes, you usually want to create it when the app starts. For how you want to treat it study up on the scope and container lifetime stuff: lightinject.net/#toc8 (Yes, the docs are very light and confused you with a missing line of code but hang in there.)Metallography
Ok. I'll initialize a singleton in Main. I'm assuming that's the way one does that the best way. Thanks again!Prismatoid
@toddmo: Actually, you don't even have to do that. If you make use of constructor and/or property injection you won't have to reference container in the rest of your app! (At least from what I can tell of the documentation. Haven't tried LightInject myself yet.)Metallography
@toddmo: Sorry for the delay but check out my updated code sample (fully working). Of special interest is the use of IBar in Foo. Take a look!Metallography
Very nice! I'm going to ask Bernhard if he thinks it will be a nice addition to his documentation. Is that ok with you?Prismatoid
Let us continue this discussion in chat.Metallography

© 2022 - 2024 — McMap. All rights reserved.