Scrutor is a fantastic library, which fills two gaps in the service registration functionality available in the Microsoft container by providing support for assembly scanning and using the decorator pattern
One of the features of Scrutor is providing assembly scanning supportOne of the features of Scrutor is providing assembly scanning support
public static class SampleServiceCollectionExtensions { public static IServiceCollection AddSampleRules(this IServiceCollection services) { // Scrutor assembly scanning services.Scan(scan => scan .FromAssemblyOf<ISampleRule>() .AddClasses(classes => classes.AssignableTo<IScopedSampleRule>()) .AsImplementedInterfaces() .WithSingletonLifetime()); return services; } }
The Scrutor assembly scanning code is only slightly shorter than the manual code. So what’s the advantage of using assembly scanning here?
For a few rules, manually registering them is not a problem. But as the complexity grows and more rules need to be added, it’s entirely possible to forget to add the required registrations for them.
Assembly scanning in this way means that I can add a new implementation of the ICourtBookingRule at any time in the future, and at runtime it will be found by the scanning process and registered for me.
Ultimately, this reduces risk and manual work needed to maintain all of the registrations.
Great content! Super high-quality! Keep it up! 🙂