Git Product home page Git Product logo

ioclite's Introduction

IocLite

IOC Lite is an IOC container that can be used in projects that need a lightweight internal DI container. It can be especially useful if you are building a library and need to use an IOC container, but also want to provide your users with an easy way to either use your internal IOC to register their own dependencies or swap out your internal IOC container and use their own container like Structure Map, or Castle Windsor.

IOC Lite is not a full fledged IOC Container like StructureMap, or Castle Windsor and only supports a subset of their functionality. However, it does support constructor injection and object lifetime management.

Getting Started

To get started with IOC Lite, in your Global.asax create a new instance of Container.

protected void Application_Start()
{
    _container = new Container();
}

##Register

To register your dependencies create a new class that dervies from Registry and override the Load() method.

public class IocRegistry : Registry
{
    public override void Load()
    {
        For<IVideoGameRepository>().Use<VideoGameRepository>();
        For<IConsoleRepository>().Use<ConsoleRepository>();
    }
}

IOC Lite provides a fluent interface for registering your dependencies.

When registering dependencies you can also specify the object lifetime. By default dependencies will use the Default Scope and a new instance of the type will be created everytime you request one from the container.

public class IocRegistry : Registry
{
    public override void Load()
    {
        For<IVideoGameRepository>().Use<VideoGameRepository>().InSingletonScope();
        For<IConsoleRepository>().Use<ConsoleRepository>();
    }
}

Back in your Global.asax, call the Register method on the container and pass it a list of your registries:

protected void Application_Start()
{
    _container = new Container();
    _container.Register(new List<IRegistry>
    {
        new IocRegistry()
    });
}

##Resolve

To resolve an instance of a type you can use the following methods on the container.

var instance = _container.Resolve(typeof(IVideoGameRepository));
var instance = _container.Resolve<IVideoGameRepository>();

###Release

Object Lifetime

IOC Lite provides a few options for object lifetime management. By default types registered with the container will be created each time you try and resolve the type from the container. You can optionally change the scope of the object to one of the following object scopes:

Scope Description
Default The container will create a new instance of the type everytime one is requested.
Singleton The container will create a single instance of the type the first time one is request, and will return that one instance for all future requests for the lifetime of the application.
Thread Like singleton scope the container will create a single instance of the type the first time one is requested, and will return that one instance for the duration of the thread.
Http Request Like singleton and thread scope the container will create a single instance of the type the first time one is requested, and will return that one instance for the duration of the http request.

ioclite's People

Contributors

officert avatar

Watchers

 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.