Skip to content

Commit a852c7c

Browse files
2 parents e53225e + daacaf1 commit a852c7c

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

.github/README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Reflex is an [Dependency Injection](https://stackify.com/dependency-injection/)
3535
- [Scopes](#-scopes)
3636
- [Bindings](#-bindings)
3737
- [Resolving](#-resolving)
38+
- [Selective Resolution](#-selective-resolution)
3839
- [Callbacks](#-callbacks)
3940
- [Attributes](#-attributes)
4041
- [Manual Injection](#-manual-injection)
@@ -407,6 +408,67 @@ private void Documentation_Bindings()
407408
}
408409
```
409410

411+
---
412+
## 🍒 Selective Resolution
413+
Selective Resolution is the technique of resolving a specific dependency or implementation **usually** using a composite key made of a `string` identifier and a `type`. It allows fine-grained control over which binding to use in contexts where multiple bindings of the same type exist.
414+
Reflex offers the flexibility to achieve the same functionality using its existing features, without the need to rely on builder methods like `WithId` and attributes such as `[Inject(Id = "FooId")]`, as seen in other DI frameworks.
415+
Here's an example:
416+
```cs
417+
using NUnit.Framework;
418+
using Reflex.Core;
419+
using UnityEngine;
420+
421+
namespace Reflex.EditModeTests
422+
{
423+
public class TypedInstance<T>
424+
{
425+
private readonly T _value;
426+
protected TypedInstance(T value) => _value = value;
427+
public static implicit operator T(TypedInstance<T> typedInstance) => typedInstance._value;
428+
}
429+
430+
public class AppName : TypedInstance<string>
431+
{
432+
public AppName(string value): base(value) {}
433+
}
434+
435+
public class AppVersion : TypedInstance<string>
436+
{
437+
public AppVersion(string value): base(value) {}
438+
}
439+
440+
public class AppWindow
441+
{
442+
private readonly string _appName;
443+
private readonly string _appVersion;
444+
445+
public AppWindow(AppName appName, AppVersion appVersion)
446+
{
447+
_appName = appName;
448+
_appVersion = appVersion;
449+
}
450+
451+
public void Present() => Debug.Log($"Hello from {_appName} version: {_appVersion}");
452+
}
453+
454+
public class SelectiveBindingTests
455+
{
456+
[Test]
457+
public void TestSelectiveBinding()
458+
{
459+
var container = new ContainerBuilder()
460+
.AddSingleton(typeof (AppWindow))
461+
.AddSingleton(new AppVersion("0.9"))
462+
.AddSingleton(new AppName("MyHelloWorldConsoleApp"))
463+
.Build();
464+
465+
var appWindow = container.Resolve <AppWindow>();
466+
appWindow.Present();
467+
}
468+
}
469+
}
470+
```
471+
410472
---
411473

412474
## 🪝 Callbacks
@@ -529,6 +591,7 @@ It can be created by asset menu item Assets → Create → Reflex → Settings.
529591
- logging verbosity is configured in this asset, and default value is set to `Info`
530592
- the list of ProjectScopes is also configured in this asset, and default value is empty
531593

594+
> [!IMPORTANT]
532595
> ReflexSettings asset is obligatory to have
533596
534597
---

0 commit comments

Comments
 (0)