Patternify.Singleton 0.1.2

dotnet add package Patternify.Singleton --version 0.1.2                
NuGet\Install-Package Patternify.Singleton -Version 0.1.2                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Patternify.Singleton" Version="0.1.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Patternify.Singleton --version 0.1.2                
#r "nuget: Patternify.Singleton, 0.1.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Patternify.Singleton as a Cake Addin
#addin nuget:?package=Patternify.Singleton&version=0.1.2

// Install Patternify.Singleton as a Cake Tool
#tool nuget:?package=Patternify.Singleton&version=0.1.2                

Patternify.Singleton

This source generator simplifies the implementation of the Singleton design pattern in C#. It ensures that a class has only one instance and provides a global point of access to it, while also handling thread safety and lazy initialization.

Features

  • Automatically generates a thread-safe Singleton implementation for your classes.
  • Simple and easy-to-use with a single attribute.
  • Supports lazy initialization to optimize performance.

How to Use

Step 1: Apply the [Singleton] Attribute

To transform a class into a Singleton, apply the [Singleton] attribute to it. The class must be partial to allow the generator to inject code into it.

Example:

using Patternify.Singleton;

namespace Samples.Singleton;

[Singleton]
public partial class Configuration
{
    public string SomeProperty { get; set; }
}

Step 2: Let the Generator Do the Work

The generator will automatically create the necessary code to implement the Singleton pattern for the Configuration class.

Generated code:

// <auto-generated/>
using Patternify.Singleton;

namespace Samples.Singleton
{
    public partial class Configuration
    {
        private static Configuration _instance = null;
        private static object obj = new object();

        private Configuration() { }

        public static Configuration GetInstance()
        {
            lock(obj)
            {
                if (_instance == null)
                {
                    _instance = new Configuration();
                }
            }

            return _instance;
        }
    }
}

Step 3: Access the Singleton

Now you can access the singleton instance using the GetInstance() method:

Generated code:

var config = Configuration.GetInstance();
config.SomeProperty = "SomeValue";

Installation

To use this source generator in your project, add the NuGet package to your project:

dotnet add package Patternify.Singleton

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 101 9/25/2024
0.1.0 117 9/24/2024