SaucyRegistrations 1.4.0
dotnet add package SaucyRegistrations --version 1.4.0
NuGet\Install-Package SaucyRegistrations -Version 1.4.0
<PackageReference Include="SaucyRegistrations" Version="1.4.0" />
paket add SaucyRegistrations --version 1.4.0
#r "nuget: SaucyRegistrations, 1.4.0"
// Install SaucyRegistrations as a Cake Addin #addin nuget:?package=SaucyRegistrations&version=1.4.0 // Install SaucyRegistrations as a Cake Tool #tool nuget:?package=SaucyRegistrations&version=1.4.0
Note
As of version 1.4, Saucy is currently in maintenance mode. This means that bugs will be fixed, PRs will be welcomed, and any new features of Microsoft.Extensions.DependencyInjection
will be considered for support. However, I'm not currently working on any new features or improvements. I use Saucy in multiple projects, both at work and in my free time, and I'll continue to maintain it as long as I'm a .NET developer.
Saucy Registrations
I've worked on a few large enterprise projects that made use of a dependency injection container. Across multiple different projects at various jobs, there existed some code to "automagically" register services with the container following some sort of convention. For example, classes that ended with "Repository" would be registered as a singleton, while classes that ended with "Service" would be registered as transient, etc. This code was always done with reflection, which was normally a pain to maintain and always seemed to be a bit of a black box.
I wanted to solve the problem for myself, once and for all. I wanted to solve it with minimal performance impact, minimal configuration, and maximum readability.
Enter...this.
What is this?
Saucy is an incremental source generator. Within the assembly it's been installed into, it generates a class with a single IServiceCollection
extension method. You can then choose to register all classes within a namespace at an assembly level, classes at an individual level, or a combination of both. Then, you simply call the generated extension method within your Startup.cs
file, and all of your services will be registered with the container.
How do I use this?
There's a comprehensive suite of integration tests within the solution, but here's a quick rundown.
The best way to make use of Saucy is to take advantage of the assembly-level namespace registration. Assume the following directory structure:
NamespaceOne:
- Class called
ServiceOne
- Class called
ServiceTwo
NamespaceTwo:
- Class called
ServiceThree
- Class called
ServiceFour
AssemblyInfo.cs
Within AssemblyInfo.cs
, you can add the following attributes:
[assembly: SaucyIncludeNamespace(nameof(SaucySample.NamespaceOne), ServiceScope.Transient)]
[assembly: SaucyIncludeNamespace(nameof(SaucySample.NamespaceTwo), ServiceScope.Scoped)]
Then, within your Startup.cs file, you can call the generated extension method:
public void ConfigureServices(IServiceCollection services)
{
services.Add{YOURASSEMBLYNAME}Services();
}
Any new classes you add to the NamespaceOne namespace will automatically be registered as transient, and any new classes you add to the NamespaceTwo namespace will automatically be registered as scoped.
It's also possible to include classes at an individual level. It's as simple as:
[SaucyInclude(ServiceScope.Singleton)]
public class YOURCLASS
{
}
Additional Notes
- If your class implements an interface, the interface will be registered as the service, and the class will be registered as the implementation. If you wish to exclude the interface, you can use the SaucyDoNotRegisterWithInterface attribute at the class level.
- By default, abstract classes are not registered as services; only the class that implements them is registered. If you wish to change this, you can use the SaucyRegisterAbstractClass attribute at the class level. I can count on one hand the number of times I've needed to resolve an abstract class, but I've included this feature for completeness.
- If you're using namespace-level registration, you can exclude a class from being registered by using the SaucyExclude attribute at the class level. However, if you find yourself needing to do this often, it might indicate that your namespaces are too broad—at least from my experience.
- Both open and closed generics are supported. Please see the integration tests for examples.
- Keyed services are supported via the SaucyKeyedService attribute. Please see the integration tests for examples.
Anything else?
I'm always open to feedback and contributions. If you have any ideas, please feel free to open an issue or a PR. I'm always looking to improve this library. Enjoy using Saucy!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.4.0 | 112 | 8/26/2024 |
1.3.0 | 100 | 7/22/2024 |
1.2.0 | 98 | 7/21/2024 |
1.1.0 | 95 | 7/21/2024 |
1.0.0 | 108 | 5/16/2024 |
0.0.7-beta | 866 | 4/16/2024 |
0.0.6-beta | 96 | 4/16/2024 |
0.0.5-beta | 110 | 4/14/2024 |
0.0.3-beta | 108 | 4/6/2024 |
0.0.2-beta | 91 | 3/24/2024 |
0.0.1-beta | 93 | 3/24/2024 |