2

I have an interface based class that I want to have a few static classes as properties. However, I can't seem to find a way to use a static class as an instance property on a class based on an interface.

public interface IHttpHelp
{
   ItemsManager {get;set;}
}

public static class ItemsManager
{
   //static methods
}

public class HttpHelper
{
   public ItemsManager { get { return ItemsManager;} 
}

The above code won't work because of the "ItemsManager is used like a variable but it's a type error." Is there anyway to use a class this way?

For some insight into what I'm doing - I have a few static helper classes that access the httpruntime and current context. I currently use them directly, but wanted to move into a container class that will be used IoC. I could make them instance classes and forget about it, but I'm wondering f there's a way to this.

3 Answers 3

13

You can't use a static class like that, because by definition you can't create an instance of it, so you can't return it from a property. Make it a singleton instead:

public class ItemsManager
{
    #region Singleton implementation

    // Make constructor private to avoid instantiation from the outside
    private ItemsManager()
    {
    }

    // Create unique instance
    private static readonly ItemsManager _instance = new ItemsManager();

    // Expose unique instance
    public static ItemsManager Instance
    {
        get { return _instance; }
    }

    #endregion

    // instance methods
    // ...
}

public class HttpHelper
{
    public ItemsManager ItemsManager { get { return ItemsManager.Instance; } }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I feared :/ This being the case, I'll just make it an instance class, it's not a big deal - just a preference.
0

This is not supported by the language directly. You can either write a proxy class manually or use a library like the Duck Typing Project to emit a proxy class at runtime.

Both will have the same result: you will have a class that implements the interface, and proxies all calls to the static methods of the static class. Whether you want to write this yourself or use the duck typing library is up to you.

EDIT: Thomas' answer of using a singleton would be the way to go, if you have that option.

Comments

0

Static classes can't implement interfaces--it really wouldn't make much sense. An interface provides a standard API that all instances will support and you can swap instances and polymorphically access the methods through the standard interface. With a static class, all references to it are through the class anyways.

Typically in this situation you want a factory to support DI of an instance class that implements your helper.

public interface IHttpHelper
{ }

public class RealHttpHelper
{ ... }

public class FakeHttpHelper 
{ ... }

public static class HttpHelper 
{
    public static IHttpHelper Instance
    {
        get 
        {
            return whatever ? new RealHttpHelper() : new FakeHttpHelper();
        }
    }
}

...
HttpHelper.Instance.Context...
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.