Skip to content

Commit 710f87b

Browse files
author
Alexander Plotnikov
committed
Release 2021.9.21
1 parent a4173e0 commit 710f87b

13 files changed

+719
-12
lines changed

.gitignore

+485-12
Large diffs are not rendered by default.

MicroLibs.Moeda.asmdef

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "MicroLibs.Moeda",
3+
"rootNamespace": "MicroLibs.Moeda",
4+
"references": [],
5+
"includePlatforms": [],
6+
"excludePlatforms": [],
7+
"allowUnsafeCode": false,
8+
"overrideReferences": false,
9+
"precompiledReferences": [],
10+
"autoReferenced": true,
11+
"defineConstraints": [],
12+
"versionDefines": [],
13+
"noEngineReferences": false
14+
}

Source/Factories/Factory.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
3+
namespace MicroLibs.Moeda.Factories
4+
{
5+
internal class Factory<TResult> : IFactory<TResult>
6+
{
7+
private readonly Func<IScope, TResult> _func;
8+
9+
public Factory(Func<IScope, TResult> func) {
10+
_func = func;
11+
}
12+
13+
public virtual TResult Get(IScope scope)
14+
{
15+
return _func.Invoke(scope);
16+
}
17+
}
18+
}

Source/Factories/IFactory.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace MicroLibs.Moeda.Factories
2+
{
3+
internal interface IFactory<TResult> {
4+
5+
TResult Get(IScope scope);
6+
7+
}
8+
}

Source/Factories/SingleFactory.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
3+
namespace MicroLibs.Moeda.Factories
4+
{
5+
internal class SingleFactory<TResult> : Factory<TResult>
6+
{
7+
private TResult _value;
8+
9+
public SingleFactory(Func<IScope, TResult> func) : base(func) {
10+
11+
}
12+
public override TResult Get(IScope scope)
13+
{
14+
if(_value == null) {
15+
_value = base.Get(scope);
16+
}
17+
return _value;
18+
}
19+
}
20+
}

Source/IScope.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace MicroLibs.Moeda
2+
{
3+
public interface IScope
4+
{
5+
public TResult Get<TResult>();
6+
}
7+
}

Source/IndexKeyUtils.cs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using MicroLibs.Moeda.Qualifiers;
3+
4+
namespace MicroLibs.Moeda
5+
{
6+
internal static class IndexKeyUtils
7+
{
8+
public static string CreateKey(Type type, IQualifier qualifier)
9+
{
10+
var qualifierValue = qualifier != null ? $":q:{qualifier.Value}" : "";
11+
return $"t:{type.FullName}{qualifierValue}";
12+
}
13+
}
14+
}

Source/Module.cs

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using MicroLibs.Moeda.Factories;
4+
using MicroLibs.Moeda.Qualifiers;
5+
6+
namespace MicroLibs.Moeda
7+
{
8+
public class Module {
9+
10+
private readonly Dictionary<string, object> _dictionary = new Dictionary<string, object>();
11+
12+
public void Single<TResult>(Func<IScope, TResult> action, IQualifier qualifier = null)
13+
{
14+
var key = CreateKey(typeof(TResult), qualifier);
15+
_dictionary[key] = new SingleFactory<TResult>(action);
16+
}
17+
18+
public void Factory<TResult>(Func<IScope, TResult> action, IQualifier qualifier = null) {
19+
var key = CreateKey(typeof(TResult), qualifier);
20+
_dictionary[key] = new Factory<TResult>(action);
21+
}
22+
23+
internal Dictionary<string, object> GetDictionary()
24+
{
25+
return _dictionary;
26+
}
27+
28+
private static string CreateKey(Type type, IQualifier qualifier)
29+
{
30+
return IndexKeyUtils.CreateKey(type, qualifier);
31+
}
32+
}
33+
}

Source/MoedaApp.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using MicroLibs.Moeda.Factories;
4+
using MicroLibs.Moeda.Qualifiers;
5+
using UnityEngine;
6+
7+
namespace MicroLibs.Moeda
8+
{
9+
public class MoedaApp
10+
{
11+
12+
private readonly Dictionary<string, object> _dictionary = new Dictionary<string, object>();
13+
private readonly Dictionary<Type, Module> _modules = new Dictionary<Type, Module>();
14+
15+
private readonly IScope _scope;
16+
17+
public MoedaApp()
18+
{
19+
_scope = new Scope(this);
20+
}
21+
22+
public void Load(Module module)
23+
{
24+
foreach (var value in module.GetDictionary())
25+
{
26+
if (_dictionary.ContainsKey(value.Key))
27+
{
28+
throw new Exception($"Duplicate object {value.Key}");
29+
}
30+
31+
_dictionary[value.Key] = value.Value;
32+
}
33+
34+
_modules[module.GetType()] = module;
35+
}
36+
37+
public void Unload<T>() where T: Module
38+
{
39+
var module = _modules[typeof(T)];
40+
if (module == null)
41+
{
42+
Debug.Log($"Not found {typeof(T)}");
43+
return;
44+
}
45+
foreach (var value in module.GetDictionary())
46+
{
47+
_dictionary.Remove(value.Key);
48+
}
49+
}
50+
51+
public TResult Inject<TResult>(IQualifier qualifier = null) {
52+
return Get<TResult>(qualifier);
53+
}
54+
55+
public Lazy<TResult> InjectLazy<TResult>(IQualifier qualifier = null) {
56+
return new Lazy<TResult>(() => Get<TResult>(qualifier));
57+
}
58+
59+
internal TResult Get<TResult>(IQualifier qualifier = null)
60+
{
61+
var key = CreateKey(typeof(TResult), qualifier);
62+
return ((IFactory<TResult>)_dictionary[key]).Get(_scope);
63+
}
64+
65+
private static string CreateKey(Type type, IQualifier qualifier)
66+
{
67+
return IndexKeyUtils.CreateKey(type, qualifier);
68+
}
69+
}
70+
}

Source/Qualifiers/IQualifier.cs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace MicroLibs.Moeda.Qualifiers
2+
{
3+
public interface IQualifier
4+
{
5+
public string Value { get; }
6+
}
7+
}

Source/Qualifiers/Qualifier.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace MicroLibs.Moeda.Qualifiers
2+
{
3+
public class Qualifier : IQualifier
4+
{
5+
public string Value { get; }
6+
7+
public Qualifier(string value)
8+
{
9+
Value = value;
10+
}
11+
}
12+
}

Source/Scope.cs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace MicroLibs.Moeda
2+
{
3+
public class Scope : IScope
4+
{
5+
private readonly MoedaApp _moedaApp;
6+
7+
public Scope(MoedaApp moedaApp)
8+
{
9+
_moedaApp = moedaApp;
10+
}
11+
12+
13+
public TResult Get<TResult>()
14+
{
15+
return _moedaApp.Get<TResult>();
16+
}
17+
}
18+
}

package.json

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "microlibs.moeda",
3+
"author": "Alexander Plotnikov",
4+
"displayName": "Moeda",
5+
"description": "Dependency Injection Framework",
6+
"unity": "2020.3",
7+
"version": "2021.9.21",
8+
"keywords": [
9+
"DI",
10+
"Moeda"
11+
],
12+
"dependencies": {}
13+
}

0 commit comments

Comments
 (0)