-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
181 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using System.Linq.Expressions; | ||
using Stashbox.Registration; | ||
using Stashbox.Resolution; | ||
|
||
namespace Stashbox.Lifetime; | ||
|
||
internal class AutoLifetime : LifetimeDescriptor | ||
{ | ||
private LifetimeDescriptor selectedLifetime; | ||
|
||
internal override bool StoreResultInLocalVariable => this.selectedLifetime.StoreResultInLocalVariable; | ||
|
||
protected internal override int LifeSpan => this.selectedLifetime.LifeSpan; | ||
|
||
public AutoLifetime(LifetimeDescriptor boundaryLifetime) | ||
{ | ||
this.selectedLifetime = boundaryLifetime; | ||
} | ||
|
||
private protected override Expression? BuildLifetimeAppliedExpression(ServiceRegistration serviceRegistration, | ||
ResolutionContext resolutionContext, TypeInformation typeInformation) | ||
{ | ||
var tracker = new ResolutionContext.AutoLifetimeTracker(); | ||
var context = resolutionContext.BeginAutoLifetimeTrackingContext(tracker); | ||
var expression = GetExpressionForRegistration(serviceRegistration, context, typeInformation); | ||
this.selectedLifetime = tracker.HighestRankingLifetime.LifeSpan <= this.selectedLifetime.LifeSpan | ||
? tracker.HighestRankingLifetime | ||
: this.selectedLifetime; | ||
|
||
var func = expression?.CompileDelegate(context, context.CurrentContainerContext.ContainerConfiguration); | ||
if (func == null) return null; | ||
|
||
var final = Expression.Invoke(func.AsConstant(), context.CurrentScopeParameter, context.RequestContextParameter); | ||
|
||
return this.selectedLifetime.ApplyLifetimeToExpression(final, serviceRegistration, resolutionContext, typeInformation); | ||
} | ||
|
||
internal override Expression? ApplyLifetimeToExpression(Expression? expression, | ||
ServiceRegistration serviceRegistration, | ||
ResolutionContext resolutionContext, TypeInformation typeInformation) => | ||
this.selectedLifetime.ApplyLifetimeToExpression(expression, serviceRegistration, resolutionContext, typeInformation); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.