From fdde58127ae2938fc888a87ef47f2700bd295056 Mon Sep 17 00:00:00 2001 From: decembrist-revolt Date: Fri, 7 May 2021 00:03:54 +0300 Subject: [PATCH] 0.1.2-beta Fixed: 1. Fix InstantiateTypes iteration count bug iteration step should be -1 and should compare with started typeMap count https://github.com/decembrist-revolt/godot-decembrist-plugin/projects/1#card-60583602 2. Fix unsatisfied dependencies exception https://github.com/decembrist-revolt/godot-decembrist-plugin/projects/1#card-60583343 3. Fix prototype dependencies as dependencies https://github.com/decembrist-revolt/godot-decembrist-plugin/projects/1#card-59902409 --- Example/DecembristConfiguration.cs | 2 ++ Example/Service/PrototypeService1.cs | 5 ++- Example/Service/PrototypeService2.cs | 21 ++++++++++++ Example/Service/SingletonService3.cs | 14 ++++++++ Example/TestScene.cs | 42 +++++++++++------------- addons/decembrist_plugin/Di/Container.cs | 21 +++++++++--- addons/decembrist_plugin/plugin.cfg | 2 +- 7 files changed, 78 insertions(+), 29 deletions(-) create mode 100644 Example/Service/PrototypeService2.cs create mode 100644 Example/Service/SingletonService3.cs diff --git a/Example/DecembristConfiguration.cs b/Example/DecembristConfiguration.cs index c3d87d2..b6c57c2 100644 --- a/Example/DecembristConfiguration.cs +++ b/Example/DecembristConfiguration.cs @@ -8,9 +8,11 @@ public class DecembristConfiguration : IDecembristConfiguration public ContainerBuilder ConfigDi(ContainerBuilder builder) { builder.RegisterInstance(InstanceService.Instance); + builder.Register(); builder.Register(); builder.Register(); builder.RegisterPrototype(); + builder.RegisterPrototype(); return builder; } } diff --git a/Example/Service/PrototypeService1.cs b/Example/Service/PrototypeService1.cs index 8700a67..e200255 100644 --- a/Example/Service/PrototypeService1.cs +++ b/Example/Service/PrototypeService1.cs @@ -4,12 +4,15 @@ namespace Decembrist.Example.Service { public class PrototypeService1: IService { + public readonly PrototypeService2 PrototypeService; + private readonly SingletonService2 _service; private readonly string _randomNumber; - public PrototypeService1(SingletonService2 service) + public PrototypeService1(SingletonService2 service, PrototypeService2 prototypeService) { _service = service; + PrototypeService = prototypeService; _randomNumber = Random.RandomInt(1, 100).ToString(); } diff --git a/Example/Service/PrototypeService2.cs b/Example/Service/PrototypeService2.cs new file mode 100644 index 0000000..1c14e72 --- /dev/null +++ b/Example/Service/PrototypeService2.cs @@ -0,0 +1,21 @@ +using Decembrist.Utils; + +namespace Decembrist.Example.Service +{ + public class PrototypeService2: IService + { + private readonly SingletonService2 _service; + private readonly string _randomNumber; + + public PrototypeService2(SingletonService2 service) + { + _service = service; + _randomNumber = Random.RandomInt(1, 100).ToString(); + } + + public string GetString() + { + return $"{_randomNumber} {_service.GetString()}"; + } + } +} \ No newline at end of file diff --git a/Example/Service/SingletonService3.cs b/Example/Service/SingletonService3.cs new file mode 100644 index 0000000..dd01a7b --- /dev/null +++ b/Example/Service/SingletonService3.cs @@ -0,0 +1,14 @@ +namespace Decembrist.Example.Service +{ + public class SingletonService3: IService + { + private readonly SingletonService2 _service2; + + public SingletonService3(SingletonService2 service2) + { + _service2 = service2; + } + + public string GetString() => _service2.GetString(); + } +} \ No newline at end of file diff --git a/Example/TestScene.cs b/Example/TestScene.cs index 551b03e..b04e9ea 100644 --- a/Example/TestScene.cs +++ b/Example/TestScene.cs @@ -8,21 +8,16 @@ namespace Decembrist.Example { public class TestScene : Node2D { - [Inject] - private IService service1; - - [Inject] - private IService service2; - - [Inject] - private PrototypeService1 prototypeService1; - - [Inject] - private PrototypeService1 prototypeService2; - - [Inject] - private InstanceService instanceService; - + [Inject] private IService service1; + + [Inject] private IService service2; + + [Inject] private PrototypeService1 prototypeService1; + + [Inject] private PrototypeService1 prototypeService2; + + [Inject] private InstanceService instanceService; + public override void _Ready() { // init dependencies @@ -40,18 +35,20 @@ public override void _Ready() AssertTrue(this.Resolve() != null, "prototype service exists"); AssertTrue(this.Resolve() != prototypeService1, "service is prototype"); AssertTrue(prototypeService1 != prototypeService2, "service is prototype"); + AssertTrue(prototypeService1.PrototypeService != prototypeService2.PrototypeService, + "service.PrototypeService is prototype"); GD.Print("Service test......................................................"); ServiceEcho(); } private void ServiceEcho() { - GD.Print($"singleton1 says {service1.GetString()}"); - GD.Print($"singleton2 says {service2.GetString()}"); - GD.Print($"resolved singleton says {this.Resolve().GetString()}"); - GD.Print($"prototype1 says {prototypeService1.GetString()}"); - GD.Print($"prototype2 says {prototypeService2.GetString()}"); - GD.Print($"resolved prototype says {this.Resolve().GetString()}"); + GD.Print($"singleton1 says {service1.GetString()}"); + GD.Print($"singleton2 says {service2.GetString()}"); + GD.Print($"resolved singleton says {this.Resolve().GetString()}"); + GD.Print($"prototype1 says {prototypeService1.GetString()}"); + GD.Print($"prototype2 says {prototypeService2.GetString()}"); + GD.Print($"resolved prototype says {this.Resolve().GetString()}"); } private static void AssertTrue(bool expression, string test) @@ -67,6 +64,5 @@ private static void AssertTrue(bool expression, string test) throw new Exception(message); } } - } -} +} \ No newline at end of file diff --git a/addons/decembrist_plugin/Di/Container.cs b/addons/decembrist_plugin/Di/Container.cs index fe6b79b..eb10fda 100644 --- a/addons/decembrist_plugin/Di/Container.cs +++ b/addons/decembrist_plugin/Di/Container.cs @@ -57,7 +57,7 @@ internal Container(Dictionary instanceMap, Dictionary typeMap) { var iteration = typeMap.Count; - while (typeMap.Count > 0 && iteration == typeMap.Count) + while (typeMap.Count > 0 && iteration >= 0) { var toRemove = new List(); foreach (var (type, dependency) in typeMap) @@ -89,8 +89,7 @@ private void InstantiateTypes(Dictionary typeMap) if (typeMap.Count != 0) { - var typesArr = typeMap.Values.Select(type => type.ToString()).ToArray(); - throw new Exception($"Unsatisfied dependencies {string.Join("", typesArr)}"); + ThrowUnsatisfiedDependenciesException(typeMap); } } @@ -107,6 +106,14 @@ private void InstantiateTypes(Dictionary typeMap) return instance; } + private void ThrowUnsatisfiedDependenciesException(Dictionary typeMap) + { + var typesArr = typeMap.Values + .Select(type => type.ToString()) + .ToArray(); + throw new Exception($"Unsatisfied dependencies for [{string.Join(", ", typesArr)}]"); + } + /// /// Instantiate from constructor by params /// @@ -121,7 +128,8 @@ private void InstantiateTypes(Dictionary typeMap) var paramType = parameter.ParameterType; if (_instanceMap.ContainsKey(paramType)) { - paramArgs.Add(_instanceMap[paramType]); + var instance = ResolveOrNull(paramType)!; + paramArgs.Add(instance); } else { @@ -193,6 +201,11 @@ public Dependency(Type type, DependencyScope scope, Type? asType = null) : this( ) { } + + public override string ToString() + { + return $"{{\"type\": \"{Type}\", \"asType\": \"{AsType}\"}}"; + } } } } \ No newline at end of file diff --git a/addons/decembrist_plugin/plugin.cfg b/addons/decembrist_plugin/plugin.cfg index e9e551b..7dc7896 100644 --- a/addons/decembrist_plugin/plugin.cfg +++ b/addons/decembrist_plugin/plugin.cfg @@ -3,5 +3,5 @@ name="Decembrist Plugin" description="Utils for godot development" author="decembrist.org" -version="0.1.1-beta" +version="0.1.2-beta" script="DecembristPlugin.cs"