Skip to main content

rscg_Interface_to_null_object by Andrei Ignat

NuGet / site data

NugetNuget GitHub last commit GitHub Repo stars

Details

Info

Original Readme

note

NuGet version

NuGet version

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

note

Generate null objects for interfaces

How to use

Example (source csproj, source files)

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>

Generated Files

Those are taken from $(BaseIntermediateOutputPath)\GX

// <auto-generated/>
#pragma warning disable
#nullable enable annotations


#if !AUTOINTERFACE_EXCLUDE_ATTRIBUTES

using System;

namespace AutoInterfaceAttributes;

/// <summary>
/// Generates an interface for the decorated class/struct.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, AllowMultiple = true)]
[System.CodeDom.Compiler.GeneratedCodeAttribute("AutoInterface", "2.4.0")]
internal sealed class AutoInterfaceAttribute : Attribute {
/// <summary>
/// <para>The name of the generated interface.</para>
/// <para>Default is "I{ClassName}"</para>
/// </summary>
public string Name { get; init; }

/// <summary>
/// <para>The modifier(s) for the interface.</para>
/// <para>Deault is "public partial"</para>
/// </summary>
public string Modifier { get; init; }

/// <summary>
/// <para>The namespace declaration for the interface.</para>
/// <para>If empty string, no namespace directive will be used (global namespace).<br />
/// Default (if not present) it will be mapped to the same namespace as the namespace of the class/struct.</para>
/// </summary>
public string Namespace { get; init; }

/// <summary>
/// <para>interface inheritance: Name(s) of interfaces this interface will inherit.</para>
/// <para>Default is Array.Empty</para>
/// </summary>
public Type[] Inheritance { get; init; }

/// <summary>
/// <para>
/// The Classes, structs or interfaces containing the generated interface.<br />
/// e.g. ["public sealed partial class Example"] will wrap the interface with that expression.
/// </para>
/// <para>Default is Array.Empty</para>
/// </summary>
public string[] Nested { get; init; }

/// <summary>
/// <para>If enabled, static members get accepted and are generating "static abstract" members.</para>
/// <para>Default is false</para>
/// </summary>
public bool StaticMembers { get; init; }
}

#endif

Useful

Download Example (.NET C#)

Share rscg_Interface_to_null_object

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

aaa

Category "Interface" has the following generators:

1 AutoInterface

2 Biwen.AutoClassGen

3 CopyCat

4 Farskeptic.AutoCompose

5 MakeInterface.Generator

6 Matryoshki

7 Minerals.AutoInterfaces

8 NetAutomaticInterface

9 ProxyGen

10 Roozie.AutoInterface

11 RSCG_CompositeProvider

12 rscg_Interface_to_null_object

13 RSCG_Static

14 ShadowWriterNullobjects

See category

Interface