Giter Site home page Giter Site logo

sting-ioc's Introduction

Sting - C# IoC container for dependency injection

Usage

Creating a new container

using Sting;

var container = new Container();

Registering a service

interface ILog { void Info(string text); }
class Log : Ilog 
{ 
    public void Info(string text) 
    { 
        Console.WriteLine(text);
    }
}
container.Register<ILog, Log>();

Resolving a service

var logger = container.Resolve<ILog>();
logger.Info("Hello");

Singleton service

container.RegisterSingleton<ILog, Log>();

var logger1 = container.Resolve<ILog>();
var logger2 = container.Resolve<ILog>();

Assert.True(logger1 == logger2);

Resolve dependencies

interface IPublisher { void Publish(string message); }
class MyPublisher : IPublisher 
{
    private ILog Log { get; }

    public MyPublisher(ILog log) 
    { 
        Log = log; 
    }

    public void Publish(string message) 
    {
        Log.Info($"Publishing message: {message}");
        // publish...
    }
}

container.Register<ILog, Log>();
container.Register<IPublisher, MyPublisher>();

var publisher = container.Resolve<IPublisher>();
publisher.Publish("Hello");

Code Status

Travis CI build status

sting-ioc's People

Contributors

gmmoreira avatar

Stargazers

Gabriel Malaquias avatar

Watchers

James Cloos 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.