RSCG – rscg_Interface_to_null_object
| name | rscg_Interface_to_null_object |
| nuget |
https://www.nuget.org/packages/rscg_Interface_to_null_object/ https://www.nuget.org/packages/rscg_Interface_to_null_object_common |
| link | https://github.com/ignatandrei/rscg_Interface_to_null_object/ |
| author | Andrei Ignat |
Generate null objects for interfaces
This is how you can use rscg_Interface_to_null_object .
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>
<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>
The code that you will use is
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);
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);
}
namespace NullInterface;
using InterfaceToNullObject;
[ToNullObject]
public interface IDepartment
{
public string Name { get; set; }
}
The code that is generated is
// <auto-generated/>
namespace Generated.NullInterface
{
public static class TheAssemblyInfo
{
public static readonly System.DateTime DateGeneratedUTC ;
public const string AssemblyName = "NullInterface";
public const string GeneratedNameNice = "Olga Tokarczuk is feeling quick-witted in Quito";
public const string GeneratedNameSmall = "quick-witted-Olga Tokarczuk";
public const string GeneratedName = "quick-witted-Olga Tokarczuk-Quito";
static TheAssemblyInfo(){
DateGeneratedUTC = System.DateTime.ParseExact("2025-02-15 15:42:10","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
Code and pdf at
https://ignatandrei.github.io/RSCG_Examples/v2/docs/rscg_Interface_to_null_object
Leave a Reply