[TOC]
# MGS.Autofac
- Toolkit for Unity project develop with Autofac.
- Unity 5.0 or above.
- .Net Framework 3.5 or above.
- 1.2.0
- Auto register the mark types.
- Resolve mark type anywhere.
- AutofacEditor auto search the register types mark by AutofacRegisterAttribute, and build the runtime config file(AutofacConfigurator.cs) on UnityEditor DidReloadScripts.
- AutofacConfigurator register the mark types to AutofacUtility(Build Autofac Container) by RuntimeInitializeOnLoadMethodAttribute on UnityEngine load.
-
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.
- Create a C# project, move the following files into the project:
-
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