Skip to content

Commit

Permalink
Cleanup up code using newer C# syntax.
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunkie committed May 24, 2024
1 parent daf4cfd commit 9dd0125
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public static ConstructorInfo GetConstructor(
}

private static ActivationException BuildActivationException(
IConstructorResolutionBehavior behavior, Type implementationType, string? message) =>
new ActivationException(
IConstructorResolutionBehavior behavior, Type implementationType, string? message) => new(
message is null || string.IsNullOrWhiteSpace(message)
? StringResources.TypeHasNoInjectableConstructorAccordingToCustomResolutionBehavior(
behavior, implementationType)
Expand Down
50 changes: 13 additions & 37 deletions src/SimpleInjector/Container.Common.cs
Original file line number Diff line number Diff line change
Expand Up @@ -709,60 +709,36 @@ where StringComparer.OrdinalIgnoreCase.Equals(friendlyName, missingTypeDefName)
select type;
}

private sealed class ContextualResolveInterceptor
private sealed class ContextualResolveInterceptor(
ResolveInterceptor interceptor, Predicate<InitializationContext> predicate)
{
public readonly ResolveInterceptor Interceptor;
public readonly Predicate<InitializationContext> Predicate;

public ContextualResolveInterceptor(
ResolveInterceptor interceptor, Predicate<InitializationContext> predicate)
{
this.Interceptor = interceptor;
this.Predicate = predicate;
}
public readonly ResolveInterceptor Interceptor = interceptor;
public readonly Predicate<InitializationContext> Predicate = predicate;
}

private sealed class TypedInstanceInitializer : IInstanceInitializer
private sealed class TypedInstanceInitializer(Type serviceType, object instanceInitializer)
: IInstanceInitializer
{
private readonly Type serviceType;
private readonly object instanceInitializer;

private TypedInstanceInitializer(Type serviceType, object instanceInitializer)
{
this.serviceType = serviceType;
this.instanceInitializer = instanceInitializer;
}

public bool AppliesTo(Type implementationType, InitializerContext context) =>
Types.GetTypeHierarchyFor(implementationType).Contains(this.serviceType);
Types.GetTypeHierarchyFor(implementationType).Contains(serviceType);

public Action<T> CreateAction<T>(InitializerContext context) =>
Helpers.CreateAction<T>(this.instanceInitializer);
Helpers.CreateAction<T>(instanceInitializer);

internal static IInstanceInitializer Create<TImplementation>(Action<TImplementation> initializer)
{
return new TypedInstanceInitializer(typeof(TImplementation), initializer);
}
}

private sealed class ContextualInstanceInitializer : IInstanceInitializer
{
private readonly Predicate<InitializerContext> predicate;
private readonly Action<InstanceInitializationData> instanceInitializer;

private ContextualInstanceInitializer(
private sealed class ContextualInstanceInitializer(
Predicate<InitializerContext> predicate,
Action<InstanceInitializationData> instanceInitializer)
{
this.predicate = predicate;
this.instanceInitializer = instanceInitializer;
}

public bool AppliesTo(Type implementationType, InitializerContext context) =>
this.predicate(context);
Action<InstanceInitializationData> instanceInitializer) : IInstanceInitializer
{
public bool AppliesTo(Type implementationType, InitializerContext context) => predicate(context);

public Action<T> CreateAction<T>(InitializerContext context) =>
instance => this.instanceInitializer(new InstanceInitializationData(context, instance!));
instance => instanceInitializer(new InstanceInitializationData(context, instance!));

internal static IInstanceInitializer Create(
Action<InstanceInitializationData> instanceInitializer,
Expand Down
35 changes: 10 additions & 25 deletions src/SimpleInjector/Container.Registration.Batch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -687,16 +687,10 @@ private static void CollectionDoesNotContainOpenGenericTypes(
}
}

private sealed class BatchMapping
private sealed class BatchMapping(Type implementationType, IEnumerable<Type> closedServiceTypes)
{
private BatchMapping(Type implementationType, IEnumerable<Type> closedServiceTypes)
{
this.ImplementationType = implementationType;
this.ClosedServiceTypes = closedServiceTypes;
}

internal Type ImplementationType { get; }
internal IEnumerable<Type> ClosedServiceTypes { get; }
public readonly Type ImplementationType = implementationType;
public readonly IEnumerable<Type> ClosedServiceTypes = closedServiceTypes;

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

public static BatchMapping Build(Type openServiceType, Type implementationType)
{
return new BatchMapping(
implementationType: implementationType,
closedServiceTypes: implementationType.GetBaseTypesAndInterfacesFor(openServiceType));
}
public static BatchMapping Build(Type openServiceType, Type implementationType) => new(
implementationType,
implementationType.GetBaseTypesAndInterfacesFor(openServiceType));

private static void RequiresNoDuplicateRegistrations(BatchMapping[] mappings)
{
Expand Down Expand Up @@ -745,17 +736,11 @@ where serviceTypeGroup.Count() > 1
}
}

private class NonGenericTypesToRegisterForOneToOneMappingResults
private sealed class NonGenericTypesToRegisterForOneToOneMappingResults(
List<Type> skippedDecorators, List<Type> implementationTypes)
{
public NonGenericTypesToRegisterForOneToOneMappingResults(
List<Type> skippedDecorators, List<Type> implementationTypes)
{
this.SkippedDecorators = skippedDecorators;
this.ImplementationTypes = implementationTypes;
}

public List<Type> SkippedDecorators { get; }
public List<Type> ImplementationTypes { get; }
public readonly List<Type> SkippedDecorators = skippedDecorators;
public readonly List<Type> ImplementationTypes = implementationTypes;
}
}
}
6 changes: 1 addition & 5 deletions src/SimpleInjector/Container.Registration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1257,12 +1257,8 @@ private void ThrowArgumentExceptionWhenTypeIsNotConstructable(
}
}

private sealed class ContainerVerificationScope : Scope
private sealed class ContainerVerificationScope(Container container) : Scope(container)
{
public ContainerVerificationScope(Container container) : base(container)
{
}

public override void WhenScopeEnds(Action action)
{
// No-op.
Expand Down
8 changes: 4 additions & 4 deletions src/SimpleInjector/ContainerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,22 +526,22 @@ public override string ToString()
descriptions.Add("Allows Overriding Registrations");
}

if (!(this.ConstructorResolutionBehavior is DefaultConstructorResolutionBehavior))
if (this.ConstructorResolutionBehavior is not DefaultConstructorResolutionBehavior)
{
descriptions.Add("Custom Constructor Resolution");
}

if (!(this.DependencyInjectionBehavior is DefaultDependencyInjectionBehavior))
if (this.DependencyInjectionBehavior is not DefaultDependencyInjectionBehavior)
{
descriptions.Add("Custom Dependency Injection");
}

if (!(this.PropertySelectionBehavior is DefaultPropertySelectionBehavior))
if (this.PropertySelectionBehavior is not DefaultPropertySelectionBehavior)
{
descriptions.Add("Custom Property Selection");
}

if (!(this.LifestyleSelectionBehavior is DefaultLifestyleSelectionBehavior))
if (this.LifestyleSelectionBehavior is not DefaultLifestyleSelectionBehavior)
{
descriptions.Add("Custom Lifestyle Selection");
}
Expand Down
8 changes: 8 additions & 0 deletions src/SimpleInjector/IsExternalInit.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Copyright (c) Simple Injector Contributors. All rights reserved.
// Licensed under the MIT License. See LICENSE file in the project root for license information.

namespace System.Runtime.CompilerServices
{
// Allows defining record types.
internal class IsExternalInit { }
}

0 comments on commit 9dd0125

Please sign in to comment.