Singleton Pattern

Singleton pattern ensure the class has only one instance that can be provided as a global point of access. “Single”ton as the name suggest, is said to be a container with one element. The instance is static and responsible for initialization of objects.

Use this pattern when the requirement must be exactly one instance of a class and it can extended by sub class and derived class should be able to use the instance without modifying the code.

Let’s take a look at the code samples:

UserResponseSingleton.cs – User class being made as singleton as I want single instance of a user on different classes

namespace DesignPatterns
{
    public sealed class UserResponseSingleton
    {
        UserResponseSingleton() { }
        private static readonly object lockObj = new object();
        private static UserResponseSingleton instance = null;
        public static UserResponseSingleton Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (lockObj)
                    {
                        if (instance == null)
                        {
                            instance = new UserResponseSingleton();
                        }
                    }
                }
                return instance;
            }
        }

        public string? _userID { get; set; }
        public void ProcessUser(string userID)
        {
            _userID = userID;
        }
    }
}

Set the instance on index.cshtml.cs and access it from the same/different classes altogether:

index.cshtml.cs


using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using System.Text;

namespace DesignPatterns.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        public UserResponseSingleton userResponseSingleton = UserResponseSingleton.Instance;

        public IndexModel(ILogger<IndexModel> logger)
        {
            _logger = logger;
            userResponseSingleton.ProcessUser("USER_ID=NK");

        }

        public void OnGet()
        {
            var userID = UserResponseSingleton.Instance;

            ViewData["Results"] = userID;

        }
    }
}

Privacy.cshtml.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace DesignPatterns.Pages
{
    public class PrivacyModel : PageModel
    {
        private readonly ILogger<PrivacyModel> _logger;
        public UserResponseSingleton userResponseSingleton = UserResponseSingleton.Instance;

        public PrivacyModel(ILogger<PrivacyModel> logger)
        {
            _logger = logger;

        }

        public void OnGet()
        {
            var userID = UserResponseSingleton.Instance;

        }
    }
}

Realtime Results of the above code:

So suppose the user id is common throughout the application, we will just set the instance value of the user ID in one accessible class. Once it is set in one class file it can be accessed in other classes by making use of singleton pattern.