SourceDepend 0.4.0

dotnet add package SourceDepend --version 0.4.0
NuGet\Install-Package SourceDepend -Version 0.4.0
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="SourceDepend" Version="0.4.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SourceDepend --version 0.4.0
#r "nuget: SourceDepend, 0.4.0"
#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 SourceDepend as a Cake Addin
#addin nuget:?package=SourceDepend&version=0.4.0

// Install SourceDepend as a Cake Tool
#tool nuget:?package=SourceDepend&version=0.4.0

Source Depend

A source generator for C# that uses Roslyn (the C# compiler) to help you with dependency injection (DI). It saves you from writing the constructor because this will be written for you (during compile time). Just tag the member with a [Dependency] attribute.

NuGet version (sourcedepend) License

Version history

  • v0.1. First implementation.
  • v0.2. Complete rewrite from ISourceGenerator to IIncrementalGenerator, this should boost performance
    • keep sealed and accessibility intact.
  • v0.3. Complete Rewrite: reorganized the code.
    • Allow one level of inheritance.
  • v0.4. Added null-checks.

How to use it

Install it and add the attribute to the fields or properties you want be set in your constructor, like so:

public partial class ExampleService
{
    [Dependency]
    private readonly AnotherService anotherService;

    [Dependency]
    AnotherService Prop { get; }
}

Alternative assignment

It is also possible that the generated assignment is to an alternative property:

public partial class ExampleService
{
    [Dependency(nameof(BindingContext))]
    AnotherService ViewModel => BindingContext as AnotherService;
}

Inheritance

And it is possible to inherit from a base implementation that also uses the [Dependency] attribute:

internal partial class BaseExampleService
{
    [Dependency]
    private readonly IForBaseService _someBaseService;
}

internal partial class ExampleService : BaseExampleService
{

}

Add construction work

Because your constructor is highjacked, there are the alternative methods PreConstruct/PostConstruct to do your construction work:

public partial class ExampleService
{
    [Dependency]
    private readonly AnotherService anotherService;

    ///This method will be called before the generated assignments
    partial void PreConstruct()
    {
        Initialize()
    }

    ///This method will be called after the generated assignments
    partial void PostConstruct() => anotherService.ConstructValue = "Hello from post-construct!";
}

These samples give the following combined generated code:

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ConsoleApp
{
    /// <inheritdoc/>
    public partial class ExampleService
    {
        public ExampleService(ConsoleApp.IAnotherService anotherService, ConsoleApp.AnotherService prop, ConsoleApp.AnotherService viewModel, ConsoleApp.IForBaseService someBaseService) : base(someBaseService)
        {

#if NET6_0_OR_GREATER
            ArgumentNullException.ThrowIfNull(anotherService);
            ArgumentNullException.ThrowIfNull(prop);
            ArgumentNullException.ThrowIfNull(viewModel);
#endif
            PreConstruct();

            this.anotherService = anotherService;
            Prop = prop;
            BindingContext = viewModel;

            PostConstruct();
        }

        partial void PreConstruct();
        partial void PostConstruct();
    }
}

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace ConsoleApp
{
    /// <inheritdoc/>
    internal partial class BaseExampleService
    {
        public BaseExampleService(ConsoleApp.IForBaseService someBaseService)
        {

#if NET6_0_OR_GREATER
            ArgumentNullException.ThrowIfNull(someBaseService);
#endif
            PreConstruct();

            this._someBaseService = someBaseService;

            PostConstruct();
        }

        partial void PreConstruct();
        partial void PostConstruct();
    }
}

Installing

The package is available on NuGet. To install from the command line:

dotnet add package sourcedepend

Or use the Package Manager in Visual Studio.

Contributing

The main supported IDE for development is Visual Studio 2019.

Questions, comments, bug reports, and pull requests are all welcome. Bug reports that include steps to reproduce (including code) are preferred. Even better, make them in the form of pull requests.

Maintainers/Core team

Contributors can be found at the contributors page on Github.

License

This software is open source, licensed under the MIT License. See LICENSE for details. Check out the terms of the license before you contribute, fork, copy or do anything with the code. If you decide to contribute you agree to grant copyright of all your contribution to this project and agree to mention clearly if do not agree to these terms. Your work will be licensed with the project at MIT, along the rest of the code.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has 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
0.4.0 3 4/27/2024
0.3.0 151 1/6/2024
0.2.0 124 12/27/2023
0.1.0 176 10/2/2022

Added null checks