Question

for our asp.net web application v 4.0, we are in process of defining a class that contains methods that are common across the application. to achieve this there are 2 Suggestions within our team.. one to create a base class, define the methods in that and derive all the other classes from that base class.. the other one is to create a seperate class (not a base class) and instantiate that common class in other classes when required to access the common methods. Please guide me in identifying the best approach..

Était-ce utile?

La solution

I'd only go the base class route if there is a real is-a relationship between the base class and the derived classes. One reason is that a class can only inherit from a single base class. I'd use this relationship in a sensible way. Just sharing some helper methods is not a scenario worth blocking this relationship.

If you want to use some helper methods in several classes, composition is the better way as you describe in the 2nd approach. Instead of creating the objects in the classes, you should think about whether you can inject the instances into the classes (see this link for details on dependency injection), e.g.:

public class HelperClass
{
    public virtual void HelperMethod()
    { 
        // ...
    }
}

public class ClassThatUsesHelper
{
    private readonly HelperClass _helper;

    public ClassThatUsesHelper(HelperClass helper)
    {
        _helper = helper;
    }

    public void DoSomething()
    {
        _helper.HelperMethod();
    }
}

By injecting the helper class you decouple the classes so that you can substitute the helper class by a different implementation that shares the same interface. ClassThatUsesHelper works with any class that is derived from HelperClass (or HelperClass itself of course). So if you need to decorate the helper method or need a special implementation in some cases, this is possible without any problem.

Using composition also enables you to test the helper methods separately.

However, if it is about very basic helper methods, you might also think about having a static class with static helper methods. Please note that you introduce a strong dependency between the classes and that you cannot adjust the implementation easily.

public static class HelperClass
{
    public static void HelperMethod()
    { 
        // ...
    }
}

public class ClassThatUsesHelper
{
    public void DoSomething()
    {
        HelperClass.HelperMethod();
    }
}

Autres conseils

Your question is vague, but if you need a method which all objects in your program will need to have access to, that uses their member variables, then I wold recommend creating an abstract class upon which your objects are based.

If you need a means of performing some sort of calculation from anywhere in your code, just create a public static method in a class meant for the purpose. MyMathClass.InterestingFourierTransform(), for example.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top