Patternify.NullObject
0.1.2
dotnet add package Patternify.NullObject --version 0.1.2
NuGet\Install-Package Patternify.NullObject -Version 0.1.2
<PackageReference Include="Patternify.NullObject" Version="0.1.2" />
paket add Patternify.NullObject --version 0.1.2
#r "nuget: Patternify.NullObject, 0.1.2"
// Install Patternify.NullObject as a Cake Addin #addin nuget:?package=Patternify.NullObject&version=0.1.2 // Install Patternify.NullObject as a Cake Tool #tool nuget:?package=Patternify.NullObject&version=0.1.2
Patternify.NullObject
This source generator simplifies the implementation of the Null Object design pattern in C#. It automatically generates a default, "do nothing" implementation of an interface, helping you avoid null reference exceptions and redundant null checks in your code.
Features
- Automatically generates a Null Object implementation for interfaces.
- Reduces the need for null checks, promoting cleaner, safer code.
- Simple and easy-to-use with a single attribute.
How to Use
Step 1: Apply the [NullObject]
Attribute
To generate a Null Object for an interface, apply the [NullObject]
attribute to the interface.
Example:
[NullObject]
public interface ILog
{
public int Id { get; }
public string Login(string username, string password);
public void Info(string message);
public void Warn(string message);
}
Step 2: Let the Generator Do the Work
The generator will automatically create a class that implements the ILog
interface with default "do nothing" behavior.
Generated code:
// <auto-generated/>
using DesignPatternCodeGenerator.Attributes.NullObject;
namespace Samples.NullObject
{
public class LogNullObject : ILog
{
public int Id { get; }
public void Info(string message) { }
public void Warn(string message) { }
public string Login(string username, string password)
{
return default(string);
}
}
}
Step 3: Use the Null Object
You can now safely use the generated LogNullObject
class as a default implementation when an instance of ILog
is expected, without the need for null checks.
Generated code:
ILog logger = new LogNullObject();
logger.Info("This will do nothing safely.");
Installation
To use this source generator in your project, add the NuGet package to your project:
dotnet add package Patternify.NullObject
Contributing
Contributions are welcome! If you encounter any issues or have ideas for improvements, feel free to open an issue or submit a pull request.
License
This project is licensed under the MIT License.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.CodeAnalysis.Common (>= 4.11.0)
- Microsoft.CodeAnalysis.CSharp (>= 4.11.0)
- Microsoft.CodeAnalysis.Workspaces.Common (>= 4.11.0)
- Patternify.Abstraction (>= 0.1.2)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
0.1.2 | 92 | 9/25/2024 |