magnum-di

Magnum D.I.

License

License

Categories

Categories

Net
GroupId

GroupId

net.avh4.util.di
ArtifactId

ArtifactId

magnum-di
Last Version

Last Version

1.0.0-rc3
Release Date

Release Date

Type

Type

jar
Description

Description

magnum-di
Magnum D.I.
Project URL

Project URL

http://github.com/avh4/magnum-di
Source Code Management

Source Code Management

https://github.com/avh4/magnum-di

Download magnum-di

How to add to project

<!-- https://jarcasting.com/artifacts/net.avh4.util.di/magnum-di/ -->
<dependency>
    <groupId>net.avh4.util.di</groupId>
    <artifactId>magnum-di</artifactId>
    <version>1.0.0-rc3</version>
</dependency>
// https://jarcasting.com/artifacts/net.avh4.util.di/magnum-di/
implementation 'net.avh4.util.di:magnum-di:1.0.0-rc3'
// https://jarcasting.com/artifacts/net.avh4.util.di/magnum-di/
implementation ("net.avh4.util.di:magnum-di:1.0.0-rc3")
'net.avh4.util.di:magnum-di:jar:1.0.0-rc3'
<dependency org="net.avh4.util.di" name="magnum-di" rev="1.0.0-rc3">
  <artifact name="magnum-di" type="jar" />
</dependency>
@Grapes(
@Grab(group='net.avh4.util.di', module='magnum-di', version='1.0.0-rc3')
)
libraryDependencies += "net.avh4.util.di" % "magnum-di" % "1.0.0-rc3"
[net.avh4.util.di/magnum-di "1.0.0-rc3"]

Dependencies

compile (1)

Group / Artifact Type Version
org.pcollections : pcollections jar 2.1.2

test (4)

Group / Artifact Type Version
junit : junit jar 4.11
org.mockito : mockito-core jar 1.9.5
org.assertj : assertj-core jar 1.4.0
net.avh4.test : junit-nested jar 1.0.1

Project Modules

There are no modules declared in this project.

Build Status

Magnum, D.I.

If basketball was all there was to life, you'd be set.

Unfortunately there's more to life than basketball, which is why you probably need dependency injection. Magnum, D.I. is a dependency injection framework that assists in automatically wiring up your dependencies.

Guice, Spring DI, and PicoContainer are other popular dependency injection frameworks. Magnum, D.I. is superior in the following ways:

  • It's easy and pleasant to use hierarchical injection
  • It's easy and pleasant to create custom scopes
  • Dynamic custom scopes can easily be created at runtime
  • Errors with module configuration have easy-to-read error messages
  • Containers are immutable and persistent
  • It has the simplest module configuration syntax (a single constructor call)
  • It has a fluent API for creating complex module configurations
  • (Not implemented yet: Reinjection/parameter injection/method injection is not entangled with object instantiation)

Magnum, D.I. is intended to enforce its opinions on your code, and may not be suitable for use in legacy projects.

Usage

Add the following dependencies to your pom.xml:

  <dependency>
    <groupId>net.avh4.util.di</groupId>
    <artifactId>magnum-di</artifactId>
    <version>1.0.0-rc3</version>
  </dependency>

Building your DeLorean

(This DI example was taken from Public Object's blog post, "What's a Hierarchical Injector?")

Suppose we have the following components:

    public class DeLorean {
        public DeLorean(TimeCircuits timeCircuits, FluxCapacitor fluxCapacitor, EnergySource energySource) { }
    }

    interface FluxCapacitor { }
    public class RealFluxCapacitor implements FluxCapacitor {
        public RealFluxCapacitor(TimeCircuits timeCircuits) { }
    }

    public class TimeCircuits {
        public TimeCircuits(EnergySource energySource) { }
    }

    interface EnergySource { }
    public static class Plutonium implements EnergySource { }
    public static class LightningBolt implements EnergySource { }

Our goal is to configure our container to be able to make DeLoreans with different energy sources.

First, we will make a container with the reusable components.

    MagnumDI magnum = new MagnumDI(TimeCircuits.class, RealFluxCapacitor.class);

Now we can create DeLoreans! In each case, we will specify additional scoped dependencies (for our DeLoreans we will specify the EnergySource). Any other objects that depend on EnergySource in the scope of this DeLorean will get the same EnergySource instance.

    final DeLorean deLorean1 = magnum.add(Plutonium.class).get(DeLorean.class);
    final DeLorean deLorean2 = magnum.add(LightningBolt.class).get(DeLorean.class);

Build commands

License

MIT

Versions

Version
1.0.0-rc3
1.0.0-rc2