Skip to content

Commit

Permalink
Small cleanup related to previous work on #690.
Browse files Browse the repository at this point in the history
  • Loading branch information
dotnetjunkie committed Apr 26, 2019
1 parent 4d172b8 commit 209a2e2
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 43 deletions.
7 changes: 4 additions & 3 deletions src/SimpleInjector/Internals/CollectionResolver.cs
Expand Up @@ -31,6 +31,7 @@ namespace SimpleInjector.Internals
internal abstract class CollectionResolver
{
private readonly List<RegistrationGroup> registrationGroups = new List<RegistrationGroup>();

private readonly Dictionary<Type, InstanceProducer> producerCache =
new Dictionary<Type, InstanceProducer>();

Expand All @@ -51,7 +52,7 @@ protected CollectionResolver(Container container, Type serviceType)
protected Container Container { get; }

internal abstract void AddControlledRegistrations(
Type serviceType, ContainerControlledItem[] registrations, bool append);
Type serviceType, ContainerControlledItem[] items, bool append);

internal abstract void RegisterUncontrolledCollection(Type serviceType, InstanceProducer producer);

Expand Down Expand Up @@ -185,11 +186,11 @@ protected sealed class RegistrationGroup
};

internal static RegistrationGroup CreateForControlledItems(
Type serviceType, ContainerControlledItem[] registrations, bool appended) =>
Type serviceType, ContainerControlledItem[] items, bool appended) =>
new RegistrationGroup
{
ServiceType = serviceType,
ControlledItems = registrations,
ControlledItems = items,
Appended = appended
};
}
Expand Down
12 changes: 6 additions & 6 deletions src/SimpleInjector/Internals/ContainerControlledCollection.cs
Expand Up @@ -138,9 +138,9 @@ void IContainerControlledCollection.Clear()
this.producers.Clear();
}

void IContainerControlledCollection.Append(ContainerControlledItem registration)
void IContainerControlledCollection.Append(ContainerControlledItem item)
{
this.producers.Add(this.ToLazyInstanceProducer(registration));
this.producers.Add(this.ToLazyInstanceProducer(item));
}

KnownRelationship[] IContainerControlledCollection.GetRelationships() => (
Expand Down Expand Up @@ -195,10 +195,10 @@ private static object VerifyCreatingProducer(Lazy<InstanceProducer> lazy)
}
}

private Lazy<InstanceProducer> ToLazyInstanceProducer(ContainerControlledItem registration) =>
registration.Registration != null
? ToLazyInstanceProducer(registration.Registration)
: new Lazy<InstanceProducer>(() => this.GetOrCreateInstanceProducer(registration));
private Lazy<InstanceProducer> ToLazyInstanceProducer(ContainerControlledItem item) =>
item.Registration != null
? ToLazyInstanceProducer(item.Registration)
: new Lazy<InstanceProducer>(() => this.GetOrCreateInstanceProducer(item));

private static Lazy<InstanceProducer> ToLazyInstanceProducer(Registration registration) =>
Helpers.ToLazy(new InstanceProducer(typeof(TService), registration));
Expand Down
Expand Up @@ -43,9 +43,9 @@ internal override void RegisterUncontrolledCollection(Type serviceType, Instance
}

internal override void AddControlledRegistrations(
Type serviceType, ContainerControlledItem[] registrations, bool append)
Type serviceType, ContainerControlledItem[] items, bool append)
{
var group = RegistrationGroup.CreateForControlledItems(serviceType, registrations, append);
var group = RegistrationGroup.CreateForControlledItems(serviceType, items, append);
this.AddRegistrationGroup(group);
}

Expand Down Expand Up @@ -82,7 +82,7 @@ private ContainerControlledItem[] GetClosedContainerControlledItemsFor(Type serv
var items = this.GetItemsFor(serviceType);

return serviceType.IsGenericType()
? Types.GetClosedGenericImplementationsFor(serviceType, items)
? GetClosedGenericImplementationsFor(serviceType, items)
: items.ToArray();
}

Expand All @@ -92,5 +92,21 @@ private ContainerControlledItem[] GetClosedContainerControlledItemsFor(Type serv
closedGenericServiceType.IsAssignableFrom(registrationGroup.ServiceType)
from item in registrationGroup.ControlledItems
select item;

private static ContainerControlledItem[] GetClosedGenericImplementationsFor(
Type closedGenericServiceType, IEnumerable<ContainerControlledItem> containerControlledItems)
{
return (
from item in containerControlledItems
let openGenericImplementation = item.ImplementationType
let builder = new GenericTypeBuilder(closedGenericServiceType, openGenericImplementation)
let result = builder.BuildClosedGenericImplementation()
where result.ClosedServiceTypeSatisfiesAllTypeConstraints
select item.Registration != null
? item
: ContainerControlledItem.CreateFromType(
openGenericImplementation, result.ClosedGenericImplementation))
.ToArray();
}
}
}
25 changes: 10 additions & 15 deletions src/SimpleInjector/Internals/IContainerControlledCollection.cs
Expand Up @@ -28,43 +28,38 @@ namespace SimpleInjector.Internals
using System.Linq;
using SimpleInjector.Advanced;

/// <summary>This interface is not meant for public use.</summary>
internal interface IContainerControlledCollection : IEnumerable
{
bool AllProducersVerified { get; }

/// <summary>Please do not use.</summary>
/// <returns>Do not use.</returns>
KnownRelationship[] GetRelationships();

/// <summary>PLease do not use.</summary>
/// <param name="registration">Do not use.</param>
void Append(ContainerControlledItem registration);

void Append(ContainerControlledItem item);

void Clear();

void VerifyCreatingProducers();
}

internal static class ContainerControlledCollectionExtensions
{
internal static void AppendAll(this IContainerControlledCollection collection,
IEnumerable<ContainerControlledItem> registrations)
internal static void AppendAll(
this IContainerControlledCollection collection, IEnumerable<ContainerControlledItem> items)
{
foreach (ContainerControlledItem registration in registrations)
foreach (ContainerControlledItem item in items)
{
collection.Append(registration);
collection.Append(item);
}
}

internal static void AppendAll(this IContainerControlledCollection collection,
IEnumerable<Registration> registrations)
internal static void AppendAll(
this IContainerControlledCollection collection, IEnumerable<Registration> registrations)
{
collection.AppendAll(registrations.Select(ContainerControlledItem.CreateFromRegistration));
}

internal static void AppendAll(this IContainerControlledCollection collection,
IEnumerable<Type> types)
internal static void AppendAll(
this IContainerControlledCollection collection, IEnumerable<Type> types)
{
collection.AppendAll(types.Select(ContainerControlledItem.CreateFromType));
}
Expand Down
16 changes: 0 additions & 16 deletions src/SimpleInjector/Types.cs
Expand Up @@ -227,22 +227,6 @@ internal static IEnumerable<Type> GetTypeBaseTypesAndInterfaces(this Type type)
return thisType.Concat(type.GetBaseTypesAndInterfaces());
}

internal static ContainerControlledItem[] GetClosedGenericImplementationsFor(
Type closedGenericServiceType, IEnumerable<ContainerControlledItem> containerControlledItems)
{
return (
from item in containerControlledItems
let openGenericImplementation = item.ImplementationType
let builder = new GenericTypeBuilder(closedGenericServiceType, openGenericImplementation)
let result = builder.BuildClosedGenericImplementation()
where result.ClosedServiceTypeSatisfiesAllTypeConstraints
select item.Registration != null
? item
: ContainerControlledItem.CreateFromType(
openGenericImplementation, result.ClosedGenericImplementation))
.ToArray();
}

private static IEnumerable<Type> GetBaseTypes(this Type type)
{
Type baseType = type.BaseType() ?? (type != typeof(object) ? typeof(object) : null);
Expand Down

0 comments on commit 209a2e2

Please sign in to comment.