Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an instance wrapper around a static class for the purpose of DI an anti pattern?

I have built a little static object for saving generic types to Isolated Storage on WP7. This works grand for older projects but some of the new projects use DI to manage configuration. I am a fan of DI as it means I can change the configuration in one place and have it filter down to all dependencies.

My thought was to create a namespace called Injection and wrap this object in an instance with an interface so I could inject it about. It would also enable me to swap out the storage handler for ones that require a more specific implementation.

Is this common practice or is this an anti pattern?

As a note, I want to keep the static option as not everyone needs or can use DI. I am simply trying to enable both with the least amount of duplication.

like image 370
deanvmc Avatar asked Feb 08 '12 09:02

deanvmc


People also ask

Are static methods an anti pattern?

On the other hand, static state, or static methods with associated static state, are utterly evil. That is an anti-pattern. It frequently helps to define a method as being non-stateful (and therefore a legitimate static method) if, and only if, it always returns equivalent output on equivalent inputs.

What is Singleton design pattern why it is used for Why not static class?

Singleton is a design pattern that makes sure that your application creates only one instance of the class anytime. It is highly efficient and very graceful. Singletons have a static property that you must access to get the object reference.

What is the purpose of static class in C#?

The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. They cannot inherit from any class except Object.

What are static classes used for?

Static classes are used as containers for static members. Static methods and static properties are the most-used members of a static class. All static members are called directly using the class name. Static methods do a specific job and are called directly using a type name, rather than the instance of a type.


1 Answers

You see this all the time. Mostly it's to work around legacy code which is already static or sealed or doesn't implement any interfaces. While a base class instead of an interface, HttpContextBase is the most prominent example I can think of.

Since you want to keep the static option, that's basically the situation you are currently in, so go ahead and do it. I wouldn't create an Injection namespace, though, as that name says more about the mechanics than the role the object plays.

You don't write exactly how the class is static, but I'm assuming that we are talking about a static class with static methods.

A slightly more elegant solution (if you can change the class a bit) is to turn the static class into a Singleton. Then it could be static and implement the interface at the same time:

public class Foo : IFoo
{
    private readonly static Foo instance = new Foo();

    private Foo() { }

    public static Foo Instance
    {
        get { return Foo.instance; }
    }

    // IFoo member:
    public void InterfaceFoo()
    {
        Foo.LegacyFoo();
    }

    public static void LegacyFoo()
    {
        // Implementation goes here...
    }
}

I think the only refactoring required to arrive at such a solution is to make the class itself concrete and make it implement the interface as a Singleton.

Old clients could still access its methods by invoking Foo.LegacyFoo();

like image 79
Mark Seemann Avatar answered Sep 30 '22 18:09

Mark Seemann