Create A Utility Class Containing Site-Wide Functions

3.08 (12 votes)

In Classic ASP, the typical model for managing site-wide functions is to create one or more server-side includes containing libraries of code. The OOP nature of ASP.NET offers a much more elegant solution.

I used to love include files in Classic ASP. I would group together similar functions and subroutines into collections of files, and selectively include them at the top of a page based on the page's requirement. ASP.NET doesn't support this approach, and one of the first questions I asked in a newsgroup relating to ASP.NET was how to manage this?

One very kind poster told me to create a "utility" class, and reference it from my code-behind. Being completely new to OOP, I had no idea how to approach this, so they even posted some sample code, based on what they were using in professional applications:

public sealed class Utils // sealed to ensure the utility class won't be inherited

{
private Utils() {} // Ensure you cannot instantiate an object of this type
    
    //Add static methods which means you can reference them
    //anywhere in the project simply by using Utils.
    //This one keeps the name of the connection string in just 2 places - 
    //once in the Web.Config, and once in the Utils class.

    public static string GetConnString()
    {
        return WebConfigurationManager.ConnectionStrings["myConnString"].ConnectionString;
    }
}

To use this method is simple. Whenever a connection object is created one of the arguments passed to the constructor is the connection string. This is done as follows:

string connectionString = Utils.GetConnString();

This simple snippet also made me realise the difference between static and instance methods. And the best thing? Intellisense loves to colour "Utils" light blue.

Date Posted:
Last Updated:
Posted by:
Total Views to date: 13721

1 Comment

- Patrick Oriyomi

Create A Utility Class Containing Site-Wide Functions.


Do you have VB version of this Utils class?

Recent Comments

Thomas 05/03/2018 00:59
In response to I'm Not Writing A Book On Razor Pages
There's a typo on this page: = true)] should be [BindProperty(SupportsGet = true)]...

Rolf Herbert 04/03/2018 19:25
In response to I'm Not Writing A Book On Razor Pages
So is MS deprecating razor Web Pages..? Is it dead..? I wish they would stop killing things so its...

Borut 17/02/2018 12:59
In response to I'm Not Writing A Book On Razor Pages
Mike, is it possible that Web Pages and Razor Pages "live" together in one web application? I a I...

hrboyce 09/02/2018 04:44
In response to I'm Not Writing A Book On Razor Pages
Mike, First thanks for doing this but I have to ask, any chance you would consider converting one of...

aziz sallam 07/02/2018 10:18
In response to I'm Not Writing A Book On Razor Pages
u are a great man...

Satyabrata Mohapatra 31/01/2018 11:36
In response to I'm Not Writing A Book On Razor Pages
This is a great news!!!! Thanks...

tangdf 30/01/2018 07:25
In response to I'm Not Writing A Book On Razor Pages
=> { o.ConfigureFilter(new IgnoreAntiforgeryTokenAttribute()); }); The extension method does...

Obinna Okafor 30/01/2018 04:02
In response to I'm Not Writing A Book On Razor Pages
Thank you very much. I would like to see a project built from scratch using Razor Pages. And it show...

rachida Dukes 31/10/2017 13:52
In response to Customising Identity in Razor Pages
Thanks again for this wonderful tutorial. I followed all the steps in this section called: Adding...

Rachida 31/10/2017 12:06
In response to Customising Identity in Razor Pages
Thanks very much for this wonderful tutorial, it helped a lot....