Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to refactor this static class in c#? [closed]

See this example static class below.

public static class BackgroundTaskExecuter
{
    public static void MethodA()
    {
        using (var service = IocManager.Instance.ResolveAsDisposable<IServiceA>())
        {
            service.Object.MethodA();
        }
    }

    public static void MethodB()
    {
        using (var service = IocManager.Instance.ResolveAsDisposable<IServiceB>())
        {
            service.Object.MethodB();
        }
    }

    public static void MethodC()
    {
        using (var service = IocManager.Instance.ResolveAsDisposable<IServiceC>())
        {
            service.Object.MethodC();
        }
    }
}

As you can see, I have three methods. MethodA, MethodB, and MethodC that correspond with three different interfaces IServiceA, IServiceB, and IServiceC

The reason I am doing this is because I am using Hangfire.io with aspnetboilerplate framework and in Hangfire, a background task does not have HttpContext from the normal Dependency Injection. Creating a static class that wraps my calls where I resolve manually seems to get around this.

Usage looks like this:

BackgroundJob.Enqueue(() => BackgroundTaskExecuter.MethodA());

For now, I only have one or two background tasks in my web app, but conceivably I may have a lot more in the future and while it's maintainable now, it will get ugly eventually if I keep this approach.

Is there a better way to do this / refactor this? A factory pattern or anything like that perhaps?

Thanks.

like image 764
apexdodge Avatar asked Dec 16 '15 17:12

apexdodge


People also ask

How to access static members of a class in C#?

The data members of static class can be directly accessed by its class name. The data members of non-static class is not directly accessed by its class name. Static class always contains static members. Non-static class may contain both static and non-static methods.

What is static class and why we use it in C#?

In C#, a static class is a class that cannot be instantiated. The main purpose of using static classes in C# is to provide blueprints of its inherited classes. Static classes are created using the static keyword in C# and . NET.

What is the use of static method in Non static class C#?

You can define one or more static methods in a non-static class. Static methods can be called without creating an object. You cannot call static methods using an object of the non-static class. The static methods can only call other static methods and access static members.


1 Answers

I would make the static wrapper generic and simple. Let it expose a single method which resolves the service and consumes it via the using statement, allowing for the caller to invoke the instance passed into the Action<T>.

Source

public static class BackgroundTaskExecuter
{
    public static void ResolveAndConsume<T>(Action<T> consumeService)
    {
        // Consider applying constraint to the <T> to 
        // match the constraint of ResolveAsDisposable<T>
        using (var service = IocManager.Instance.ResolveAsDisposable<T>())
        {
            consumeService(service);
        }
    }
}

Example Usage

BackgroundJob.Enqueue(() => 
    BackgroundTaskExecuter.ResolveAndConsume<IServiceA>(serviceA => serviceA.MethodA()));

With the above you could then resolve and consume an implementation of the service and call its functionality as desired.

like image 168
David Pine Avatar answered Oct 12 '22 13:10

David Pine