/MGS.Autofac

Toolkit for Unity project develop with Autofac.

Primary LanguageC#Apache License 2.0Apache-2.0

[TOC]

# MGS.Autofac

Summary

  • Toolkit for Unity project develop with Autofac.

Environment

  • Unity 5.0 or above.
  • .Net Framework 3.5 or above.

Version

  • 1.2.0

Demand

  • Auto register the mark types.
  • Resolve mark type anywhere.

Implement

Config

  • AutofacEditor auto search the register types mark by AutofacRegisterAttribute, and build the runtime config file(AutofacConfigurator.cs) on UnityEditor DidReloadScripts.

Register

  • AutofacConfigurator register the mark types to AutofacUtility(Build Autofac Container) by RuntimeInitializeOnLoadMethodAttribute on UnityEngine load.

Usage

  • Reference

    If you need use AutofacUtility in dll:

    • Create a C# project, move the following files into the project:
      • ./Scripts/AutofacRegisterAttribute.cs
      • ./Scripts/AutofacUtility.cs
    • Compile project as dll to reference.
  • Define interface.

    public interface ITest
    {
        void DoTest();
    }
  • Use AutofacRegisterAttribute to mark a type need register.

    [AutofacRegister]
    internal class Test : ITest
    {
        public void DoTest(){}
    }
    
    [AutofacRegister(ServiceKey = "Debug", ServiceType = typeof(ITest))]
    internal class TestDebug : ITest
    {
        public void DoTest(){}
    }
  • Use AutofacUtility.Resolve to get the instance of the mark type.

    var test = AutofacUtility.Resolve<ITest>();
    test.DoTest();
    
    var testDebug = AutofacUtility.ResolveKeyed<ITest>("Debug");
    testDebug.DoTest();

Copyright © 2021 Mogoson. All rights reserved. mogoson@outlook.com