A poor man Dependency Injection

A poor man Dependency Injection

Dependency Injection (DI) has been around for a while now.

A typical use case would be, for instance, the same piece of code is used by two scenarios. For example, a system that hooks to a data source and does something with the data. On Sunday, the system hooks to Goolge, and on Monday to Facebook.

In this case, we usually create an interface and two implementations. Then somehow push the implementation into the code. On Sunday we invoke the first implementation, and on Monday the second implementation.

A more elegant solution is to use java reflection to inject the implementation via Class.forName(). That was true back than when we didn’t have frameworks such as Guice and a few others.

With Guice we only need to add something like this:

public interface Communication{

    public Data getData(String locator);

}
 

Public class GoogleCommunication implements Communication{

      public Data getData(String locator){

            // Fetch the data form Google somehow.
            return data;
      }

}


Public class FacebookCommunication implements Communication{

      public Data getData(String locator){

            // Fetch the data from Facebook somehow.
            return data;
      }
}

On top of that, we need also to create a Module:

public class BasicModule extends AbstractModule{


    @Override
    protected void configure(){

        bind(Communicator.class).to(GoogleCommunicator.class);
    }
}

And to glue it altogether:

public static void main(String[] args){

    Injector injector = Guice.createInjector(new BasicModule());
    Communication comms = injector.getInstance(Communication.class);
}

We can use Annotation for that as well:

@Inject @Named("GoogleCommunicator")
Communicator communicator;

Guice is good, actually very good, but in some cases I find it too verbal for me when it comes to small projects, or one that barely usage injection.

So I sat down and wrote my own ultra-light DI framework, it lacks the fancy interface, and it’s very basic, but I ended up with this simple piece of code (in this case creating an Integer):

Integer integer = Injector.of("java.lang.Integer","0");

We can also annotate the method:

@Inject(name="java.lang.Integer",values={"0"})
public void someMethod(){

    // Using the annotation above.
    Integer integer = Injector.of();
}

Or for a few local variables

@Inject(name="java.lang.Integer",values={"0"})
@Inject(name="java.lang.Error",values={"Dummy 1"})
public void someMethod(){

      // Using the annotations above.
      Integer integer = Injector.of(0);
      Error e2 = Injector.of(1);
}

It’s much more elegant and very easy to maintain and implement. But it comes in the price of giving up the supporting of inner dependencies.

Have fun, the code can be found here.

要查看或添加评论,请登录

Eran Shaham的更多文章

  • Microservices Chatbot and Coronavirus

    Microservices Chatbot and Coronavirus

    A few weeks ago I shared a short post about a new initiative of mine to have a fun bot to make life much easier in…

  • Docker image build vs. jib

    Docker image build vs. jib

    Jib is an open-source Java containerizer originally coming from Google. Jib allows to build Docker images from Java…

  • A JSON schema validator

    A JSON schema validator

    A simple JSON schema validator for the Vert.x world.

    2 条评论
  • vertx-lucene-classification

    vertx-lucene-classification

    Lucene is here for a long time, ML was added to Lucene for a few releases now, yet some aspects were left out. ML can…

  • Kafka vs. Pulsar

    Kafka vs. Pulsar

    Kafka is here for a long time. Perhaps too long.

    1 条评论
  • UMLet- an open source UML tool

    UMLet- an open source UML tool

    Some aspects of my day job work are drawing many diagrams. That's part of an architect role to create design documents…

    2 条评论
  • Revive- a Single Page Application framework

    Revive- a Single Page Application framework

    I'm uploading a short presentation about a new open sourced Revive which I've made public. Revive is a new light open…

  • A few words on Docker and Kubernetes

    A few words on Docker and Kubernetes

    We all know Docker Engine; it’s a container runtime. We can run “docker run” on a host whether it’s a server or a VM…

    2 条评论
  • Apache Storm and big data

    Apache Storm and big data

    A background: Big data is here for a while now. At the practical level, big data helps us to better understand our…

  • Cassandra VS. MongoDB

    Cassandra VS. MongoDB

    Cassandra and MongoDB became to be the two of the most popular NOSQL databases that are running around in the last few…

    4 条评论

社区洞察

其他会员也浏览了