@@ -71,22 +71,22 @@ namespace DryIoc
71
71
public sealed partial class Container : IContainer
72
72
{
73
73
/// <summary>Creates new container with default rules <see cref="DryIoc.Rules.Default"/>.</summary>
74
- public Container() : this(Rules.Default, Ref.Of(Registry.Default), NewSingletonScope())
74
+ public Container(bool preferInterpretation = false ) : this(Rules.Default, Ref.Of(Registry.Default), NewSingletonScope(), preferInterpretation: preferInterpretation )
75
75
{ }
76
76
77
77
/// <summary>Creates new container, optionally providing <see cref="Rules"/> to modify default container behavior.</summary>
78
78
/// <param name="rules">(optional) Rules to modify container default resolution behavior.
79
79
/// If not specified, then <see cref="DryIoc.Rules.Default"/> will be used.</param>
80
80
/// <param name="scopeContext">(optional) Scope context to use for scoped reuse.</param>
81
- public Container(Rules rules = null, IScopeContext scopeContext = null)
82
- : this(rules ?? Rules.Default, Ref.Of(Registry.Default), NewSingletonScope(), scopeContext)
81
+ public Container(Rules rules = null, IScopeContext scopeContext = null, bool preferInterpretation = false )
82
+ : this(rules ?? Rules.Default, Ref.Of(Registry.Default), NewSingletonScope(), scopeContext, preferInterpretation: preferInterpretation )
83
83
{ }
84
84
85
85
/// <summary>Creates new container with configured rules.</summary>
86
86
/// <param name="configure">Allows to modify <see cref="DryIoc.Rules.Default"/> rules.</param>
87
87
/// <param name="scopeContext">(optional) Scope context to use for <see cref="Reuse.InCurrentScope"/>.</param>
88
- public Container(Func<Rules, Rules> configure, IScopeContext scopeContext = null)
89
- : this(configure.ThrowIfNull()(Rules.Default) ?? Rules.Default, scopeContext)
88
+ public Container(Func<Rules, Rules> configure, IScopeContext scopeContext = null, bool preferInterpretation = false )
89
+ : this(configure.ThrowIfNull()(Rules.Default) ?? Rules.Default, scopeContext, preferInterpretation: preferInterpretation )
90
90
{ }
91
91
92
92
/// <summary>Helper to create singleton scope</summary>
@@ -1417,6 +1417,10 @@ private Factory GetWrapperFactoryOrDefault(Request request)
1417
1417
private readonly IResolverContext _root;
1418
1418
private readonly IResolverContext _parent;
1419
1419
1420
+ private readonly bool _preferInterpretation;
1421
+
1422
+ internal bool PreferInterpretation => _preferInterpretation;
1423
+
1420
1424
internal sealed class InstanceFactory : Factory
1421
1425
{
1422
1426
public override Type ImplementationType { get; }
@@ -1927,7 +1931,8 @@ public Registry WithNoMoreRegistrationAllowed(bool ignoreInsteadOfThrow) =>
1927
1931
private Container(Rules rules, Ref<Registry> registry, IScope singletonScope,
1928
1932
IScopeContext scopeContext = null, IScope ownCurrentScope = null,
1929
1933
int disposed = 0, StackTrace disposeStackTrace = null,
1930
- IResolverContext parent = null, IResolverContext root = null)
1934
+ IResolverContext parent = null, IResolverContext root = null,
1935
+ bool preferInterpretation = false)
1931
1936
{
1932
1937
Rules = rules;
1933
1938
@@ -1942,6 +1947,7 @@ private Container(Rules rules, Ref<Registry> registry, IScope singletonScope,
1942
1947
1943
1948
_parent = parent;
1944
1949
_root = root;
1950
+ _preferInterpretation = preferInterpretation;
1945
1951
}
1946
1952
1947
1953
#endregion
@@ -2022,7 +2028,7 @@ public static FactoryDelegateExpr WrapInFactoryExpression(this Expr expression)
2022
2028
2023
2029
/// <summary>First wraps the input service expression into lambda expression and
2024
2030
/// then compiles lambda expression to actual <see cref="FactoryDelegate"/> used for service resolution.</summary>
2025
- public static FactoryDelegate CompileToFactoryDelegate(this Expr expression)
2031
+ public static FactoryDelegate CompileToFactoryDelegate(this Expr expression, bool preferInterpretation = false )
2026
2032
{
2027
2033
expression = expression.NormalizeExpression();
2028
2034
@@ -2046,8 +2052,8 @@ public static FactoryDelegate CompileToFactoryDelegate(this Expr expression)
2046
2052
return lambdaExpr.ToLambdaExpression().Compile();
2047
2053
#else
2048
2054
// Passing preferInterpretation: true as the default value of False uses JIT compile which has a huge impact on Functions cold start time
2049
- // This optimization is needed for Functions Host only and is not part of the DryIoc codebase which it was copied from.
2050
- return lambdaExpr.Compile(preferInterpretation: true );
2055
+ // This optimization is needed for Functions Host only and is not part of the DryIoc codebase which it was copied from.
2056
+ return lambdaExpr.Compile(preferInterpretation: preferInterpretation );
2051
2057
#endif
2052
2058
}
2053
2059
@@ -6574,7 +6580,8 @@ reuse is SingletonReuse && request.Rules.EagerCachingSingletonForFasterAccess &&
6574
6580
!request.TracksTransientDisposable &&
6575
6581
!request.IsWrappedInFunc())
6576
6582
{
6577
- var factoryDelegate = serviceExpr.CompileToFactoryDelegate();
6583
+ var container = request.Container as Container;
6584
+ var factoryDelegate = serviceExpr.CompileToFactoryDelegate(container != null ? container.PreferInterpretation : false);
6578
6585
var factory = factoryDelegate;
6579
6586
6580
6587
if (Setup.WeaklyReferenced)
0 commit comments