SlowFox.Constructors 0.1.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
There is a newer version of this package available.
See the version list below for details.
dotnet add package SlowFox.Constructors --version 0.1.2
NuGet\Install-Package SlowFox.Constructors -Version 0.1.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SlowFox.Constructors" Version="0.1.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SlowFox.Constructors --version 0.1.2
#r "nuget: SlowFox.Constructors, 0.1.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install SlowFox.Constructors as a Cake Addin
#addin nuget:?package=SlowFox.Constructors&version=0.1.2

// Install SlowFox.Constructors as a Cake Tool
#tool nuget:?package=SlowFox.Constructors&version=0.1.2

Introduction

SlowFox is a suite of .NET source generators, aiming to reduce the amount of repetitive code you need to write and maintain. Since they're all written using source generators, there's no run-time cost (so no reflection involved), instead 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.

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.

alternate text is missing from this package README image alternate text is missing from this package README image Build Status Repo Size Licence Release Date

Why use SlowFox.Constructors?

It can sometimes get a bit tedious writing constructors, especially if you're using Dependency Injection and the constructor parameters are just a list of interfaces. Adding a new dependency might involve adding a class member, adding a constructor parameter, and adding an assignment between the two in that constructor. This can be quite distracting, especially if you're in the middle of a rapid prototype, or trying to get some refactoring done.

Using a new attribute provided by SlowFox.Constructors, you can now just list the dependencies needed to be injected into a class, and the generator will do the rest for you. Adding or removing a dependency is as simple as updating the list within the attribute, and the generator will automatically refresh and create the updated code.

How do I get this working?

First off, install the NuGet package into your project:

alternate text is missing from this package README image

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;
        }
    }
}

And that's it!

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. However, if you change what's listed within the InjectDependencies attribute, this generated code will automatically update, without you having to do anything.

But, I hate underscores. Can I change what's generated?

Yes you can. In a .editorconfig file, you can change the naming convention used so that it doesn't have a preceeding underscore.

[*.cs]
slowfox_generation.constructors.skip_underscores = true

You can place this .editorconfig file in the root of your project to apply it everywhere, or you can put it inside a directory structure to change only classes under that area:

namespace MySampleProject
{
    public partial class MyNewClass
    {
        private readonly IUserReader userReader;
        private readonly IFileHandler fileHandler;

        public MyNewClass(IUserReader userReader, IFileHandler fileHandler)
        {
            this.userReader = userReader;
            this.fileHandler = fileHandler;
        }
    }
}

Can I automatically check for nulls?

A common check within constructors like this is to check that the parameters are not null. This can be set to be included in the generated file. In your .editorconfig file, you can enable this:

[*.cs]
slowfox_generation.constructors.include_nullcheck = true

This will check if the dependency injected is null (if it's a nullable type), and throw an ArgumentNullException if it is null:

namespace MySampleProject
{
    public partial class MyNewClass
    {
        private readonly IUserReader _userReader;
        private readonly IFileHandler _fileHandler;

        public MyNewClass(IUserReader userReader, IFileHandler fileHandler)
        {
            _userReader = userReader ?? throw new System.ArgumentNullException(nameof(userReader));
            _fileHandler = fileHandler ?? throw new System.ArgumentNullException(nameof(fileHandler));
        }
    }
}

FAQ

Sometimes IntelliSense doesn't update properly, is there something wrong?

It appears as though the tooling for source generators aren't 100% there yet, so if you find that IntelliSense is warning you that something doesn't exist that you'd expect, or if you go and find the generated code and it's not updated following a change, it may be that you need to give a bit of a kick-start to get going again. If a rebuild doesn't fix it, try closing and opening your IDE (especially if you're using Visual Studio), and you should see the updates then. This only seems to affect IDEs though - the actual .NET build seems really solid for generated code.

I've applied the InjectDependencies attribute but I get an error for "Missing partial modifier on declaration of type 'MyCurrentClass'; another partial declaration of this type exists'". What do I do?

You've not set your class to be partial. Do that, and the error will be fixed.

Can I use this to generate constructors for nested classes?

Yes.

What happens if I don't list any types in the InjectDependencies attribute?

Nothing. No code is generated for that class.

Can I help fix any bugs with this, or add new capabilities?

Of course! Head on over to the issues page, raise it, and we'll have a chat about what needs to be done.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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 51 5/3/2024
1.0.1-CI-20240502-154026 33 5/2/2024
1.0.0 292 4/19/2023
1.0.0-CI-20230419-075719 196 4/19/2023
0.3.1 174 4/18/2023
0.3.1-CI-20230418-125027 207 4/18/2023
0.3.1-CI-20230418-104341 183 4/18/2023
0.3.0 373 2/24/2023
0.3.0-CI-20230224-125642 209 2/24/2023
0.3.0-CI-20230224-123744 188 2/24/2023
0.2.0 442 5/23/2022
0.2.0-CI-20220523-151129 261 5/23/2022
0.1.2 408 3/14/2022
0.1.2-CI-20220428-101109 239 4/28/2022