Skip to content

Commit

Permalink
0.1.2-beta
Browse files Browse the repository at this point in the history
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
  • Loading branch information
decembrist-revolt authored and decembrist-revolt committed May 6, 2021
1 parent f48dfd2 commit fdde581
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Example/DecembristConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ public class DecembristConfiguration : IDecembristConfiguration
public ContainerBuilder ConfigDi(ContainerBuilder builder)
{
builder.RegisterInstance(InstanceService.Instance);
builder.Register<SingletonService3>();
builder.Register<SingletonService2, IService>();
builder.Register<SingletonService1>();
builder.RegisterPrototype<PrototypeService1>();
builder.RegisterPrototype<PrototypeService2>();
return builder;
}
}
Expand Down
5 changes: 4 additions & 1 deletion Example/Service/PrototypeService1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
21 changes: 21 additions & 0 deletions Example/Service/PrototypeService2.cs
Original file line number Diff line number Diff line change
@@ -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()}";
}
}
}
14 changes: 14 additions & 0 deletions Example/Service/SingletonService3.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
42 changes: 19 additions & 23 deletions Example/TestScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -40,18 +35,20 @@ public override void _Ready()
AssertTrue(this.Resolve<PrototypeService1>() != null, "prototype service exists");
AssertTrue(this.Resolve<PrototypeService1>() != 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<IService>().GetString()}");
GD.Print($"prototype1 says {prototypeService1.GetString()}");
GD.Print($"prototype2 says {prototypeService2.GetString()}");
GD.Print($"resolved prototype says {this.Resolve<PrototypeService1>().GetString()}");
GD.Print($"singleton1 says {service1.GetString()}");
GD.Print($"singleton2 says {service2.GetString()}");
GD.Print($"resolved singleton says {this.Resolve<IService>().GetString()}");
GD.Print($"prototype1 says {prototypeService1.GetString()}");
GD.Print($"prototype2 says {prototypeService2.GetString()}");
GD.Print($"resolved prototype says {this.Resolve<PrototypeService1>().GetString()}");
}

private static void AssertTrue(bool expression, string test)
Expand All @@ -67,6 +64,5 @@ private static void AssertTrue(bool expression, string test)
throw new Exception(message);
}
}

}
}
}
21 changes: 17 additions & 4 deletions addons/decembrist_plugin/Di/Container.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ internal Container(Dictionary<Type, Dependency> instanceMap, Dictionary<Type, De
private void InstantiateTypes(Dictionary<Type, Dependency> typeMap)
{
var iteration = typeMap.Count;
while (typeMap.Count > 0 && iteration == typeMap.Count)
while (typeMap.Count > 0 && iteration >= 0)
{
var toRemove = new List<Type>();
foreach (var (type, dependency) in typeMap)
Expand Down Expand Up @@ -89,8 +89,7 @@ private void InstantiateTypes(Dictionary<Type, Dependency> typeMap)

if (typeMap.Count != 0)
{
var typesArr = typeMap.Values.Select(type => type.ToString()).ToArray();
throw new Exception($"Unsatisfied dependencies {string.Join("", typesArr)}");
ThrowUnsatisfiedDependenciesException(typeMap);
}
}

Expand All @@ -107,6 +106,14 @@ private void InstantiateTypes(Dictionary<Type, Dependency> typeMap)
return instance;
}

private void ThrowUnsatisfiedDependenciesException(Dictionary<Type, Dependency> typeMap)
{
var typesArr = typeMap.Values
.Select(type => type.ToString())
.ToArray();
throw new Exception($"Unsatisfied dependencies for [{string.Join(", ", typesArr)}]");
}

/// <summary>
/// Instantiate from constructor by params
/// </summary>
Expand All @@ -121,7 +128,8 @@ private void InstantiateTypes(Dictionary<Type, Dependency> typeMap)
var paramType = parameter.ParameterType;
if (_instanceMap.ContainsKey(paramType))
{
paramArgs.Add(_instanceMap[paramType]);
var instance = ResolveOrNull(paramType)!;
paramArgs.Add(instance);
}
else
{
Expand Down Expand Up @@ -193,6 +201,11 @@ private static bool ValidAsType(Type type, Type? asType)
)
{
}

public override string ToString()
{
return $"{{\"type\": \"{Type}\", \"asType\": \"{AsType}\"}}";
}
}
}
}
2 changes: 1 addition & 1 deletion addons/decembrist_plugin/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -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"

0 comments on commit fdde581

Please sign in to comment.