tags:

views:

226

answers:

3

I have a set of functions I want to be available to my web pages and user controls in my c# .net 3.5 web project. My standard approach for pages is to create a "base page" that extends the System.Web.UI.Page class and then have my pages inherit from this, rather than directly from the Page class.

I now want to expose some of these functions to my web controls (ascx) and web services. I can think of a number of ways to do this, but they seem a little clumsy and I think I'm missing a trick.

Is there an easy way to provide some common functions to both my pages, web services and controls using inheritance, or do I need to wrap these functions in a class that all of them can access?

An example to clarify:

  • I have a singleton that handles most functionality for my web application.
  • At the start of each request I want to check that the class exists in the web cache and initialise it if not.

Initially this was handled in a page base that the pages all used. Now I need to be able to access my singleton safely from services and controls, with the same checks. I have therefore extracted the checking and initialisation logic into another class, that then each of my base page, control and web service, all instantiate. Even with this model I have the same code repeated in 3 places (each of my base classes for controls, ws and pages), albeit not much code, this seems wrong too!

It works, but it seems clumsy...I look forward to you guys humbling me with your wisdom!

+1  A: 

Sounds to mee like a case of aspect-oriented programming. .NET is ill equipped for this. I'm afraid that your solution is one of the best.

Alternatively perhaps you can move all or some of those functions to a static class/singleton and then use that class from your aspx/ascx/asmx? Not much in the way of inheritance, but at least less code duplication.

Vilx-
+1  A: 

My solution to this is to put all the methods and functions I want to share in my base master page class. I then put an equivalent for each method and function in the user control base class as follows:

    //Property in masterpage base
    public string QsSearchTerm
    {
        get
        {
            if (!String.IsNullOrEmpty(Request.QueryString["q"]))
            {
                return Helpers.SanitiseString(Server.UrlDecode(Request.QueryString["q"]));
            }
            return String.Empty;
        }
    }

    //Property in usercontrol base
    public string QsSearchTerm
    {
        get
        {
            if (Page.Master is BaseMasterPage)
            {
                return ((BaseMasterPage)Page.Master).QsSearchTerm;
            }
            return string.Empty;
        }
    }

What this doesn't help with, is your code repetition with web service base classes. I would think that refactoring the above into a class with a constructor that accepts an HttpContext instance would be the way forward. You can then expose a singleton instance of this class in your base web service, master page, user control, page etc.

Hope this helps, but I too would be interested in hearing if there's a better way.

Paul Suart
+1  A: 

In your Singleton you could provide a Strategy interface to allow variations of the code depending on the configured environment. This would allow you to switch between web/windows/wcf...and so on.

Adam Fyles