Giter Club home page Giter Club logo

injectionist's Introduction

Injectionist

The Injectionist is a dependency injection tool that can be used to e.g. configure something.

It came to life when Rebus needed a flexible built-in IoC container-like thing that supported decorators. Rebus uses it to register stuff throughout the Configure.With(...) spell, finally calling injectionist.Get<IBus>() at the end to resolve the bus and have all of its dependencies injected.

How to use it?

First, create an Injectionist instance:

var injectionist = new Injectionist();

Then, register a type-to-func mapping in it:

injectionist.Register<ISomething>(c => new ImplementationOfSomething());

where ImplementationOfSomething, obviously, is an implementation of ISomething.

If a class has one or more dependencies and needs to have stuff injected, get that stuff from the IResolutionContext passed to the func:

injectionist.Register(c => new HomeController(c.Get<ISomething>()));

Now, let's pretend that I want to decorate ImplementationOfSomething with another implementation of ISomething - do this:

injectioninst.Decorate<ISomething>(c => {
    var something = c.Get<ISomething>();

    return new SomethingDecorator(something);
});

The Injectionist will always resolve decorators first, in the order that they're registered in, recursively moving closer to the primary implementation for each nested call to c.Get<ISomething>().

When the time comes to actually create an instance of something, just Get it:

var result = injectionist.Get<HomeController>();

which will give you a ResolutionResult<HomeController> whose Instance property will give you the HomeController instance you asked for, and the TrackedInstances property will be an IEnumerable of all the objects that were built in order to build the object you asked for.

var instance = result.Instance; //< this is the HomeController

var disposables = result.TrackedInstances.OfType<IDisposable>();

This way, you can do whatever is necessary to properly dispose disposables that may have been built in order to satisfy some remote transitive dependency of HomeController.

It's pretty simple, but it's still kind of neat.

License

Injectionist is MIT-licensed

injectionist's People

Contributors

mookid8000 avatar

Watchers

 avatar  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.