# MGS.Autofact **Repository Path**: Mogoson/MGS.Autofact ## Basic Information - **Project Name**: MGS.Autofact - **Description**: Toolkit for Unity project develop with Autofac. - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 0 - **Created**: 2021-06-19 - **Last Updated**: 2022-12-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README [TOC] # MGS.Autofac ## Summary - Toolkit for Unity project develop with Autofac. ## Environment - Unity 5.0 or above. - .Net Framework 3.5 or above. ## 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. ```c# public interface ITest { void DoTest(); } ``` - Use AutofacRegisterAttribute to mark a type need register. ```c# [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. ```c# var test = AutofacUtility.Resolve(); test.DoTest(); var testDebug = AutofacUtility.ResolveKeyed("Debug"); testDebug.DoTest(); ``` ## Demo - Demos in the path "MGS.Packages/Autofac/Demo/" provide reference to you. ------ Copyright © 2021 Mogoson. All rights reserved. mogoson@outlook.com