0

I've read up a good few articles etc., here and from Google, about trying to accomplish my task. I probably am over complicating matters.

Now before you all say it, I know you can't inherit static members/routines. What I'm asking is how to do what I need to do and properly.

I am writing a static helper class which has a number of member and routines. It has to be static as per the nature of the tasks it works with so I can't make an instance of it, unless you tell me otherwise.

What I need to be able to do is make a base static class, which will just work and do the logic (The Helper) but then I need to be able to make a new class (Static essentially) that I can put in one or more "new" static methods which override the base methods but where this class now acts like the base one - passing through its members.

I first thought using Interface but that needs an instance.

What do you think is the best way to resolve this?

Anthony
  • 441
  • 1
  • 5
  • 20

4 Answers4

2

It looks like one of your classes could be a regular class. You could then use composition to bring the two together to achieve what you need. I would break things up like this:

internal class TaskProcessor
{
    // All of the work/logic goes in this class which can have instances
}

public static class StaticHelper
{
    private TaskProcessor _processor = new TaskProcessor();

    public static void SomeMethod()
    {
        _processor.SomeMethod();
    }

    // And so on
}
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • The problem is the base class has to have static properties and routines, that is, at least I think it has to be. It is an nHibernate helper wiring up the Session Factory among other things. My intention is to separate out logic that reference System.Web into the "extended" static class. I cannot make an instance, think the Singleton pattern (based on my knowledge anyway). – Anthony Mar 01 '12 at 16:13
  • Why not use the singleton pattern then here to ensure you only have 1 instance and are using that same instance for everything. Then you can achieve the inheritance that you want. – jzworkman Mar 01 '12 at 16:14
  • Wait. I just re-read your answer. Doh! I may try it but I am sure you are correct. As I am only making one route to the static class, either the extended or the base directly, doing the above will be the same will it not? Will the "instanced" class stay available for the life time of the app i.e. a web instance on Application Start? – Anthony Mar 01 '12 at 16:15
  • @jzworkman - If the OP truly needs a static class, the Singleton pattern won't help. A Singleton isn't necessarily static...it's an instance class that can only have a single instance of itself. A static class has no instance. – Justin Niessner Mar 01 '12 at 16:16
  • @jzworkman Honestly, because I don't fully understand it. I get the jist of it but not the full working reason why it should be used... Would this be one of those reasons? – Anthony Mar 01 '12 at 16:17
  • @JustinNiessner Maybe it doesn't have to be static but it can only be "built" once at application start (WEB) and has to stay alive until the worker process, not the current request, shuts down. nHibernate requires this for its Session Factory – Anthony Mar 01 '12 at 16:18
  • @JustinNiessner I know the singleton would no longer be truly static(as there is one instance) but it would allow the inheritance that he wants just instead of calling myClass.function() he would be calling myClass.getInstance().function() – jzworkman Mar 01 '12 at 16:19
  • @Anthony I edited my answer below to show the singleton example for how you create and access the singleton pattern instance – jzworkman Mar 01 '12 at 16:22
1

To me this is the entire purpose of extension methods. Allowing a safe way to share functionality among types that conform to a standard. You can have the same extension method to be overloaded to provide different functionality for dispirit types while preserving semantically similar code.

rerun
  • 25,014
  • 6
  • 48
  • 78
0

See this answer: Why can't I have abstract static methods in C#? to understand why abstract static classes cannot be used in c#

you could also use the singleton pattern to make sure only one instance exists for your class:

public class Singleton
{
   private static Singleton instance;

   private Singleton() {}

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
      }
   }
}
Community
  • 1
  • 1
jzworkman
  • 2,695
  • 14
  • 20
  • Before I go ahead with this I have one more query: The whole reason why I am doing this is to separate out dependency on System.Web... I hadn't thought it through until now but will I require to implement what I am doing with two separate assemblies? The base one being the assembly without the System.Web reference... If you are wondering why, I am writing a data access layer which could then be used with any project Web or Windows Forms etc.. – Anthony Mar 01 '12 at 16:32
  • Yea if the base one has the System.Web reference then any inherited classes will as well, so if you want one to be with System.Web and one without, then the base would need to be the one without the System.Web reference. – jzworkman Mar 01 '12 at 16:35
0

If you're trying to avoid depending on System.Web everywhere, you could use a kind of dependency injection.

// Base assembly; does not reference System.Web
public class HelperBase
{
    public virtual void DoSomething() { ... }
    public virtual void DoSomethingElse() { ... }
}

public static class StaticHelper
{
    public static HelperBase Helper { get; set; }
    static void DoSomething()
    {
        Helper.DoSomething();
    }

    static void DoSomethingElse()
    {
        Helper.DoSomethingElse();
    }
}

// HelperWeb assembly; references System.Web, base assembly
public class HelperWeb : HelperBase
{
    // override base implementation, using System.Web
    public virtual void DoSomethingElse() { ... }
}


// usage at top level:

StaticHelper.Helper = new HelperBase();
// or
StaticHelper.Helper = new HelperWeb();

StaticHelper.DoSomething();

This way, StaticHelper does not need to reference System.Web. You only need to reference System.Web at the top level if you're actually using the Helper which needs System.Web. You could even use reflection to conditionally load the HelperWeb assembly only at runtime.

tcovo
  • 7,550
  • 2
  • 20
  • 13