Skip to content

Commit 9dd0125

Browse files
committed
Cleanup up code using newer C# syntax.
1 parent daf4cfd commit 9dd0125

File tree

6 files changed

+37
-73
lines changed

6 files changed

+37
-73
lines changed

src/SimpleInjector/ConstructorResolutionBehaviorExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ public static ConstructorInfo GetConstructor(
4040
}
4141

4242
private static ActivationException BuildActivationException(
43-
IConstructorResolutionBehavior behavior, Type implementationType, string? message) =>
44-
new ActivationException(
43+
IConstructorResolutionBehavior behavior, Type implementationType, string? message) => new(
4544
message is null || string.IsNullOrWhiteSpace(message)
4645
? StringResources.TypeHasNoInjectableConstructorAccordingToCustomResolutionBehavior(
4746
behavior, implementationType)

src/SimpleInjector/Container.Common.cs

Lines changed: 13 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -709,60 +709,36 @@ where StringComparer.OrdinalIgnoreCase.Equals(friendlyName, missingTypeDefName)
709709
select type;
710710
}
711711

712-
private sealed class ContextualResolveInterceptor
712+
private sealed class ContextualResolveInterceptor(
713+
ResolveInterceptor interceptor, Predicate<InitializationContext> predicate)
713714
{
714-
public readonly ResolveInterceptor Interceptor;
715-
public readonly Predicate<InitializationContext> Predicate;
716-
717-
public ContextualResolveInterceptor(
718-
ResolveInterceptor interceptor, Predicate<InitializationContext> predicate)
719-
{
720-
this.Interceptor = interceptor;
721-
this.Predicate = predicate;
722-
}
715+
public readonly ResolveInterceptor Interceptor = interceptor;
716+
public readonly Predicate<InitializationContext> Predicate = predicate;
723717
}
724718

725-
private sealed class TypedInstanceInitializer : IInstanceInitializer
719+
private sealed class TypedInstanceInitializer(Type serviceType, object instanceInitializer)
720+
: IInstanceInitializer
726721
{
727-
private readonly Type serviceType;
728-
private readonly object instanceInitializer;
729-
730-
private TypedInstanceInitializer(Type serviceType, object instanceInitializer)
731-
{
732-
this.serviceType = serviceType;
733-
this.instanceInitializer = instanceInitializer;
734-
}
735-
736722
public bool AppliesTo(Type implementationType, InitializerContext context) =>
737-
Types.GetTypeHierarchyFor(implementationType).Contains(this.serviceType);
723+
Types.GetTypeHierarchyFor(implementationType).Contains(serviceType);
738724

739725
public Action<T> CreateAction<T>(InitializerContext context) =>
740-
Helpers.CreateAction<T>(this.instanceInitializer);
726+
Helpers.CreateAction<T>(instanceInitializer);
741727

742728
internal static IInstanceInitializer Create<TImplementation>(Action<TImplementation> initializer)
743729
{
744730
return new TypedInstanceInitializer(typeof(TImplementation), initializer);
745731
}
746732
}
747733

748-
private sealed class ContextualInstanceInitializer : IInstanceInitializer
749-
{
750-
private readonly Predicate<InitializerContext> predicate;
751-
private readonly Action<InstanceInitializationData> instanceInitializer;
752-
753-
private ContextualInstanceInitializer(
734+
private sealed class ContextualInstanceInitializer(
754735
Predicate<InitializerContext> predicate,
755-
Action<InstanceInitializationData> instanceInitializer)
756-
{
757-
this.predicate = predicate;
758-
this.instanceInitializer = instanceInitializer;
759-
}
760-
761-
public bool AppliesTo(Type implementationType, InitializerContext context) =>
762-
this.predicate(context);
736+
Action<InstanceInitializationData> instanceInitializer) : IInstanceInitializer
737+
{
738+
public bool AppliesTo(Type implementationType, InitializerContext context) => predicate(context);
763739

764740
public Action<T> CreateAction<T>(InitializerContext context) =>
765-
instance => this.instanceInitializer(new InstanceInitializationData(context, instance!));
741+
instance => instanceInitializer(new InstanceInitializationData(context, instance!));
766742

767743
internal static IInstanceInitializer Create(
768744
Action<InstanceInitializationData> instanceInitializer,

src/SimpleInjector/Container.Registration.Batch.cs

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -687,16 +687,10 @@ private static void CollectionDoesNotContainOpenGenericTypes(
687687
}
688688
}
689689

690-
private sealed class BatchMapping
690+
private sealed class BatchMapping(Type implementationType, IEnumerable<Type> closedServiceTypes)
691691
{
692-
private BatchMapping(Type implementationType, IEnumerable<Type> closedServiceTypes)
693-
{
694-
this.ImplementationType = implementationType;
695-
this.ClosedServiceTypes = closedServiceTypes;
696-
}
697-
698-
internal Type ImplementationType { get; }
699-
internal IEnumerable<Type> ClosedServiceTypes { get; }
692+
public readonly Type ImplementationType = implementationType;
693+
public readonly IEnumerable<Type> ClosedServiceTypes = closedServiceTypes;
700694

701695
public static BatchMapping[] Build(Type openServiceType, IEnumerable<Type> implementationTypes)
702696
{
@@ -710,12 +704,9 @@ select Build(openServiceType, implemenationType))
710704
return mappings;
711705
}
712706

713-
public static BatchMapping Build(Type openServiceType, Type implementationType)
714-
{
715-
return new BatchMapping(
716-
implementationType: implementationType,
717-
closedServiceTypes: implementationType.GetBaseTypesAndInterfacesFor(openServiceType));
718-
}
707+
public static BatchMapping Build(Type openServiceType, Type implementationType) => new(
708+
implementationType,
709+
implementationType.GetBaseTypesAndInterfacesFor(openServiceType));
719710

720711
private static void RequiresNoDuplicateRegistrations(BatchMapping[] mappings)
721712
{
@@ -745,17 +736,11 @@ where serviceTypeGroup.Count() > 1
745736
}
746737
}
747738

748-
private class NonGenericTypesToRegisterForOneToOneMappingResults
739+
private sealed class NonGenericTypesToRegisterForOneToOneMappingResults(
740+
List<Type> skippedDecorators, List<Type> implementationTypes)
749741
{
750-
public NonGenericTypesToRegisterForOneToOneMappingResults(
751-
List<Type> skippedDecorators, List<Type> implementationTypes)
752-
{
753-
this.SkippedDecorators = skippedDecorators;
754-
this.ImplementationTypes = implementationTypes;
755-
}
756-
757-
public List<Type> SkippedDecorators { get; }
758-
public List<Type> ImplementationTypes { get; }
742+
public readonly List<Type> SkippedDecorators = skippedDecorators;
743+
public readonly List<Type> ImplementationTypes = implementationTypes;
759744
}
760745
}
761746
}

src/SimpleInjector/Container.Registration.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,12 +1257,8 @@ private void ThrowArgumentExceptionWhenTypeIsNotConstructable(
12571257
}
12581258
}
12591259

1260-
private sealed class ContainerVerificationScope : Scope
1260+
private sealed class ContainerVerificationScope(Container container) : Scope(container)
12611261
{
1262-
public ContainerVerificationScope(Container container) : base(container)
1263-
{
1264-
}
1265-
12661262
public override void WhenScopeEnds(Action action)
12671263
{
12681264
// No-op.

src/SimpleInjector/ContainerOptions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,22 +526,22 @@ public override string ToString()
526526
descriptions.Add("Allows Overriding Registrations");
527527
}
528528

529-
if (!(this.ConstructorResolutionBehavior is DefaultConstructorResolutionBehavior))
529+
if (this.ConstructorResolutionBehavior is not DefaultConstructorResolutionBehavior)
530530
{
531531
descriptions.Add("Custom Constructor Resolution");
532532
}
533533

534-
if (!(this.DependencyInjectionBehavior is DefaultDependencyInjectionBehavior))
534+
if (this.DependencyInjectionBehavior is not DefaultDependencyInjectionBehavior)
535535
{
536536
descriptions.Add("Custom Dependency Injection");
537537
}
538538

539-
if (!(this.PropertySelectionBehavior is DefaultPropertySelectionBehavior))
539+
if (this.PropertySelectionBehavior is not DefaultPropertySelectionBehavior)
540540
{
541541
descriptions.Add("Custom Property Selection");
542542
}
543543

544-
if (!(this.LifestyleSelectionBehavior is DefaultLifestyleSelectionBehavior))
544+
if (this.LifestyleSelectionBehavior is not DefaultLifestyleSelectionBehavior)
545545
{
546546
descriptions.Add("Custom Lifestyle Selection");
547547
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright (c) Simple Injector Contributors. All rights reserved.
2+
// Licensed under the MIT License. See LICENSE file in the project root for license information.
3+
4+
namespace System.Runtime.CompilerServices
5+
{
6+
// Allows defining record types.
7+
internal class IsExternalInit { }
8+
}

0 commit comments

Comments
 (0)