ica8fq
Last Updated: February 25, 2016
·
349
· ordepdev
49eba23cc4b611e2bacd22000a9e08df 7

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?

Say Thanks
Respond

2 Responses
Add your response

7380

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 ·
7413
49eba23cc4b611e2bacd22000a9e08df 7

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 ·