Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependency Injection for singelton class with properties

I am having a custom context class in my ASP.NET 4.8 Framework website:

public sealed class MyCustomContext
{
    private static readonly Lazy<MyCustomContext> staticContext =
        new Lazy<MyCustomContext>(() => new MyCustomContext());

    private MyCustomContext()
    {
    }

    public static MyCustomContext Current => staticContext.Value;

    public HttpContext Context => HttpContext.Current;

    // Logic to return current user based on logged in user
    public User LoggedInUser => ...
    
    // Logic to return SiteWideSettings
    public Collection<SiteWideSettings> SiteWideSettings => ...
}

The above class is a Singleton and the usage of the above class in my service class methods is like this:

public class MyService : IMyService
{
    public MyService()
    {
    }

    public void DoWork()
    {
        var current = MyCustomContext.Current;
        var loggedInUser = current.LoggedInUser;
        var siteWideSettings = current.SiteWideSettings;
        var currentContext = current.Context;
        // use the above properties further for this method
    }
}

My goal is to remove MyCustomContext class dependency hardcoded in my DoWork method of MyService class so that it can look like this:

public class MyService : IMyService
{
    private readonly IMyCustomContext _myCustomContext;
    public MyService(IMyCustomContext myCustomContext)
    {
        _myCustomContext = myCustomContext;
    }

    public void DoWork()
    {
        var current = _myCustomContext.Current;
        var loggedInUser = current.LoggedInUser;
        var siteWideSettings = current.SiteWideSettings;
        var currentContext = current.Context;
        // use the above properties further for this method
    }
}

Can you share how to convert my MyCustomContext class so that it can be injected via dependency injection into MyService?

I have one more question, do the properties like LoggedInUser, SiteWideSettings and Context of MyCustomContext class should be written as properties or they should be converted to methods for dependency injection?

like image 639
Raghav Avatar asked Oct 19 '25 23:10

Raghav


1 Answers

For the dependency injection you need an interface which gets initialized, so your MyCustomContext class needs to implement a new interface called IMyCustomContext. The interface can look like following:

public interface IMyCustomContext
{
    HttpContext Context { get; }
    User LoggedInUser { get; }
    Collection<SiteWideSettings> SiteWideSettings { get; }
}

public class MyCustomContext : IMyCustomContext
{
    public HttpContext Context
    {
        get { return HttpContext.Current; }
    }


    public User LoggedInUser
    {
        get
        {
            // Logic to return current user based on logged in user
        }
    }

    public Collection<SiteWideSettings> SiteWideSettings
    {
        get
        {
            // Logic to return SiteWideSettings
        }
    }
}

In the Startup.cs there is a method called ConfigureServices, there you can add the following for the dependency injection:

container.RegisterType<IMyCustomContext, MyCustomContext>(
    TypeLifetime.Singleton);
like image 76
Celdus Avatar answered Oct 21 '25 13:10

Celdus