Git Product home page Git Product logo

ricaun.di's Introduction

ricaun.DI

Visual Studio 2022 Nuke License MIT Build Release

ricaun.DI

Create a Container and register your dependencies with AddSingleton, AddScoped and AddTransient methods. Resolve your dependencies with Resolve method.

ricaun.Revit.DI

To use this library in Autodesk Revit, use the ricaun.Revit.DI library.

Implementation Reference

This project uses the same implementation from Onbox IOC container system, except by:

The Container when Dispose can dispose all IDisposable scoped instances registered in the container.

Container

// Empty Container.
Container container = new Container();
// Dispose Container.
container.Dispose();

ContainerUtils

// Create Container with itself registered as a singleton.
IContainer container = ContainerUtils.CreateContainer();
// Create Container with itself registered as a singleton, with dispose scoped instances enabled.
IContainer container = ContainerUtils.CreateContainer(true);

Singleton

// Register a singleton.
var singleton = new Singleton();
container.AddSingleton(singleton);
container.AddSingleton<ISingleton>(singleton);
container.AddSingleton<ISingleton, Singleton>();
container.AddSingleton<ISingleton, Singleton>(singleton);

Scoped

// Register a scoped.
container.AddScoped<Scoped>();
container.AddScoped<IScoped, Scoped>();

Transient

// Register a transient.
container.AddTransient<Transient>();
container.AddTransient<ITransient, Transient>();

Resolve

// Resolve the ISingleton.
var singleton = container.Resolve<ISingleton>();
// Resolve the IScoped.
var scoped = container.Resolve<IScoped>();
// Resolve the ITransient.
var transient = container.Resolve<ITransient>();

ResolveOrNull

// Resolve the ISingleton.
var singleton = container.ResolveOrNull<ISingleton>();
// Resolve the IScoped.
var scoped = container.ResolveOrNull<IScoped>();
// Resolve the ITransient.
var transient = container.ResolveOrNull<ITransient>();

Enable Console

// Enable Console.
container.EnableConsolePrinting(true);

Enable Dispose Scoped Instances

// Enable Dispose Scoped Instances.
container.EnableDisposeScopedInstances(true);

Static Container Example

Simple Host static class to hold the container.

using ricaun.DI;
public static class Host
{
    public static IContainer Container { get; } = ContainerUtils.CreateContainer();
    public static IContainerResolver ContainerResolver => Container;
    public static T Resolve<T>() where T : class => ContainerResolver.Resolve<T>();
    public static T ResolveOrNull<T>() where T : class => ContainerResolver.ResolveOrNull<T>();
}

Extension for the IHost interface.

public interface IHost { }
public static class HostExtension
{
    public static IContainer GetContainer(this IHost _) => Host.Container;
    public static IContainerResolver GetContainerResolver(this IHost _) => Host.ContainerResolver;
    public static T Resolve<T>(this IHost _) where T : class => Host.Resolve<T>();
    public static T ResolveOrNull<T>(this IHost _) where T : class => Host.ResolveOrNull<T>();
}

Release

License

This project is licensed under the MIT Licence.


Do you like this project? Please star this project on GitHub!

ricaun.di's People

Contributors

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