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

Open generic scanning extensions #1246

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 22 additions & 0 deletions test/Autofac.Test/Assertions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Autofac.Builder;
using Autofac.Core;
using Autofac.Features.Indexed;
using Autofac.Features.OpenGenerics;
using Xunit;

namespace Autofac.Test
Expand Down Expand Up @@ -92,6 +96,24 @@ public static void AssertComponentRegistrationOrder<TService, TFirstComponent, T
Assert.True(foundLast);
}

public static bool RegisteredAnyOpenGenericTypeFromScanningAssembly(this IComponentContext context)
{
return context.ComponentRegistry.Sources.Any(source =>
{
if (source is OpenGenericRegistrationSource)
{
var activatorData = typeof(OpenGenericRegistrationSource)
.GetField("_activatorData", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(source) as ReflectionActivatorData;
return activatorData.ImplementationType != typeof(KeyedServiceIndex<,>);
}
else
{
return false;
}
});
}

private static IEnumerable<Type> LookForComponents(this IEnumerable<IComponentRegistration> registrations, IEnumerable<Type> types)
{
return registrations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Autofac.Core.Lifetime;
using Autofac.Core.Registration;
using Autofac.Features.Metadata;
using Autofac.Features.OpenGenerics;
using Autofac.Features.Scanning;
using Autofac.Test.Scenarios.ScannedAssembly;
using Autofac.Test.Scenarios.ScannedAssembly.MetadataAttributeScanningScenario;
Expand Down Expand Up @@ -119,6 +120,30 @@ public void AssignableToNullTypeProvidedThrowsException()
.AssignableTo(null, t => t));
}

[Theory]
[InlineData(typeof(ICloseCommand))]
[InlineData(typeof(CloseCommand))]
public void AssignableToClosedTypeProvidedNoneOpenGenericSourceRegistered(Type closedType)
{
var cb = new ContainerBuilder();
cb.RegisterAssemblyOpenGenericTypes(typeof(ICommand<>).GetTypeInfo().Assembly)
.AssignableTo(closedType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It feels like passing a closed type into AssignableTo for Open Generics is enough of an input mistake that we should throw an ArgumentException if someone tries it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alistairjevans when using AssignableTo in registering assembly closed type, it doesn't throw exception when the argument is open generic type. Therefore should we keep this behavior in this case or we will modify it too ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point. We should retain the same behaviour for consistency, and I don't think it's worth a breaking change in the existing AssignableTo.

var c = cb.Build();

Assert.False(c.RegisteredAnyOpenGenericTypeFromScanningAssembly());
}

[Fact]
public void ServiceIsNotAssignableToIsNotRegistered()
{
var cb = new ContainerBuilder();
cb.RegisterAssemblyOpenGenericTypes(typeof(ICommand<>).GetTypeInfo().Assembly)
.AssignableTo(typeof(RedoOpenGenericCommand<>));
var c = cb.Build();

Assert.Throws<ComponentNotRegisteredException>(() => c.Resolve<DeleteOpenGenericCommand<int>>());
}

[Fact]
public void AssignableToOpenGenericInterfaceTypeProvidedOpenGenericTypesRegistered()
{
Expand Down