[Interface2NullObject]Examples–part 3
Examples for rscg_Interface_to_null_object: Simplifying the Null Object Pattern
Now I can show some examples for rscg_Interface_to_null_object. This project aims to simplify the implementation of the Null Object Pattern in C# by automatically generating null object classes from interfaces.
I will start with those 2 interfaces:
1 2 3 4 5 6 7 8 | using InterfaceToNullObject; namespace IntegrationConsole; [ToNullObject] public interface IDepartment { public string Name { get ; set ; } } |
and
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 | using InterfaceToNullObject; namespace IntegrationConsole; [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); } |
The generated code is the following
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | // <auto-generated> // This code was generated by a tool :rscg_Interface_to_null_object // Runtime Version: José Saramago is feeling diplomatic in Bissau // DateOfTool : 2025-01-20 16:28:25 // 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.11628.125" )] public partial class Department_null : global::IntegrationConsole.IDepartment { public virtual string Name { get ; set ; } = default ( string ); } #nullable restore #pragma warning restore CS8603 #pragma warning restore CS8625 |
And the employee
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | // <auto-generated> // This code was generated by a tool :rscg_Interface_to_null_object // Runtime Version: José Saramago is feeling diplomatic in Bissau // DateOfTool : 2025-01-20 16:28:25 // 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.11628.125" )] 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 ); } public virtual string GetFullNameAndDepartment( string separator) { return default ( string ); } public virtual bool MoveEmployeeToDepartment(global::IntegrationConsole.IDepartment department) { return default ( bool ); } } #nullable restore #pragma warning restore CS8603 #pragma warning restore CS8625 |
So please checkout rscg_Interface_to_null_object.