Last Updated: February 25, 2016
·
921
· ordepdev

Manual Dependency Injection

If you don't like any of the plenty IoC out there you gonna love this fancy way to inject your dependencies by hand.

First, if you don't know what the hell is DI I'm shure that you use this line of code several times:

MyBusinessLayer _myBusinessLayer = new MyBusinessLayer();
var results = _myBusinessLayer.GetAll();

Don't use it anymore. Use this instead.

public static class BL<T> where T : new()
{
    public static T GenericBL
    {
        get { return new T(); }
    }
}

var results = BL<MyBusinessLayer>.GenericBL.GetAll();

Pretty simple, don't you think?

2 Responses
Add your response

Well, you haven't really done anything :)

Old:

MyBusinessLayer _myBusinessLayer = new MyBusinessLayer();

var results = _myBusinessLayer.GetAll();

New:

MyBusinessLayer _myBusinessLayer = BL<MyBusinessLayer>.GenericBL;

var results = _myBusinessLayer.GetAll();

...you just wrote it on one line.

over 1 year ago ·

But the fact is that you are creating -again- a new dependency by hand.

I don't want to do that anymore. With this you can remove all typed dependencies on your code.

over 1 year ago ·