Coder Social home page Coder Social logo

trew's Introduction

Trew D.I. Build Status

Trew is a very lightweight and fast dependency injection library based on Guice like the old Syringe DI. Trew, unlike Syringe, supports circular dependencies, this is an important difference between these D.I

Why?

Trew helps you create a very scalable and orderly application in Java, check this examples with Trew and without Trew

With Trew

Injector injector = Injector.create(binder -> {
  binder.bind(Tea.class).toProvider(TeaProvider.class).singleton();
  binder.bind(Cookie.class).toInstance(new ChocolateCookie());
});
public class TeaProvider implements Provider<Tea> {
  @Override
  public Tea get() {
    Season season = Season.current();
    if (season == Season.SUMMER || season == Season.SPRING) {
      return new IcedTea();
    } else {
      return new HotTea();
    }
  }
}
public class Consumer {
  @Inject private Tea tea;
  @Inject private Cookie cookie;

  public void consumeYourStuff() {
    // do something with tea and cookie...
  }
}

Without Trew

public class TeaFactory {
  private static final Object lock = new Object();
  private static Tea tea;
  
  public static Tea get() {
    if (tea == null) {
      // thread-safety
      synchronized (lock) {
        if (tea == null) {
          Season season = Season.current();
          if (season == Season.SUMMER || season == Season.SPRING) {
            tea = new IcedTea();
          } else {
            tea = new HotTea();
          }
        }
      }
    }
    return tea;
  }
}
public class Consumer {
  private final Tea tea;
  private final Cookie cookie;
  
  public Consumer(Tea tea, Cookie cookie) {
    // trew checks nullability for you, if an injectable is annotated with
    // any @Nullable annotation, it's handled as an optional injection
    this.tea = Objects.requireNonNull(tea);
    this.cookie = Objects.requireNonNull(cookie);
  }
  
  public void consumeYourStuff() {
    // do something with tea and cookie...
  }
}

Instantiation without Trew is new Consumer(TeaFactory.get(), new ChocolatCookie()); Instantiation with Trew is Injector#getInstance(Consumer.class) or just @Inject private Consumer consumer;, with Trew the implementations are only known by the configuration Modules

Usage

Read the Wiki on GitHub to learn about the usage of Trew

Download

You can simply download the JAR from GitHub in the Releases section, or use Maven (recommended)

Maven Repositories

Releases repository, all versions that doesn't end with "-SNAPSHOT" will be here.

<repository>
    <id>unnamed-releases</id>
    <url>https://repo.unnamed.team/repository/unnamed-releases/</url>
</repository>

Snapshots repository, all versions ending with "-SNAPSHOT" will be here

<repository>
    <id>unnamed-snapshots</id>
    <url>https://repo.unnamed.team/repository/unnamed-snapshots/</url>
</repository>

Maven Dependency

Put this in your POM.xml <dependencies> section

<dependency>
    <groupId>me.yushust.inject</groupId>
    <artifactId>trew</artifactId>
    <version>0.2.5-SNAPSHOT</version> <!--Check the latest version in the repositories-->
</dependency>

trew's People

Contributors

yusshu avatar siansxint avatar alexissdev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.