@@ -5253,18 +5253,25 @@ public DynamicRegistration(Factory factory,
5253
5253
/// <summary> Defines resolution/registration rules associated with Container instance. They may be different for different containers.</summary>
5254
5254
public sealed class Rules
5255
5255
{
5256
- /// Default rules as staring point.
5256
+ /// <summary> Default rules as a staring point.</summary>
5257
5257
public static readonly Rules Default = new Rules();
5258
5258
5259
- /// Default rules as staring point.
5260
- public static readonly Rules MicrosoftDependencyInjectionRules = new Rules(
5261
- (DEFAULT_SETTINGS | Settings.TrackingDisposableTransients)
5262
- & ~Settings.ThrowOnRegisteringDisposableTransient & ~Settings.VariantGenericTypesInResolvedCollection,
5263
- Rules.SelectLastRegisteredFactory(), Reuse.Transient,
5264
- Made.Of(DryIoc.FactoryMethod.ConstructorWithResolvableArguments),
5265
- IfAlreadyRegistered.AppendNotKeyed,
5266
- DefaultDependencyCountInLambdaToSplitBigObjectGraph, null, null, null, null, null);
5267
-
5259
+ private static Rules SetMicrosoftDependencyInjectionRules(Rules rules)
5260
+ {
5261
+ rules._settings |= Settings.TrackingDisposableTransients;
5262
+ rules._settings &= ~Settings.ThrowOnRegisteringDisposableTransient;
5263
+ rules._settings &= ~Settings.VariantGenericTypesInResolvedCollection;
5264
+ rules._factorySelector = SelectLastRegisteredFactory;
5265
+ rules._made._factoryMethod = DryIoc.FactoryMethod.ConstructorWithResolvableArguments;
5266
+ return rules;
5267
+ }
5268
+
5269
+ /// <summary>The rules implementing the conventions of Microsoft.Extension.DependencyInjection library.</summary>
5270
+ public static readonly Rules MicrosoftDependencyInjectionRules = SetMicrosoftDependencyInjectionRules(Default.Clone());
5271
+
5272
+ /// <summary>Returns the copy of the rules with the applied conventions of Microsoft.Extension.DependencyInjection library.</summary>
5273
+ public Rules WithMicrosoftDependencyInjectionRules() => SetMicrosoftDependencyInjectionRules(Clone());
5274
+
5268
5275
/// <summary>Does nothing</summary>
5269
5276
[Obsolete("Is not used anymore to split the graph - instead use the `DependencyCountInLambdaToSplitBigObjectGraph`")]
5270
5277
public const int DefaultDependencyDepthToSplitObjectGraph = 20;
@@ -5385,7 +5392,7 @@ public Rules WithDefaultRegistrationServiceKey(object serviceKey) =>
5385
5392
/// <summary>Rules to select single matched factory default and keyed registered factory/factories.
5386
5393
/// Selectors applied in specified array order, until first returns not null <see cref="Factory"/>.
5387
5394
/// Default behavior is to throw on multiple registered default factories, cause it is not obvious what to use.</summary>
5388
- public FactorySelectorRule FactorySelector { get; }
5395
+ public FactorySelectorRule FactorySelector => _factorySelector;
5389
5396
5390
5397
/// <summary>Sets <see cref="FactorySelector"/></summary>
5391
5398
public Rules WithFactorySelector(FactorySelectorRule rule) =>
@@ -5590,7 +5597,7 @@ public static DynamicRegistrationProvider AutoFallbackDynamicRegistrations(
5590
5597
5591
5598
// We nullify default keys (usually passed by ResolveMany to resolve the specific factory in order)
5592
5599
// so that `CombineRegisteredWithDynamicFactories` may assign the key again.
5593
- // Given that the implementation types are unchanged then the new keys assignement will be the same the last one,
5600
+ // Given that the implementation types are unchanged then the new keys assignment will be the same the last one,
5594
5601
// so that the factory resolution will correctly match the required factory by key.
5595
5602
// e.g. bitbucket issue #396
5596
5603
var theKey = serviceKey is DefaultDynamicKey ? null : serviceKey;
@@ -5914,7 +5921,7 @@ private Rules(Settings settings,
5914
5921
{
5915
5922
_settings = settings;
5916
5923
_made = made;
5917
- FactorySelector = factorySelector;
5924
+ _factorySelector = factorySelector;
5918
5925
DefaultReuse = defaultReuse;
5919
5926
DefaultIfAlreadyRegistered = defaultIfAlreadyRegistered;
5920
5927
DependencyCountInLambdaToSplitBigObjectGraph = dependencyCountInLambdaToSplitBigObjectGraph;
@@ -5925,13 +5932,20 @@ private Rules(Settings settings,
5925
5932
DefaultRegistrationServiceKey = defaultRegistrationServiceKey;
5926
5933
}
5927
5934
5928
- private Rules WithSettings(Settings newSettings ) =>
5929
- new Rules(newSettings ,
5930
- FactorySelector, DefaultReuse, _made, DefaultIfAlreadyRegistered, DependencyCountInLambdaToSplitBigObjectGraph,
5935
+ private Rules Clone( ) =>
5936
+ new Rules(_settings, FactorySelector, DefaultReuse ,
5937
+ _made.Copy() , DefaultIfAlreadyRegistered, DependencyCountInLambdaToSplitBigObjectGraph,
5931
5938
DependencyResolutionCallExprs, ItemToExpressionConverter,
5932
5939
DynamicRegistrationProviders, UnknownServiceResolvers, DefaultRegistrationServiceKey);
5933
5940
5934
- private readonly Made _made;
5941
+ private Rules WithSettings(Settings newSettings)
5942
+ {
5943
+ var newRules = Clone();
5944
+ newRules._settings = newSettings;
5945
+ return newRules;
5946
+ }
5947
+
5948
+ private Made _made;
5935
5949
5936
5950
[Flags]
5937
5951
private enum Settings
@@ -5970,8 +5984,9 @@ private const Settings DEFAULT_SETTINGS
5970
5984
| Settings.UseInterpretationForTheFirstResolution;
5971
5985
5972
5986
private Settings _settings;
5987
+ private FactorySelectorRule _factorySelector;
5973
5988
5974
- #endregion
5989
+ #endregion
5975
5990
}
5976
5991
5977
5992
/// <summary>Wraps constructor or factory method optionally with factory instance to create service.</summary>
@@ -6268,7 +6283,8 @@ internal FactoryMethod(ConstructorInfo ctor, Expression[] resolvedParameterExpre
6268
6283
public class Made
6269
6284
{
6270
6285
/// <summary>Returns delegate to select constructor based on provided request.</summary>
6271
- public FactoryMethodSelector FactoryMethod { get; private set; }
6286
+ public FactoryMethodSelector FactoryMethod { get => _factoryMethod; private set => _factoryMethod = value; }
6287
+ internal FactoryMethodSelector _factoryMethod;
6272
6288
6273
6289
/// <summary>Return type of strongly-typed factory method expression.</summary>
6274
6290
public Type FactoryMethodKnownResultType { get; private set; }
@@ -6285,7 +6301,7 @@ private enum MadeDetails
6285
6301
private readonly MadeDetails _details;
6286
6302
6287
6303
/// Has any conditional flags
6288
- public bool IsConditional => _details != MadeDetails.NoConditionals;
6304
+ public bool IsConditional => _details != MadeDetails.NoConditionals;
6289
6305
6290
6306
/// True is made has properties or parameters with custom value.
6291
6307
/// That's mean the whole made become context based which affects caching.
@@ -6337,17 +6353,15 @@ public override string ToString()
6337
6353
public static readonly Made Default = new Made();
6338
6354
6339
6355
/// <summary>Creates rules with only <see cref="FactoryMethod"/> specified.</summary>
6340
- public static implicit operator Made(FactoryMethodSelector factoryMethod) =>
6341
- Of(factoryMethod);
6356
+ public static implicit operator Made(FactoryMethodSelector factoryMethod) => new Made(factoryMethod);
6342
6357
6343
6358
/// <summary>Creates rules with only <see cref="Parameters"/> specified.</summary>
6344
- public static implicit operator Made(ParameterSelector parameters) =>
6345
- Of(parameters: parameters);
6359
+ public static implicit operator Made(ParameterSelector parameters) => new Made(null, parameters);
6346
6360
6347
6361
/// <summary>Creates rules with only <see cref="PropertiesAndFields"/> specified.</summary>
6348
- public static implicit operator Made(PropertiesAndFieldsSelector propertiesAndFields) =>
6349
- Of(propertiesAndFields: propertiesAndFields);
6362
+ public static implicit operator Made(PropertiesAndFieldsSelector propertiesAndFields) => new Made(null, null, propertiesAndFields);
6350
6363
6364
+ // todo: @bug fix the spelling for `isConditionalImlementation`
6351
6365
/// <summary>Specifies injections rules for Constructor, Parameters, Properties and Fields. If no rules specified returns <see cref="Default"/> rules.</summary>
6352
6366
public static Made Of(FactoryMethodSelector factoryMethod = null,
6353
6367
ParameterSelector parameters = null, PropertiesAndFieldsSelector propertiesAndFields = null,
@@ -6395,7 +6409,7 @@ public static Made Of(Func<Request, MemberInfo> getMethodOrMember, ServiceInfo f
6395
6409
/// Where <paramref name="getMethodOrMember"/>Method, or constructor, or member selector.</summary>
6396
6410
public static Made Of(Func<Request, MemberInfo> getMethodOrMember, Func<Request, ServiceInfo> factoryInfo,
6397
6411
ParameterSelector parameters = null, PropertiesAndFieldsSelector propertiesAndFields = null) =>
6398
- new Made(r => DryIoc.FactoryMethod.Of(getMethodOrMember(r), factoryInfo(r)),
6412
+ new Made(r => DryIoc.FactoryMethod.Of(getMethodOrMember(r), factoryInfo(r)),
6399
6413
parameters, propertiesAndFields, isImplMemberDependsOnRequest: true);
6400
6414
6401
6415
/// <summary>Defines how to select constructor from implementation type.
@@ -6526,11 +6540,11 @@ internal TypedMade(FactoryMethodSelector factoryMethod = null,
6526
6540
{ }
6527
6541
}
6528
6542
6529
- #region Implementation
6543
+ #region Implementation
6530
6544
6531
6545
internal Made(
6532
6546
FactoryMethodSelector factoryMethod = null, ParameterSelector parameters = null, PropertiesAndFieldsSelector propertiesAndFields = null,
6533
- Type factoryMethodKnownResultType = null, bool hasCustomValue = false, bool isConditionalImlementation = false,
6547
+ Type factoryMethodKnownResultType = null, bool hasCustomValue = false, bool isConditionalImlementation = false,
6534
6548
bool isImplMemberDependsOnRequest = false)
6535
6549
{
6536
6550
FactoryMethod = factoryMethod;
@@ -6554,6 +6568,20 @@ internal Made(FactoryMethod factoryMethod, Type factoryReturnType)
6554
6568
FactoryMethodKnownResultType = factoryReturnType;
6555
6569
}
6556
6570
6571
+ private Made(
6572
+ FactoryMethodSelector factoryMethod, ParameterSelector parameters, PropertiesAndFieldsSelector propertiesAndFields,
6573
+ Type factoryMethodKnownResultType, MadeDetails details)
6574
+ {
6575
+ FactoryMethod = factoryMethod;
6576
+ Parameters = parameters;
6577
+ PropertiesAndFields = propertiesAndFields;
6578
+ FactoryMethodKnownResultType = factoryMethodKnownResultType;
6579
+ _details = details;
6580
+ }
6581
+
6582
+ internal Made Copy() =>
6583
+ new Made(FactoryMethod, Parameters, PropertiesAndFields, FactoryMethodKnownResultType, _details);
6584
+
6557
6585
private static ParameterSelector ComposeParameterSelectorFromArgs(ref bool hasCustomValue,
6558
6586
System.Linq.Expressions.Expression wholeServiceExpr, ParameterInfo[] paramInfos,
6559
6587
IList<System.Linq.Expressions.Expression> argExprs,
@@ -6617,7 +6645,7 @@ private static PropertiesAndFieldsSelector ComposePropertiesAndFieldsSelector(re
6617
6645
if (methodCallExpr.Method.Name == Arg.ArgIndexMethodName) // handle custom value
6618
6646
{
6619
6647
var getArgValue = GetArgCustomValueProvider(wholeServiceExpr, methodCallExpr, argValues);
6620
- propertiesAndFields = propertiesAndFields.OverrideWith(req =>
6648
+ propertiesAndFields = propertiesAndFields.OverrideWith(req =>
6621
6649
PropertyOrFieldServiceInfo.Of(member).WithDetails(ServiceDetails.Of(getArgValue(req))).One());
6622
6650
hasCustomValue = true;
6623
6651
}
@@ -6734,7 +6762,7 @@ private static object GetArgExpressionValueOrThrow(
6734
6762
argExpr, wholeServiceExpr);
6735
6763
}
6736
6764
6737
- #endregion
6765
+ #endregion
6738
6766
}
6739
6767
6740
6768
/// <summary>Class for defining parameters/properties/fields service info in <see cref="Made"/> expressions.
0 commit comments