Skip to content

Commit

Permalink
#69 Feature request: validate scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
NikolayPianikov committed Aug 20, 2024
1 parent 9cfe08e commit 44263b8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/Pure.DI.Core/Components/Api.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,18 @@ internal enum Lifetime
/// </example>
/// </summary>
Transient,

/// <summary>
/// Does not guarantee that there will be a single instance of the dependency for each root of the composition, but is useful to reduce the number of instances of type.
/// Ensures that there will be a single instance of the dependency for each composition.
/// <example>
/// <code>
/// DI.Setup("Composition")
/// .Bind&lt;IDependency&gt;().As(Lifetime.PerBlock).To&lt;Dependency&gt;();
/// .Bind&lt;IDependency&gt;().As(Lifetime.Singleton).To&lt;Dependency&gt;();
/// </code>
/// </example>
/// </summary>
PerBlock,
Singleton,

/// <summary>
/// Guarantees that there will be a single instance of the dependency for each root of the composition.
/// <example>
Expand All @@ -78,26 +78,26 @@ internal enum Lifetime
PerResolve,

/// <summary>
/// Ensures that there will be a single instance of the dependency for each scope.
/// Does not guarantee that there will be a single instance of the dependency for each root of the composition, but is useful to reduce the number of instances of type.
/// <example>
/// <code>
/// DI.Setup("Composition")
/// .Bind&lt;IDependency&gt;().As(Lifetime.Singleton).To&lt;Dependency&gt;();
/// .Bind&lt;IDependency&gt;().As(Lifetime.PerBlock).To&lt;Dependency&gt;();
/// </code>
/// </example>
/// </summary>
Scoped,
PerBlock,

/// <summary>
/// Ensures that there will be a single instance of the dependency for each composition.
/// Ensures that there will be a single instance of the dependency for each scope.
/// <example>
/// <code>
/// DI.Setup("Composition")
/// .Bind&lt;IDependency&gt;().As(Lifetime.Singleton).To&lt;Dependency&gt;();
/// </code>
/// </example>
/// </summary>
Singleton
Scoped
}

/// <summary>
Expand Down
13 changes: 12 additions & 1 deletion src/Pure.DI.Core/Core/LifetimesValidatorVisitor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ internal class LifetimesValidatorVisitor(
ILogger<LifetimesValidatorVisitor> logger)
: IPathVisitor<HashSet<object>>
{
private static readonly List<Lifetime> LifetimesByPriority = [Lifetime.Transient, Lifetime.PerBlock, Lifetime.PerResolve, Lifetime.Scoped, Lifetime.Singleton];
private static readonly int[] LifetimePriorities = new int[(int)LifetimesByPriority.Max() + 1];

static LifetimesValidatorVisitor()
{
for (var priority = 0; priority < LifetimesByPriority.Count; priority++)
{
LifetimePriorities[(int)LifetimesByPriority[priority]] = priority;
}
}

public bool Visit(HashSet<object> errors, in ImmutableArray<DependencyNode> path)
{
var actualTargetLifetimeNode = path[0];
Expand All @@ -18,7 +29,7 @@ public bool Visit(HashSet<object> errors, in ImmutableArray<DependencyNode> path
}
}

if (dependencyNode.Lifetime >= actualTargetLifetimeNode.Lifetime)
if (LifetimePriorities[(int)dependencyNode.Lifetime] >= LifetimePriorities[(int)actualTargetLifetimeNode.Lifetime])
{
actualTargetLifetimeNode = dependencyNode;
}
Expand Down

0 comments on commit 44263b8

Please sign in to comment.