Skip to content

Commit

Permalink
local scope injection bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
arty-F committed Jun 10, 2024
1 parent dacf124 commit 604f2cf
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 11 deletions.
49 changes: 47 additions & 2 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ GameObject:
- component: {fileID: 357009554}
- component: {fileID: 357009553}
m_Layer: 0
m_Name: ReleaseDependency
m_Name: ReleaseDependencyTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
m_IsActive: 0
--- !u!114 &357009553
MonoBehaviour:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -318,6 +318,50 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: b5d2362725654c94894df5bd0cc8a6d4, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &1794166374
GameObject:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
serializedVersion: 6
m_Component:
- component: {fileID: 1794166376}
- component: {fileID: 1794166375}
m_Layer: 0
m_Name: LocalScopeTest
m_TagString: Untagged
m_Icon: {fileID: 0}
m_NavMeshLayer: 0
m_StaticEditorFlags: 0
m_IsActive: 1
--- !u!114 &1794166375
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1794166374}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3a99b13ce29beec4b870bb953e8df2ff, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!4 &1794166376
Transform:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 1794166374}
serializedVersion: 2
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
m_LocalPosition: {x: 0, y: 0, z: 0}
m_LocalScale: {x: 1, y: 1, z: 1}
m_ConstrainProportionsScale: 0
m_Children: []
m_Father: {fileID: 0}
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!1 &1890540586
GameObject:
m_ObjectHideFlags: 0
Expand Down Expand Up @@ -372,3 +416,4 @@ SceneRoots:
- {fileID: 1249362804}
- {fileID: 1890540587}
- {fileID: 357009554}
- {fileID: 1794166376}
27 changes: 25 additions & 2 deletions Assets/UniDI/Scripts/Resolvers/MethodResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class MethodResolver
private readonly ParameterTypesProvider _parameterTypesProvider;
private readonly MethodInfo _baseGetParameterMethod;
private readonly Dictionary<MethodInfo, object[]> _methodParametersMap = new();
private readonly Dictionary<int, Dictionary<MethodInfo, object[]>> _methodIdParametersMap = new();
private readonly object[] _tempResolveParams = new object[1];

internal MethodResolver(ProvidersDto providersDto, BindingFlags flags)
Expand All @@ -39,9 +40,31 @@ internal void Resolve(object consumer, Type consumerType, int? id = null)
}
}

internal void ClearGlobalCache()
{
_methodParametersMap.Clear();
}

internal void ClearLocalCache()
{
_methodIdParametersMap.Clear();
}

internal void ClearLocalCache(int id)
{
_methodIdParametersMap.Remove(id);
}

private void ResolveMethod(object consumer, MethodInfo methodInfo, int? id = null)
{
if (!_methodParametersMap.TryGetValue(methodInfo, out object[] methodParameters))
if (id != null && !_methodIdParametersMap.ContainsKey(id.Value))
{
_methodIdParametersMap.Add(id.Value, new Dictionary<MethodInfo, object[]>());
}

var map = id == null ? _methodParametersMap : _methodIdParametersMap[id.Value];

if (!map.TryGetValue(methodInfo, out object[] methodParameters))
{
var injectedParameterTypes = _parameterTypesProvider.GetParameterTypes(methodInfo);
methodParameters = new object[injectedParameterTypes.Length];
Expand All @@ -52,7 +75,7 @@ private void ResolveMethod(object consumer, MethodInfo methodInfo, int? id = nul
var getInstance = _genericMethodsProvider.GetParameterInstanceMethod(_baseGetParameterMethod, injectedParameterTypes[i]);
methodParameters[i] = getInstance.Invoke(this, _tempResolveParams);
}
_methodParametersMap.Add(methodInfo, methodParameters);
map.Add(methodInfo, methodParameters);
}
methodInfo.Invoke(consumer, methodParameters);
}
Expand Down
15 changes: 15 additions & 0 deletions Assets/UniDI/Scripts/Strategies/ResolvingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,20 @@ internal void Resolve(object consumer, int? id = null)
_propertyResolver.Resolve(consumer, consumerType, id);
_methodResolver.Resolve(consumer, consumerType, id);
}

internal void ClearGlobalCache()
{
_methodResolver.ClearGlobalCache();
}

internal void ClearLocalCache()
{
_methodResolver.ClearLocalCache();
}

internal void ClearLocalCache(int id)
{
_methodResolver.ClearLocalCache(id);
}
}
}
4 changes: 4 additions & 0 deletions Assets/UniDI/Scripts/UniDIContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,20 @@ internal void Inject<T>(T injected, int id, Lifetime lifetime)
internal void ReleaseDependency(Type type)
{
_instancesProvider.ClearInstances(type);
_resolvingStrategy.ClearGlobalCache();
}

internal void ReleaseDependency(Type type, int id, bool clearFullScope)
{
_instancesProvider.ClearInstancesById(type, id, clearFullScope);
_resolvingStrategy.ClearLocalCache(id);
}

private void OnActiveSceneChanged(Scene current, Scene next)
{
_instancesProvider.ClearSceneInstances();
_resolvingStrategy.ClearGlobalCache();
_resolvingStrategy.ClearLocalCache();
}
}
}
2 changes: 1 addition & 1 deletion Assets/UniDI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "com.arty-f.unidi",
"displayName": "UniDI",
"author": { "name": "arty-F" },
"version": "1.1.0",
"version": "1.1.1",
"unity": "2021.3",
"description": "Fast and easy Unity DI library.",
"keywords": [ "DI", "Dependency Injection", "DI Container" ],
Expand Down
8 changes: 8 additions & 0 deletions Assets/UniDITestScripts/LocalScopeTests.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions Assets/UniDITestScripts/LocalScopeTests/LocalConsumerClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace UniDI.Test
{
public class LocalConsumerClass
{
public LocalInjectedClass LocalInjectedClass;

[Inject]
private void Init(LocalInjectedClass localInjectedClass)
{
LocalInjectedClass = localInjectedClass;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Assets/UniDITestScripts/LocalScopeTests/LocalInjectedClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace UniDI.Test
{
public class LocalInjectedClass
{
public int Value;

public LocalInjectedClass(int value)
{
Value = value;
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions Assets/UniDITestScripts/LocalScopeTests/LocalScopeTester.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using UnityEngine;

namespace UniDI.Test
{
public class LocalScopeTester : MonoBehaviour
{
private void Start()
{
var injected1 = new LocalInjectedClass(1);
var injected2 = new LocalInjectedClass(2);
injected1.Inject(1);
injected2.Inject(2);

var consumer1 = new LocalConsumerClass();
var consumer2 = new LocalConsumerClass();
consumer1.Resolve(1);
consumer2.Resolve(2);

Debug.Log($"consumer1: {consumer1.LocalInjectedClass.Value}");
Debug.Log($"consumer2: {consumer2.LocalInjectedClass.Value}");
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Assets.UniDITestScripts;
using System.Collections;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
Expand Down
6 changes: 2 additions & 4 deletions Assets/UniDITestScripts/PerformanceTests/PerformanceTester.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using UniDI;
using UniDI.Test;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Diagnostics;
using UnityEngine;

namespace Assets.UniDITestScripts
namespace UniDI.Test
{
public class PerformanceTester : MonoBehaviour
{
Expand Down

0 comments on commit 604f2cf

Please sign in to comment.