Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common base class for usercontrols and pages in ASP.NET

Tags:

c#

asp.net

Right now I have a base class for my pages which inherits System.Web.UI.Page and another base class for my usercontrols which inherits System.Web.UI.UserControl, these classes contains the same methods. Since C# doesn't support multiple inheritance I can't combine the two classes into one that inherits both Page and UserControl.

What would be the best way to keep the functionality in the two base classes but have the implementation of these methods in only one place?

I'm thinking of making a interface and let the two base classes call a third class that contains the implementation of the interface. Is there any better way so that when I add a new method I don't have to do it in three places (even though the implementation is only in the third class).

like image 489
salle55 Avatar asked Sep 20 '09 11:09

salle55


3 Answers

If you are using C# 3.0, you can provide your helper methods as extension methods for the System.Web.UI.Control class, of which both System.Web.UI.Page and System.Web.UI.UserControl classes derive.

public static class ControlExtensions {
    public static void DoSomething(this Control obj) {
       // do something
    }
}

In the Page or UserControl:

this.DoSomething();
like image 83
mmx Avatar answered Nov 02 '22 08:11

mmx


I have exactly the same issue. Here is my solution.

I defined empty interface

public interface ISecurableWebObject
    {
    }

Then I defined class that has extesion methods for the interface above

public static class ISecurableWebObjectExtender
    {
        public static bool ExtensionMetotX(this ISecurableWebObject obj)
        {
            return ...;
        }
    }

I inherited ISecurableWebObject in Page and WebUserControl classes so dublicate definition has gone.

 public partial class UcExample : System.Web.UI.UserControl, ISecurableWebObject
    {
        protected void Page_Load(object sender, EventArgs e)
        {
             if(this.ExtensionMetotX() == true)
             { ... }
        }
    }
like image 21
Hamdi Avatar answered Nov 02 '22 08:11

Hamdi


Hmm...sounds like the usage of the famous Helper classes, basically classes like

public static class StringHelper
{

    public static string Replace(...)
    {
        ...
    }

}

and calling them like

string x = StringHelper.Replace(...);

Although I'm often quite concerned about having too much of these Helpers because they really somehow remember to procedural programming with the static methods in them. On the other side, such functionality as you describe it (in some base classes that extend UserControl and Page) are usually of this type.

What I often then do is to have a StringHelper and a corresponding StringExtender who's logic inside calls the static methods of the Helper class. In this way you can use the functionality with the new C# extension methods or directly through the static class as usual.

like image 1
Juri Avatar answered Nov 02 '22 09:11

Juri