SlowFox.Constructors
1.0.1
Prefix Reserved
dotnet add package SlowFox.Constructors --version 1.0.1
NuGet\Install-Package SlowFox.Constructors -Version 1.0.1
<PackageReference Include="SlowFox.Constructors" Version="1.0.1" />
paket add SlowFox.Constructors --version 1.0.1
#r "nuget: SlowFox.Constructors, 1.0.1"
// Install SlowFox.Constructors as a Cake Addin #addin nuget:?package=SlowFox.Constructors&version=1.0.1 // Install SlowFox.Constructors as a Cake Tool #tool nuget:?package=SlowFox.Constructors&version=1.0.1
Introduction
SlowFox is a suite of .NET source generators, aiming to reduce the amount of repetitive code you need to write and maintain.
Source generators incur no run-time cost (as no reflection is involved), because the code is created at build time. Plus, using a supported IDE (like Visual Studio 2019/2022), you can see what's being generated immediately when you save your source code.
There are currently 2 generators available via SlowFox:
- Constructors, (this package), for generating constructors and private variables
- UnitTestMocks, for generating mock objects in unit tests
SlowFox.Constructors
SloxFox.Constructors is a generator that allows you to define the injectable dependencies for any given class, and the private class members, constructor and constructor assignments are all automatically created for you.
How do I get it working?
First off, install the NuGet package into your project:
Then, find a class where you want to have the dependencies injected, mark it as partial
, and add the SlowFox.InjectDependencies
attribute. Pass into this attribute the types you want to be injected:
namespace MySampleProject
{
[SlowFox.InjectDependencies(typeof(IUserReader), typeof(IFileHandler))]
public partial class MyNewClass
{
}
}
SlowFox will then generate everything needed for these dependencies, as another partial class:
namespace MySampleProject
{
public partial class MyNewClass
{
private readonly IUserReader _userReader;
private readonly IFileHandler _fileHandler;
public MyNewClass(IUserReader userReader, IFileHandler fileHandler)
{
_userReader = userReader;
_fileHandler = fileHandler;
}
}
}
You can reference these private members in your original class, and you can call this constructor during DI, or call it manually, or use it in unit tests - it's as if you've written it all yourself:
namespace MySampleProject
{
[SlowFox.InjectDependencies(typeof(IUserReader), typeof(IFileHandler))]
public partial class MyNewClass
{
public void SaveUserImage()
{
var image = _userReader.GetImage();
_fileHandler.AddFile(image);
}
}
}
This generator is compatible with:
- inheritance
- abstract classes
- generic types
- nullable types
- tuples
Configuration
Configuration is set in a .editorconfig file.
To configure the generated code to not use underscores for member names, set the skip_underscores
value to true:
[*.cs]
slowfox_generation.constructors.skip_underscores = true
To include a null check (and a throw of ArgumentNullException
if the constructor parameter is null), set include_nullcheck
to true:
[*.cs]
slowfox_generation.constructors.include_nullcheck = true
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.CSharp (>= 4.0.1)
- Microsoft.CodeAnalysis.CSharp.Workspaces (>= 4.0.1)
- SlowFox.Constructors.Shared (>= 1.0.1)
- SlowFox.Core (>= 1.0.1)
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.0.1 | 87 | 5/3/2024 |
1.0.1-CI-20240502-154026 | 50 | 5/2/2024 |
1.0.0 | 314 | 4/19/2023 |
1.0.0-CI-20230419-075719 | 224 | 4/19/2023 |
0.3.1 | 195 | 4/18/2023 |
0.3.1-CI-20230418-125027 | 240 | 4/18/2023 |
0.3.1-CI-20230418-104341 | 209 | 4/18/2023 |
0.3.0 | 441 | 2/24/2023 |
0.3.0-CI-20230224-125642 | 241 | 2/24/2023 |
0.3.0-CI-20230224-123744 | 216 | 2/24/2023 |
0.2.0 | 467 | 5/23/2022 |
0.2.0-CI-20220523-151129 | 290 | 5/23/2022 |
0.1.2 | 438 | 3/14/2022 |
0.1.2-CI-20220428-101109 | 275 | 4/28/2022 |