Code Monkey home page Code Monkey logo

iockit's Introduction

IOCKit

独立的 IOC/DI Container,依赖注入、控制反转

由 QFramework 团队官方维护的独立工具包(不依赖 QFramework)。

环境要求

  • Unity 2018.4LTS

安装

快速开始

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using QFramework;

namespace QFramework.Example
{    

    public class ServiceA
    {
        public void Say()
        {
            Debug.Log("I am ServiceA:" + this.GetHashCode());
        }
    }
    
    public interface IService
    {
        void Say();
    }
  
    public class ServiceB : IService
    {
        public void Say()
        {
            Debug.Log("I am ServiceB:" + this.GetHashCode());
        }
    }
    
    public class ServiceC : IService
    {
        public void Say()
        {
            Debug.Log("I am ServiceC:" + this.GetHashCode());
        }
    }
  
    public class IOCExample : MonoBehaviour 
    {
        // 声明为需要注入的对象
        [Inject] 
        public ServiceA A {get;set;}

        [Inject] 
        public IService B {get;set;}

        [Inject] 
        public ServiceC C {get;set;}
        
        void Start () 
        {
            // 创建实例容器
            var container = new QFrameworkContainer();

            // 注册类型
            container.Register<ServiceA>();
            
            container.Register<IService,ServiceB>();

            container.RegisterInstance(new ServiceC());

            // 注入对象(会自动查找 Inject Atrributet的对象)
            container.Inject(this);

            // 注入之后,就可以直接使用 A 对象了
            A.Say();        
        }
    }
}

更多

iockit's People

Contributors

liangxiegame avatar

Watchers

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