Skip to content

Commit

Permalink
Fix name comparison during named scope resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Dec 6, 2022
1 parent 7817591 commit aa0beec
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- `WhenResolutionPathHas()` & `WhenInResolutionPathOf()` registration options for handling more conditional resolution cases. They extend the original *parent type* and *attribute* conditions with inheritance.

### Fixed
- Name comparison during named scope resolution.

## [v5.5.3] - 2022-11-29
### Fixed
- `IsRegistered()` produced falsy results on requests with dynamically constructed string service names.
Expand Down
2 changes: 1 addition & 1 deletion src/Lifetime/NamedScopeLifetime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ private static object GetScopedValue(IResolutionScope currentScope, IRequestCont
Func<IResolutionScope, IRequestContext, object> factory, Type serviceType, int scopeId, object scopeName)
{
var scope = currentScope;
while (scope != null && scope.Name != scopeName)
while (scope != null && !scopeName.Equals(scope.Name))
scope = scope.ParentScope;

if (scope == null)
Expand Down
2 changes: 1 addition & 1 deletion src/Registration/SelectionRules/ScopeNameRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public bool IsValidForCurrentRequest(TypeInformation typeInformation,
if (resolutionContext.ScopeNames.Length == 0)
return false;

shouldIncrementWeight = resolutionContext.ScopeNames.First() == namedScopeLifetime.ScopeName;
shouldIncrementWeight = resolutionContext.ScopeNames.First().Equals(namedScopeLifetime.ScopeName);
return resolutionContext.ScopeNames.Contains(namedScopeLifetime.ScopeName);
}
}
Expand Down
5 changes: 3 additions & 2 deletions test/NamedScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ public class NamedScopeTests
[ClassData(typeof(CompilerTypeTestData))]
public void NamedScope_Simple_Resolve_Prefer_Named(CompilerType compilerType)
{
var name = "A".ToLower();
var inst = new StashboxContainer(config => config.WithCompiler(compilerType))
.Register<ITest, Test11>()
.Register<ITest, Test>(config => config.InNamedScope("A"))
.Register<ITest, Test>(config => config.InNamedScope(name))
.Register<ITest, Test1>()
.BeginScope("A")
.BeginScope(name)
.Resolve<ITest>();

Assert.IsType<Test>(inst);
Expand Down

0 comments on commit aa0beec

Please sign in to comment.