Is the instance wrapping around a static class to create an anti-pattern?

I created a small static object to save common types in isolated storage on WP7. This works for older projects, but some of the newer projects use DI for configuration management. I am a fan of DI, as this means that I can change the configuration in one place and filter it to all the dependencies.

My thought was to create a namespace called Injection and wrap this object in an instance with an interface so that I can embed it. It would also allow me to replace the storage handler with those that require a more specific implementation.

Is this common practice or is it an anti-pattern?

As a side note, I want to keep the static parameter as not everyone needs or can use DI. I'm just trying to include both with the least amount of duplication.

+5
source share
2 answers

You see it all the time. This mainly works with legacy code that is already static or sealed or does not implement any interfaces. Although the base class is instead of an interface, HttpContextBase is the most striking example that I can think of.

, , , , . Injection, , , , .

, , , .

( ) - Singleton. :

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...
    }
}

, , , - Singleton.

, Foo.LegacyFoo();

+8

-, , - , , .

.

+1

All Articles