Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
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

Make Auto Registration of Generic Handlers OPT-IN #1057

Merged
merged 2 commits into from
Sep 9, 2024
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
Next Next commit
Remove Missing Constraints logic
- remove logic
- reomove tests
  • Loading branch information
zachpainter77 committed Aug 3, 2024
commit 6abee0ca2c246cd70dc26cc0a1f29b34f1f809c4
5 changes: 1 addition & 4 deletions src/MediatR/Registration/ServiceRegistrar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,7 @@ private static (Type Service, Type Implementation) GetConcreteRegistrationTypes(
var constraintsForEachParameter = openRequestHandlerImplementation
.GetGenericArguments()
.Select(x => x.GetGenericParameterConstraints())
.ToList();

if (constraintsForEachParameter.Count > 2 && constraintsForEachParameter.Any(constraints => !constraints.Where(x => x.IsInterface || x.IsClass).Any()))
throw new ArgumentException($"Error registering the generic handler type: {openRequestHandlerImplementation.FullName}. When registering generic requests with more than two type parameters, each type parameter must have at least one constraint of type interface or class.");
.ToList();

var typesThatCanCloseForEachParameter = constraintsForEachParameter
.Select(constraints => assembliesToScan
Expand Down
18 changes: 0 additions & 18 deletions test/MediatR.Tests/GenericRequestHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,6 @@ public void ShouldNotRegisterDuplicateHandlers(int numberOfClasses, int numberOf
hasDuplicates.ShouldBeFalse();
}

[Fact]
public void ShouldThrowExceptionWhenRegisterningHandlersWithNoConstraints()
{
IServiceCollection services = new ServiceCollection();
services.AddSingleton(new Logger());

var assembly = GenerateMissingConstraintsAssembly();

Should.Throw<ArgumentException>(() =>
{
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(assembly);
});
})
.Message.ShouldContain("When registering generic requests with more than two type parameters, each type parameter must have at least one constraint of type interface or class.");
}

[Fact]
public void ShouldThrowExceptionWhenTypesClosingExceedsMaximum()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@
namespace MediatR.Tests.MicrosoftExtensionsDI
{
public abstract class BaseGenericRequestHandlerTests
{

protected static Assembly GenerateMissingConstraintsAssembly() =>
CreateAssemblyModuleBuilder("MissingConstraintsAssembly", 3, 3, CreateHandlerForMissingConstraintsTest);

{
protected static Assembly GenerateTypesClosingExceedsMaximumAssembly() =>
CreateAssemblyModuleBuilder("ExceedsMaximumTypesClosingAssembly", 201, 1, CreateHandlerForExceedsMaximumClassesTest);

Expand Down
Loading