RSCG – ShadowWriterNullobjects
name | ShadowWriterNullobjects |
nuget | https://www.nuget.org/packages/ShadowWriter/ |
link | https://github.com/StefanStolz/ShadowWriter |
author | Stefan Stolz |
Generate null object for an class that implements an interface
This is how you can use ShadowWriterNullobjects .
The code that you start with is
<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>net9.0</TargetFramework> <ImplicitUsings>enable</ImplicitUsings> <Nullable>enable</Nullable> </PropertyGroup> <PropertyGroup> <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> <CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath> </PropertyGroup> <ItemGroup> <PackageReference Include="ShadowWriter" Version="0.9.8"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> </PackageReference> </ItemGroup> </Project>
The code that you will use is
using NullInterface; Console.WriteLine("Hello, World!"); Console.WriteLine("Hello, World!"); Department department = Department.Instance; department.Name = "IT"; IEmployee employee = Employee.Instance; employee.FirstName = "Andrei"; //employee.Department = department; Console.WriteLine(employee.FirstName); Console.WriteLine(employee.Department.Name); Console.WriteLine(employee.GetFullNameAndDepartment(" - "));
namespace NullInterface; [ShadowWriter.NullObject] public partial class Department : IDepartment { } public interface IDepartment { public string Name { get; set; } }
namespace NullInterface; [ShadowWriter.NullObject] public partial class Employee: IEmployee { public IDepartment Department { get; set; } = NullInterface.Department.Instance; } public interface IEmployee { public string FirstName { get; set; } public string LastName { get; set; } public IDepartment Department { get; set; } public string GetFullName(); public string GetFullNameAndDepartment(string separator); }
The code that is generated is
// <auto-generated/> using System; using System.CodeDom.Compiler; using System.Runtime.CompilerServices; namespace ShadowWriter { [CompilerGenerated] [GeneratedCode("ShadowWriter", "0.9.8.0")] [System.AttributeUsage(AttributeTargets.Class)] internal sealed class BuilderAttribute : System.Attribute { } }
using System; using System.Linq; using System.CodeDom.Compiler; using System.Runtime.CompilerServices; using System.Threading.Tasks; // Generated by GenerateNullObjectInPartialClass #nullable enable namespace NullInterface; [CompilerGenerated] [GeneratedCode("ShadowWriter", "0.9.8.0")] public sealed partial class Department { private Department() {} public static NullInterface.Department Instance { get; } = new Department(); public string Name { get => string.Empty; set => _ = value; } }
using System; using System.Linq; using System.CodeDom.Compiler; using System.Runtime.CompilerServices; using System.Threading.Tasks; // Generated by GenerateNullObjectInPartialClass #nullable enable namespace NullInterface; [CompilerGenerated] [GeneratedCode("ShadowWriter", "0.9.8.0")] public sealed partial class Employee { private Employee() {} public static NullInterface.Employee Instance { get; } = new Employee(); public string FirstName { get => string.Empty; set => _ = value; } public string LastName { get => string.Empty; set => _ = value; } // GetFullName public string GetFullName() { return string.Empty; } // GetFullNameAndDepartment public string GetFullNameAndDepartment(string separator) { return string.Empty; } }
// <auto-generated/> using System; using System.CodeDom.Compiler; using System.Runtime.CompilerServices; namespace ShadowWriter { [CompilerGenerated] [GeneratedCode("ShadowWriter", "0.9.8.0")] [System.AttributeUsage(AttributeTargets.Interface | AttributeTargets.Class)] internal sealed class NullObjectAttribute : System.Attribute { } [CompilerGenerated] [GeneratedCode("ShadowWriter", "0.9.8.0")] [System.AttributeUsage(System.AttributeTargets.Interface)] internal sealed class ClassNameAttribute : System.Attribute { public string Name { get; } public ClassNameAttribute(string name) { this.Name = name; } public ClassNameAttribute() { this.Name = string.Empty; } } }
using System; using System.CodeDom.Compiler; using System.Runtime.CompilerServices; namespace ShadowWriter; [CompilerGenerated] [GeneratedCode("ShadowWriter", "0.9.8.0")] internal static class TheProject { public static string FullPath => @"D:\gth\RSCG_Examples\v2\rscg_examples\ShadowWriterNullobjects\src\NullInterface\NullInterface.csproj"; public static string ProjectDirectory => @"D:\gth\RSCG_Examples\v2\rscg_examples\ShadowWriterNullobjects\src\NullInterface"; public static string Name => @"NullInterface"; public static string OutDir => @"C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\bin\Debug\net9.0\"; public static string Version => @"1.0.0"; public static string RootNamespace => @"NullInterface"; public static DateTimeOffset BuildTimeUtc => new DateTimeOffset(638918269412724408, TimeSpan.Zero); }
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/ShadowWriterNullobjects