Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IServiceProviderIsKeyedService in Lamar DI framework #401

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ public static HostApplicationBuilder UseLamar(this HostApplicationBuilder builde

// This enables the usage of implicit services in Minimal APIs
builder.Services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());

builder.Services.AddSingleton(s => (IServiceProviderIsKeyedService) s.GetRequiredService<IContainer>());

builder.ConfigureContainer<ServiceRegistry>(new LamarServiceProviderFactory(), x =>
{
configure?.Invoke(x);
Expand Down Expand Up @@ -76,8 +77,9 @@ public static IHostBuilder UseLamar(this IHostBuilder builder, Action<HostBuilde
#if NET6_0_OR_GREATER
// This enables the usage of implicit services in Minimal APIs
services.AddSingleton(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());
services.AddSingleton(s => (IServiceProviderIsKeyedService) s.GetRequiredService<IContainer>());
#endif
});
}

Expand Down Expand Up @@ -119,6 +121,7 @@ public static IServiceCollection AddLamar(this IServiceCollection services, Serv

#if NET6_0_OR_GREATER
services.AddSingleton<IServiceProviderIsService>(s => (IServiceProviderIsService) s.GetRequiredService<IContainer>());
services.AddSingleton<IServiceProviderIsKeyedService>(s => (IServiceProviderIsKeyedService)s.GetRequiredService<IContainer>());
#endif

registry ??= new ServiceRegistry();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Shouldly;
using StructureMap.Testing.Widget;
using Xunit;

namespace Lamar.Testing.IoC.Acceptance;
#if NET6_0_OR_GREATER
public class IServiceProviderIsKeyedService_implementation
{
[Fact]
public void explicit_checks_of_non_concrete_types()
{
var container = Container.For(services => {
services.For<IClock>().Use<Clock>();
services.AddKeyedSingleton<IClock, Clock>("IClock");
});

container.IsKeyedService(typeof(IClock), "IClock").ShouldBeTrue();
container.IsKeyedService(typeof(IWidget), "IWidget").ShouldBeFalse();
}

[Fact]
public void fix_for_368_auto_resolve_enumerable_concretes_and_generics()
{
var container = Container.For(services =>
{
services.Scan(s =>
{
s.AssemblyContainingType(GetType());
s.WithDefaultConventions();
s.AddAllTypesOf<IService>();
});

services.For(typeof(IGenericService1<>)).Use(typeof(GenericService1<>));
services.AddKeyedSingleton<IEnumerable<IService>, List<ServiceA>>("IEnumerable<ServiceA>");
services.AddKeyedSingleton<IEnumerable<IService>, List<ServiceB>>("IEnumerable<ServiceB>");
services.AddKeyedSingleton(typeof(IGenericService1<>), "GenericService1<>", typeof(GenericService1<>));

});

container.IsKeyedService(typeof(ConcreteClass), "ConcreteClass").ShouldBeFalse();
container.IsKeyedService(typeof(IEnumerable<IService>), "IEnumerable<ServiceA>").ShouldBeTrue();
container.IsKeyedService(typeof(IGenericService1<IService>), "IGenericService1<IService>").ShouldBeTrue();
container.IsKeyedService(typeof(IGenericService1<ConcreteClass>), "IGenericService1<ConcreteClass>").ShouldBeTrue();
}

[Fact]
public void fix_for_391_dont_resolve_collections_with_not_registered_type()
{
var containerWithNoRegistrations = Container.Empty();

containerWithNoRegistrations.IsKeyedService(typeof(IEnumerable<ConcreteClass>),"IEnumerable<ConcreteClass>").ShouldBeFalse();
containerWithNoRegistrations.IsKeyedService(typeof(IReadOnlyCollection<ConcreteClass>) ,"IReadOnlyCollection<ConcreteClass>").ShouldBeFalse();
containerWithNoRegistrations.IsKeyedService(typeof(IList<ConcreteClass>), "IList<ConcreteClass>").ShouldBeFalse();
containerWithNoRegistrations.IsKeyedService(typeof(List<ConcreteClass>), "List<ConcreteClass>").ShouldBeFalse();

var containerWithRegistrations = Container.For(services =>
{
services.ForConcreteType<ConcreteClass>();
services.AddKeyedSingleton<IEnumerable<IService>, List<ServiceA>>("IEnumerable<ServiceA>");
});

containerWithRegistrations.IsKeyedService(typeof(IEnumerable<ConcreteClass>), "IEnumerable<ConcreteClass>").ShouldBeTrue();
containerWithRegistrations.IsKeyedService(typeof(IReadOnlyCollection<ConcreteClass>), "IReadOnlyCollection<ConcreteClass>").ShouldBeTrue();
containerWithRegistrations.IsKeyedService(typeof(IList<ConcreteClass>), "IList<ConcreteClass>").ShouldBeTrue();
containerWithRegistrations.IsKeyedService(typeof(List<ConcreteClass>), "List<ConcreteClass>").ShouldBeTrue();
}

public interface IService
{
}

public class ServiceA : IService
{
}

public class ServiceB : IService
{
}

public class ConcreteClass
{
}

public interface IGenericService1<T>
{
}

public class GenericService1<T> : IGenericService1<T>
{
}
}
#endif
9 changes: 7 additions & 2 deletions src/Lamar/IoC/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#region sample_Scope-Declarations

public class Scope : IServiceContext, IServiceProviderIsService
public class Scope : IServiceContext, IServiceProviderIsService, IServiceProviderIsKeyedService

#endregion

Expand Down Expand Up @@ -248,7 +248,7 @@
if (instance == null)
{
throw new InvalidOperationException(
$"Cannot QuickBuild type {objectType.GetFullName()} because Lamar cannot determine how to build required dependency {x.ParameterType.FullNameInCode()}");

Check warning on line 251 in src/Lamar/IoC/Scope.cs

View workflow job for this annotation

GitHub Actions / build

'TypeExtensions.GetFullName(Type)' is obsolete: 'Favor FullNameInCode(), this is just a passthrough'

Check warning on line 251 in src/Lamar/IoC/Scope.cs

View workflow job for this annotation

GitHub Actions / build

'TypeExtensions.GetFullName(Type)' is obsolete: 'Favor FullNameInCode(), this is just a passthrough'
}

try
Expand Down Expand Up @@ -369,6 +369,11 @@
public bool IsService(Type serviceType)
{
return ServiceGraph.CanBeServiceByNetCoreRules(serviceType);
}

public bool IsKeyedService(Type serviceType, object serviceKey)
{
return ServiceGraph.CanBeServiceByNetCoreRules(serviceType, serviceKey.ToString());
}

public static Scope Empty()
Expand Down Expand Up @@ -482,5 +487,5 @@
public object GetRequiredKeyedService(Type serviceType, object serviceKey)
{
return GetInstance(serviceType, serviceKey.ToString());
}
}
}
31 changes: 31 additions & 0 deletions src/Lamar/ServiceGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,35 @@ public bool CanBeServiceByNetCoreRules(Type serviceType)

return family.Default != null;
}

public bool CanBeServiceByNetCoreRules(Type serviceType, string name)
{
if (_families.TryFind(serviceType, out var family))
{
return family.InstanceFor(name) != null;
}

lock (_familyLock)
{
if (_families.TryFind(serviceType, out family))
{
return family.InstanceFor(name) != null;
}

family = TryToCreateMissingFamilyWithNetCoreRules(serviceType);
_families = _families.AddOrUpdate(serviceType, family);

if (!_inPlanning)
{
buildOutMissingResolvers();

if (family != null)
{
rebuildReferencedAssemblyArray();
}
}
}

return family.InstanceFor(name) != null;
}
}
Loading