RSCG – sourcedepend

RSCG – sourcedepend
 
 

name sourcedepend
nuget https://www.nuget.org/packages/sourcedepend/
link https://github.com/crwsolutions/sourcedepend
author Colin Wilmans

Generating constructor for DI

 

This is how you can use sourcedepend .

The code that you start with is


<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>

	<PropertyGroup>
		<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
		<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
	</PropertyGroup>

	<ItemGroup>
	  <PackageReference Include="SourceDepend" Version="0.3.0" />
	</ItemGroup>

</Project>


The code that you will use is


using CtorDemo;

var p = new Person("Andrei", "Ignat");

Console.WriteLine(p.FullName());



namespace CtorDemo;
internal partial class Person
{
    [Dependency]
    public readonly string? FirstName;
    [Dependency]
    public readonly string? LastName;
    
    public string FullName() => $"{FirstName} {LastName}";

    partial void PostConstruct()
    {
        Console.WriteLine("Person constructed");
    }
    partial void PreConstruct()
    {
        Console.WriteLine("Person constructing");
    }

}


 

The code that is generated is

// <auto-generated />

/// <summary>
/// Injects this item in the constructor. This will also highjack your constructor, so if you have any construct business, use PreConstruct() or PostConstruct() methods.
/// </summary>
/// <remarks>
/// Make sure your class is partial.
/// </remarks>
[System.AttributeUsage(System.AttributeTargets.Field | System.AttributeTargets.Property, Inherited = false, AllowMultiple = false)]
[System.Diagnostics.Conditional("DependencyGenerator_DEBUG")]
internal sealed class DependencyAttribute : System.Attribute
{
    internal DependencyAttribute(string alternativePropertyName = null) { }
}

// <auto-generated/>
#pragma warning disable
#nullable enable
namespace CtorDemo
{
    /// <inheritdoc/>
    internal partial class Person
    {
        public Person(string? FirstName, string? LastName)
        {
            PreConstruct();

            this.FirstName = FirstName;
            this.LastName = LastName;

            PostConstruct();
        }

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

Code and pdf at

https://ignatandrei.github.io/RSCG_Examples/v2/docs/sourcedepend