Skip to content

Commit

Permalink
Minor registration improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
z4kn4fein committed Oct 11, 2022
1 parent 68b829b commit acd8289
Show file tree
Hide file tree
Showing 73 changed files with 675 additions and 817 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5.4.3
5.5.0
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [v5.5.0] -
### Fixed
- Minor registration improvements.

## [v5.4.3] - 2022-09-09
### Fixed
- Named resolution using ResolveAll returns all named and unnamed instances disregarding the WithNamedDependencyResolutionForUnNamedRequests flag. [#118](https://github.com/z4kn4fein/stashbox/issues/118)
Expand Down
10 changes: 6 additions & 4 deletions src/Expressions/ExpressionBuilder.Default.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Stashbox.Exceptions;
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Utils;
using System.Collections.Generic;
using System.Linq.Expressions;

namespace Stashbox.Expressions
Expand All @@ -21,16 +22,17 @@ internal static partial class ExpressionBuilder

private static Expression? PrepareDefaultExpression(ServiceRegistration serviceRegistration, ResolutionContext resolutionContext)
{
if (serviceRegistration is ComplexRegistration complex && complex.DefinedScopeName != null)
var definedScopeName = serviceRegistration.Options.GetOrDefault(OptionIds.DefinedScopeName);
if (definedScopeName != null)
{
var variable = Constants.ResolutionScopeType.AsVariable();

var newScope = resolutionContext.CurrentScopeParameter
.CallMethod(Constants.BeginScopeMethod,
complex.DefinedScopeName.AsConstant(),
definedScopeName.AsConstant(),
true.AsConstant());

var newScopeContext = resolutionContext.BeginNewScopeContext(new ReadOnlyKeyValue<object, ParameterExpression>(complex.DefinedScopeName, variable));
var newScopeContext = resolutionContext.BeginNewScopeContext(new ReadOnlyKeyValue<object, ParameterExpression>(definedScopeName, variable));

resolutionContext.AddDefinedVariable(variable);
resolutionContext.AddInstruction(variable.AssignTo(newScope.ConvertTo(Constants.ResolutionScopeType)));
Expand Down
24 changes: 12 additions & 12 deletions src/Expressions/ExpressionBuilder.Factory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using Stashbox.Exceptions;
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using System;
using System.Collections.Generic;
Expand All @@ -9,38 +9,38 @@ namespace Stashbox.Expressions
{
internal static partial class ExpressionBuilder
{
private static Expression GetExpressionForFactory(FactoryRegistration serviceRegistration, ResolutionContext resolutionContext, Type resolveType)
private static Expression GetExpressionForFactory(ServiceRegistration serviceRegistration, FactoryOptions factoryOptions, ResolutionContext resolutionContext, Type resolveType)
{
if (resolutionContext.CircularDependencyBarrier.Contains(serviceRegistration.RegistrationId))
throw new CircularDependencyException(serviceRegistration.ImplementationType);

resolutionContext.CircularDependencyBarrier.Add(serviceRegistration.RegistrationId);

var parameters = GetFactoryParameters(serviceRegistration, resolutionContext);
var expression = ConstructFactoryExpression(serviceRegistration, parameters);
var parameters = GetFactoryParameters(factoryOptions, resolutionContext);
var expression = ConstructFactoryExpression(factoryOptions, parameters);
var result = ExpressionFactory.ConstructBuildUpExpression(serviceRegistration, resolutionContext, expression, resolveType);

resolutionContext.CircularDependencyBarrier.Pop();
return result;
}

private static Expression ConstructFactoryExpression(FactoryRegistration serviceRegistration, IEnumerable<Expression> parameters)
private static Expression ConstructFactoryExpression(FactoryOptions factoryOptions, IEnumerable<Expression> parameters)
{
if (serviceRegistration.IsFactoryDelegateACompiledLambda || serviceRegistration.Factory.IsCompiledLambda())
return serviceRegistration.Factory.InvokeDelegate(parameters);
if (factoryOptions.IsFactoryDelegateACompiledLambda || factoryOptions.Factory.IsCompiledLambda())
return factoryOptions.Factory.InvokeDelegate(parameters);

var method = serviceRegistration.Factory.GetMethod();
var method = factoryOptions.Factory.GetMethod();
return method.IsStatic
? method.CallStaticMethod(parameters)
: method.CallMethod(serviceRegistration.Factory.Target.AsConstant(), parameters);
: method.CallMethod(factoryOptions.Factory.Target.AsConstant(), parameters);
}

private static IEnumerable<Expression> GetFactoryParameters(FactoryRegistration serviceRegistration, ResolutionContext resolutionContext)
private static IEnumerable<Expression> GetFactoryParameters(FactoryOptions factoryOptions, ResolutionContext resolutionContext)
{
var length = serviceRegistration.FactoryParameters.Length;
var length = factoryOptions.FactoryParameters.Length;
for (var i = 0; i < length - 1; i++)
{
var typeInfo = new TypeInformation(serviceRegistration.FactoryParameters[i], null);
var typeInfo = new TypeInformation(factoryOptions.FactoryParameters[i], null);
yield return resolutionContext.CurrentContainerContext.ResolutionStrategy.BuildExpressionForType(resolutionContext, typeInfo).ServiceExpression;
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/Expressions/ExpressionBuilder.Func.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using System;
using System.Collections.Generic;
Expand All @@ -10,18 +10,18 @@ namespace Stashbox.Expressions
{
internal static partial class ExpressionBuilder
{
private static Expression GetExpressionForFunc(FuncRegistration serviceRegistration, ResolutionContext resolutionContext)
private static Expression GetExpressionForFunc(ServiceRegistration serviceRegistration, Delegate func, ResolutionContext resolutionContext)
{
var internalMethodInfo = serviceRegistration.FuncDelegate.GetMethod();
var internalMethodInfo = func.GetMethod();

var parameters = GetFuncParametersWithScope(serviceRegistration.ImplementationType.GetMethod("Invoke")!.GetParameters(), resolutionContext);
if (serviceRegistration.FuncDelegate.IsCompiledLambda())
return serviceRegistration.FuncDelegate.InvokeDelegate(parameters)
if (func.IsCompiledLambda())
return func.InvokeDelegate(parameters)
.AsLambda(parameters.Take(parameters.Length - 1).Cast<ParameterExpression>());

var expr = internalMethodInfo.IsStatic
? internalMethodInfo.CallStaticMethod(parameters)
: serviceRegistration.FuncDelegate.Target.AsConstant().CallMethod(internalMethodInfo, parameters);
: func.Target.AsConstant().CallMethod(internalMethodInfo, parameters);

return expr.AsLambda(parameters.Take(parameters.Length - 1).Cast<ParameterExpression>());
}
Expand Down
29 changes: 16 additions & 13 deletions src/Expressions/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using Stashbox.Lifetime;
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Utils;
using System;
using System.Linq.Expressions;
using System.Collections.Generic;

namespace Stashbox.Expressions
{
Expand All @@ -16,17 +17,17 @@ internal static partial class ExpressionBuilder
if (expression == null)
return null;

if (serviceRegistration is not InstanceRegistration && serviceRegistration is ComplexRegistration complex)
if (serviceRegistration.Options != null && !serviceRegistration.IsInstance())
{
if (complex.AsyncInitializer != null)
if (serviceRegistration.Options.TryGetValue(OptionIds.AsyncInitializer, out var asyncInitializer))
expression = resolutionContext.CurrentScopeParameter.CallMethod(Constants.AddWithAsyncInitializerMethod, expression,
complex.AsyncInitializer.AsConstant()).ConvertTo(requestedType);
asyncInitializer.AsConstant()).ConvertTo(requestedType);

if (complex.Finalizer != null)
if (serviceRegistration.Options.TryGetValue(OptionIds.Finalizer, out var finalizer))
expression = resolutionContext.CurrentScopeParameter.CallMethod(Constants.AddWithFinalizerMethod, expression,
complex.Finalizer.AsConstant()).ConvertTo(requestedType);
finalizer.AsConstant()).ConvertTo(requestedType);
}

if (!ShouldHandleDisposal(resolutionContext.CurrentContainerContext, serviceRegistration) || !expression.Type.IsDisposable())
return expression;

Expand All @@ -42,23 +43,25 @@ internal static partial class ExpressionBuilder
? resolutionContext.BeginCrossContainerContext(resolutionContext.RequestInitiatorContainerContext)
: resolutionContext;

return serviceRegistration switch
var options = serviceRegistration.Options?.GetOrDefault(OptionIds.RegistrationTypeOptions);

return options switch
{
FactoryRegistration factoryRegistration => GetExpressionForFactory(factoryRegistration, resolutionContext, requestedType),
InstanceRegistration instanceRegistration => instanceRegistration.IsWireUp
FactoryOptions factoryOptions => GetExpressionForFactory(serviceRegistration, factoryOptions, resolutionContext, requestedType),
InstanceOptions instanceRegistration => instanceRegistration.IsWireUp
? ExpressionFactory.ConstructBuildUpExpression(serviceRegistration,
resolutionContext, instanceRegistration.ExistingInstance.AsConstant(),
serviceRegistration.ImplementationType)
: instanceRegistration.ExistingInstance.AsConstant(),
FuncRegistration funcRegistration => GetExpressionForFunc(funcRegistration, resolutionContext),
Delegate func => GetExpressionForFunc(serviceRegistration, func, resolutionContext),
_ => GetExpressionForDefault(serviceRegistration, resolutionContext)
};
}

private static bool ShouldHandleDisposal(IContainerContext containerContext, ServiceRegistration serviceRegistration)
{
if (serviceRegistration is ComplexRegistration complex && complex.IsLifetimeExternallyOwned ||
serviceRegistration is InstanceRegistration)
if (serviceRegistration.Options.IsOn(OptionIds.IsLifetimeExternallyOwned) ||
serviceRegistration.IsInstance())
return false;

return containerContext.ContainerConfiguration.TrackTransientsForDisposalEnabled ||
Expand Down
5 changes: 3 additions & 2 deletions src/Expressions/ExpressionFactory.Member.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
using Stashbox.Exceptions;
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Resolution.Extensions;
using Stashbox.Utils.Data;
using System;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -40,7 +41,7 @@ private static Expression GetMemberExpression(
var memberTypeInfo = member.AsTypeInformation(serviceRegistration,
resolutionContext.CurrentContainerContext.ContainerConfiguration);

var injectionParameter = (serviceRegistration as ComplexRegistration)?.InjectionParameters?.SelectInjectionParameterOrDefault(memberTypeInfo);
var injectionParameter = serviceRegistration?.Options.GetOrDefault<ExpandableArray<KeyValuePair<string, object?>>>(OptionIds.InjectionParameters)?.SelectInjectionParameterOrDefault(memberTypeInfo);
if (injectionParameter != null) return injectionParameter;

var serviceContext = resolutionContext.CurrentContainerContext
Expand Down
8 changes: 4 additions & 4 deletions src/Expressions/ExpressionFactory.Method.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
using Stashbox.Exceptions;
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Resolution.Extensions;
using Stashbox.Utils;
using Stashbox.Utils.Data;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -26,7 +27,7 @@ private static IEnumerable<Expression> CreateParameterExpressionsForMethod(
var parameter = parameters[i].AsTypeInformation(method.DeclaringType, serviceRegistration,
resolutionContext.CurrentContainerContext.ContainerConfiguration);

var injectionParameter = (serviceRegistration as ComplexRegistration)?.InjectionParameters?.SelectInjectionParameterOrDefault(parameter);
var injectionParameter = serviceRegistration?.Options.GetOrDefault<ExpandableArray<KeyValuePair<string, object?>>>(OptionIds.InjectionParameters)?.SelectInjectionParameterOrDefault(parameter);
if (injectionParameter != null) yield return injectionParameter;

yield return resolutionContext.CurrentContainerContext.ResolutionStrategy.BuildExpressionForType(
Expand Down Expand Up @@ -136,8 +137,7 @@ private static bool TryBuildMethod(
var parameter = parameters[i].AsTypeInformation(method.DeclaringType, serviceRegistration,
resolutionContext.CurrentContainerContext.ContainerConfiguration);

var injectionParameter = (serviceRegistration as ComplexRegistration)?.InjectionParameters?.SelectInjectionParameterOrDefault(parameter);

var injectionParameter = serviceRegistration?.Options.GetOrDefault<ExpandableArray<KeyValuePair<string, object?>>>(OptionIds.InjectionParameters)?.SelectInjectionParameterOrDefault(parameter);
if (injectionParameter != null)
{
parameterExpressions[i] = injectionParameter;
Expand Down
44 changes: 23 additions & 21 deletions src/Expressions/ExpressionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Stashbox.Registration.ServiceRegistrations;
using Stashbox.Registration;
using Stashbox.Resolution;
using Stashbox.Utils.Data;
using System;
Expand All @@ -24,10 +24,10 @@ public static Expression ConstructBuildUpExpression(
var members = serviceRegistration.ImplementationType.GetUsableMembers(serviceRegistration,
resolutionContext.CurrentContainerContext.ContainerConfiguration);

var complexRegistration = serviceRegistration as ComplexRegistration;
var initializer = serviceRegistration.Options.GetOrDefault(OptionIds.Initializer);

if (members.Length == 0 && methods.Length == 0 &&
complexRegistration?.Initializer == null) return instance;
initializer == null) return instance;

var variable = instance.Type.AsVariable();
var assign = variable.AssignTo(instance);
Expand All @@ -40,9 +40,9 @@ public static Expression ConstructBuildUpExpression(
lines.AddRange(CreateMethodExpressions(methods,
serviceRegistration, resolutionContext, variable));

if (complexRegistration?.Initializer != null)
lines.Add(complexRegistration.Initializer.AsConstant()
.CallMethod(complexRegistration.Initializer.GetType().GetMethod("Invoke")!,
if (initializer != null)
lines.Add(initializer.AsConstant()
.CallMethod(initializer.GetType().GetMethod("Invoke")!,
variable, resolutionContext.CurrentScopeParameter));

lines.Add(variable.Type != serviceType ? variable.ConvertTo(serviceType) : variable);
Expand Down Expand Up @@ -101,9 +101,9 @@ public static Expression ConstructBuildUpExpression(
initExpression = initExpression.InitMembers(GetMemberBindings(members,
serviceRegistration, resolutionContext));

var complexRegistration = serviceRegistration as ComplexRegistration;
var initializer = serviceRegistration.Options.GetOrDefault(OptionIds.Initializer);

if (methods.Length == 0 && complexRegistration?.Initializer == null)
if (methods.Length == 0 && initializer == null)
return initExpression;

var variable = initExpression.Type.AsVariable();
Expand All @@ -114,9 +114,9 @@ public static Expression ConstructBuildUpExpression(
lines.AddRange(CreateMethodExpressions(methods,
serviceRegistration, resolutionContext, variable));

if (complexRegistration?.Initializer != null)
lines.Add(complexRegistration.Initializer.AsConstant()
.CallMethod(complexRegistration.Initializer.GetType().GetMethod("Invoke")!,
if (initializer != null)
lines.Add(initializer.AsConstant()
.CallMethod(initializer.GetType().GetMethod("Invoke")!,
variable, resolutionContext.CurrentScopeParameter));

lines.Add(variable);
Expand Down Expand Up @@ -163,24 +163,26 @@ public static Expression ConstructBuildUpExpression(
IEnumerable<ConstructorInfo> constructors)
{
var rule = resolutionContext.CurrentContainerContext.ContainerConfiguration.ConstructorSelectionRule;
if (serviceRegistration is ComplexRegistration complexRegistration)
var constructorOptions = serviceRegistration.Options.GetOrDefault<ConstructorOptions>(OptionIds.ConstructorOptions);
if (constructorOptions != null)
{
if (complexRegistration.SelectedConstructor != null)
if (constructorOptions.SelectedConstructor != null)
{
if (complexRegistration.ConstructorArguments != null)
return complexRegistration.SelectedConstructor
.MakeNew(complexRegistration.ConstructorArguments.Select(Expression.Constant));
if (constructorOptions.ConstructorArguments != null)
return constructorOptions.SelectedConstructor
.MakeNew(constructorOptions.ConstructorArguments.Select(Expression.Constant));

return complexRegistration.SelectedConstructor.MakeNew(
return constructorOptions.SelectedConstructor.MakeNew(
CreateParameterExpressionsForMethod(
serviceRegistration,
resolutionContext, complexRegistration.SelectedConstructor));
resolutionContext, constructorOptions.SelectedConstructor));
}

if (complexRegistration.ConstructorSelectionRule != null)
rule = complexRegistration.ConstructorSelectionRule;
}

var constructorSelectionRule = serviceRegistration.Options.GetOrDefault<Func<IEnumerable<ConstructorInfo>, IEnumerable<ConstructorInfo>>>(OptionIds.ConstructorSelectionRule);
if (constructorSelectionRule != null)
rule = constructorSelectionRule;

constructors = rule(constructors);

return SelectConstructor(typeToConstruct, serviceRegistration, resolutionContext,
Expand Down
Loading

0 comments on commit acd8289

Please sign in to comment.