agoda-com / agoda-com/Agoda.IoC

Singleton with multiple interfaces - idiosyncrasies depending on underlying container

Offen
#23 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
enhancement
Vorherrschende Sprache
C#
Sterne
38
Forks
10
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

# Feature request

## Type

- [ ] - Enhancement - completely new feature
- [x] - Improvement - make what we have better

## Is your feature request related to a problem?

I have web api that was using Unity container with Agoda.IoC on top of it. I was playing around with it, trying to get rid of Unity in favor of the AspNetCore DI. Since we're talking small api, this was fairly easy to do, but some of my integration tests became flaky after this process. Turns out I have a service class, that implements, and is registered into the DI multiple interfaces.

It looks like that when this scenario is used with unity, it will resolve the same instance for each registered interface - this seems to be the desired behavior, since there is even a test for it:
https://github.com/agoda-com/Agoda.IoC/blob/2e0c4981b76023fb545bc8c4727afabe0b5da9a3/src/Agoda.IoC.Unity.UnitTests/UnityContainerAttributeExtensionsTest.cs#L344-L354

Turns out that my application depends on this behavior, and this works differently if the underlying container is the default asp.net core container. A quick test in `MicrosoftExtensionsDependencyInjectionAutowireTests` seems to prove this (note that the passing test condition is `AreNotSame()` in this case):
image

## Describe the solution you'd like

Depending whether we want the annotations to behave in the same way regardless of the underlying container, we should either change the default behavior of these implementations to be aligned, or discuss whether this is even a scenario that we want to support in case of .Net DI. If we want to support both methods, we can come up with an "overloaded syntax", something like:
```csharp
[RegisterSingleton(For = new[]{typeof(IMultipleAttributes1), typeof(IMultipleAttributes2) })]
```

## Additional context

Actually this is a little tricky to do with aspnet di as it is not supported out of box, but not impossible. See https://github.com/aspnet/DependencyInjection/issues/360

Beitragsleitfaden

Beitragsleitfaden öffnen

Bewertung

Dieses Issue wurde noch nicht bewertet.

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.