Create A Utility Class Containing Site-Wide Functions

2.91 (11 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.

You might also like...

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

1 Comment

- Patrick Oriyomi

Create A Utility Class Containing Site-Wide Functions.


Do you have VB version of this Utils class?

Recent Comments

ojorma 17/06/2017 09:24
In response to Razor Pages - The Elevator Pitch
Finally I can say goodbye to webforms...

Mau 06/06/2017 08:58
In response to Razor Pages - The Elevator Pitch
I am missing the razor view code. Thank you for your article. Design pattern should improve the of...

Obinna Okafor 26/05/2017 16:16
In response to Razor Pages - The Elevator Pitch
Thank you for this wonderful piece, Mike. I need more of these...Keep them coming - anything more...

Cyrus 26/05/2017 06:00
In response to Razor Pages - The Elevator Pitch
There are some concern about razor pages performance. Is it faster or slower than MVC? would you a a...

Cyrus 26/05/2017 05:44
In response to Razor Pages - Understanding Handler Methods
well done, thank you....

Satyabrata Mohapatra 23/05/2017 11:41
In response to Razor Pages - Understanding Handler Methods
Nice and easy !! Great post....

Cyrus 16/05/2017 19:55
In response to Razor Pages - Getting Started With The Preview
There is something wrong related to microsoft.dotnetcore.mvc.taghelpers! if you remove it from page...

Cyrus 16/05/2017 10:18
In response to Razor Pages - Getting Started With The Preview
well done mike, it was very useful, I really appreciate that....

Satyabrata Mohapatra 16/05/2017 07:21
In response to Razor Pages - Getting Started With The Preview
Finally!!!! web pages in asp.net core!!! Super excited !!!! Thank u sir for sharing.....Awaiting on...

Daniele 14/03/2017 10:24
In response to Working With Zip Files In ASP.NET MVC
is it possible give to the user a progress bar of the zipping process? Thanks in advance. ...