Skip to content

Commit

Permalink
Fix bug in attributes registration with custom strategy (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
manuel-fernandez-rodriguez authored Dec 20, 2024
1 parent 3393ab4 commit 9cf0882
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Registerly/src/DeviantCoding.Registerly.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
<IsDeviantCodingNugetPackage>true</IsDeviantCodingNugetPackage>
<Description>Dependency injection made easy</Description>
<PackageTags>DI;Dependency;Injection;</PackageTags>
<Version>1.2.1</Version>
<Version>1.2.2</Version>
<PackageReleaseNotes>
Add net standard support
Fix bug in attributes registration with custom strategy
</PackageReleaseNotes>
<PackageProjectUrl>https://manuel-fernandez-rodriguez.github.io/DeviantCoding/docs/registerly/introduction.html</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand Down
11 changes: 6 additions & 5 deletions Registerly/src/Strategies/Mapping/AttributeMappingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,19 @@ public IEnumerable<ServiceDescriptor> Map(IEnumerable<Type> implementationTypes,
yield return serviceDescriptor;
}
}

if (implementationType.GetInterfaces().Length != 0)
else if (implementationType.GetInterfaces().Length != 0)
{
foreach (var serviceDescriptor in _asImplementedInterfaces.Map([implementationType], lifetimeStrategy))
{
yield return serviceDescriptor;
}
}

foreach (var serviceDescriptor in _asSelf.Map([implementationType], lifetimeStrategy))
else
{
yield return serviceDescriptor;
foreach (var serviceDescriptor in _asSelf.Map([implementationType], lifetimeStrategy))
{
yield return serviceDescriptor;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public IServiceCollection RegisterServices(IServiceCollection serviceCollection,
foreach (var descriptor in descriptors)
{
GetRegistrationStrategy(descriptor)
.RegisterServices(serviceCollection, new[] { descriptor });
.RegisterServices(serviceCollection, [descriptor]);
}

return serviceCollection;
Expand Down
18 changes: 18 additions & 0 deletions Registerly/test/UnitTests/RegisterByAttributesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,24 @@ public void ResolveServicesFromCustomAssemblySource()
.Should().BeOfType<TestSingletonService>();
}

[Fact]
public void CanRegisterWithCustomMappingStrategy()
{
var host = Host.CreateEmptyApplicationBuilder(new());

host.RegisterServicesByAttributes(classes => classes
.FromAssemblyOf<TestScopedService>()
.Where(c => c.Exactly<ClassImplementingTwoInterfaces>()));

var services = host.Services.BuildServiceProvider();

services.GetRequiredService<IInterface1>()
.Should().BeOfType<ClassImplementingTwoInterfaces>();

services.GetService<IInterface2>()
.Should().BeNull();
}


private static void Assert(IServiceCollection services)
{
Expand Down
8 changes: 8 additions & 0 deletions Registerly/test/UnitTests/SampleServices/SampleServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public class TestSingletonService(TestServiceDependency serviceDependency) : ISi
#pragma warning disable IDE0052 // Remove unread private members
private readonly TestServiceDependency _serviceDependency = serviceDependency;
#pragma warning restore IDE0052 // Remove unread private members
}

public interface IInterface1 { }
public interface IInterface2 { }

[Scoped<As<IInterface1>>]
public class ClassImplementingTwoInterfaces : IInterface1, IInterface2
{
}

0 comments on commit 9cf0882

Please sign in to comment.