rscg_Interface_to_null_object by Andrei Ignat
Nuget / site data
Details
Info
Name: rscg_Interface_to_null_object
Interface to null object - common
Author: Andrei Ignat
NuGet: https://www.nuget.org/packages/rscg_Interface_to_null_object/
https://www.nuget.org/packages/rscg_Interface_to_null_object_common
You can find more details at https://github.com/ignatandrei/rscg_Interface_to_null_object/
Source : https://github.com/ignatandrei/rscg_Interface_to_null_object/
Original Readme
Interface to Null Object Pattern
Implementation of https://en.wikipedia.org/wiki/Null_object_pattern from interface
Installation
Add to your csproj file:
<ItemGroup>
<PackageReference Include="rscg_Interface_to_null_object" Version="2025.120.1832" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="rscg_Interface_to_null_object_common" Version="2025.120.1832" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
Or add the nuget packages rscg_Interface_to_null_object and rscg_Interface_to_null_object_common
Usage
[InterfaceToNullObject.ToNullObject]
public interface IEmployee
{
public string FirstName { get; set; }
public string LastName { get; set; }
public IDepartment Department { get; set; }
public string GetFullName();
}
And then a C# class that implements the interface will be generated
public partial class Employee_null : global::IntegrationConsole.IEmployee
{
public virtual string FirstName { get; set; } = default(string);
public virtual string LastName { get; set; } = default(string);
public virtual IntegrationConsole.IDepartment Department { get; set; } = default(IntegrationConsole.IDepartment);
public virtual string GetFullName() { return default(string); }
}
About
Generate null objects for interfaces
How to use
Example ( source csproj, source files )
- CSharp Project
- Program.cs
- IEmployee.cs
- IDepartment.cs
This is the CSharp Project that references rscg_Interface_to_null_object
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="rscg_Interface_to_null_object" Version="2025.120.1832" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<PackageReference Include="rscg_Interface_to_null_object_common" Version="2025.120.1832" />
</ItemGroup>
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
<CompilerGeneratedFilesOutputPath>$(BaseIntermediateOutputPath)\GX</CompilerGeneratedFilesOutputPath>
</PropertyGroup>
</Project>
This is the use of rscg_Interface_to_null_object in Program.cs
using NullInterface;
Console.WriteLine("Hello, World!");
Console.WriteLine("Hello, World!");
IDepartment department = new Department_null();
department.Name = "IT";
IEmployee employee = new Employee_null();
employee.FirstName = "Andrei";
employee.Department = department;
Console.WriteLine(employee.FirstName);
Console.WriteLine(employee.Department.Name);
This is the use of rscg_Interface_to_null_object in IEmployee.cs
using InterfaceToNullObject;
namespace NullInterface;
[ToNullObject]
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);
public bool MoveEmployeeToDepartment(IDepartment department);
}
This is the use of rscg_Interface_to_null_object in IDepartment.cs
namespace NullInterface;
using InterfaceToNullObject;
[ToNullObject]
public interface IDepartment
{
public string Name { get; set; }
}
Generated Files
Those are taken from $(BaseIntermediateOutputPath)\GX
- TheAssemblyInfo.g.cs
- IDepartment_null.cs
- IEmployee_null.cs
// <auto-generated/>
namespace Generated.NullInterface
{
public static class TheAssemblyInfo
{
public static readonly System.DateTime DateGeneratedUTC ;
public const string AssemblyName = "NullInterface";
public const string GeneratedNameNice = "Nadine Gordimer is feeling rational in Seoul";
public const string GeneratedNameSmall = "rational-Nadine Gordimer";
public const string GeneratedName = "rational-Nadine Gordimer-Seoul";
static TheAssemblyInfo(){
DateGeneratedUTC = System.DateTime.ParseExact("2025-01-21 13:29:00", "yyyy-MM-dd HH:mm:ss", null);
}
}
}
// <auto-generated>
// This code was generated by a tool :rscg_Interface_to_null_object
// Runtime Version: Jaroslav Seifert is feeling cheap in Belmopan
// DateOfTool : 2025-01-20 16:33:43
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
/// <summary>
/// This static partial class contains extension methods for sorting collections of IDepartment objects.
/// </summary>
#nullable enable
#pragma warning disable CS8603
#pragma warning disable CS8625
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("GeneratorName","2025.10120.11633.143")]
public partial class Department_null : global::NullInterface.IDepartment
{
public virtual string Name { get; set; } = default(string);
}
#nullable restore
#pragma warning restore CS8603
#pragma warning restore CS8625
// <auto-generated>
// This code was generated by a tool :rscg_Interface_to_null_object
// Runtime Version: Jaroslav Seifert is feeling cheap in Belmopan
// DateOfTool : 2025-01-20 16:33:43
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
/// <summary>
/// This static partial class contains extension methods for sorting collections of IEmployee objects.
/// </summary>
#nullable enable
#pragma warning disable CS8603
#pragma warning disable CS8625
[global::System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
[global::System.CodeDom.Compiler.GeneratedCode("GeneratorName","2025.10120.11633.143")]
public partial class Employee_null : global::NullInterface.IEmployee
{
public virtual string FirstName { get; set; } = default(string);
public virtual string LastName { get; set; } = default(string);
public virtual NullInterface.IDepartment Department { get; set; } = default(NullInterface.IDepartment);
public virtual string GetFullName() { return default(string); }
public virtual string GetFullNameAndDepartment(string separator) { return default(string); }
public virtual bool MoveEmployeeToDepartment(global::NullInterface.IDepartment department) { return default(bool); }
}
#nullable restore
#pragma warning restore CS8603
#pragma warning restore CS8625
Usefull
Download Example (.NET C# )
Share rscg_Interface_to_null_object
https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg_Interface_to_null_object