ProgrammingJavascriptCareerProductivityGadgetsASP.NETWeb Design
Reuse and Reduce

Implementing DLL's For Your ASP.NET Site

Written by
Published on
Modified on
Filed under
Implementing DLL's For Your ASP.NET Site

Code re-use is at the heart of any good developer and it is a very welcomed ally when the time arrives. The last thing you want to do when working on a project is to find yourself making a small change and then having to deploy it across a dozen different pages. Write once, use everywhere. By defining our own DLL's with our custom generalized code we can keep all of our most commonly used code in a single project and then deploy it across all of our other projects.

Create Your Project

To create your DLL you'll need either Express 2012 for Desktop or Express 2013 for Desktop. I'm currently using 2012 as I haven't had much time to update to the latest, but the process is the same.

Visual Studio Express for Desktop
Create Your New Project

From there, you can either choose from the Visual C# templates or the Visual Basic templates. Either way, you'll have the same templates to choose from. Select the Class Library template and we'll get on our way. DLL's are essentially the same as EXE's, so we can code them just like we do other windows projects.

Visual Studio Express Class Library

Our New Project

Our DLL consists of the namespace, given the name of the project, followed by classes that we define ourselves. For our example, we'll make our first class DateUtil which will handle generalized date and time functions and it will be in the generalized Utilities namespace. Pretty straightforward after that. You can pretty much add any and all code that you may want after that.

VS Express class

Let's add our first function to the DateUtil class that we defined. Our custom function will be GetTimeSince. It returns the "x minutes ago" format given a date being passed in. This type of function is exactly what should be in our DLL. It's not tied to any single project and it can be used across a wide spectrum of projects.


public static string GetTimeSince(DateTime date)
{
       TimeSpan ts = DateTime.Now.ToUniversalTime().Subtract(date);
       int intDays = ts.Days;
       int intHours = ts.Hours;
       int intMinutes = ts.Minutes;
       int intSeconds = ts.Seconds;

       if (intDays > 0)
           return string.Format("{0} days", intDays);

       if (intHours > 0)
           return string.Format("{0} hours", intHours);

       if (intMinutes > 0)
           return string.Format("{0} minutes", intMinutes);

       if (intSeconds > 0)
           return string.Format("{0} seconds", intSeconds);

       return "a bit";
}

That should be good enough for us to get started using our new library. So let's build our solution, and fix up any bugs we encountered. Everything looks OK with our sample, so we're good to go.

VS Express Build

Because we're building our project in DEBUG mode for now, we'll check our debug folder in our bin for our newly created DLL.

VS Express DLL

That library is now ready to be incorporated in our projects. I've created a test web project in Visual Studio 2013 to see it in action.

In order to add a reference to our library right click on our bin directory and select Add Reference. You'll get the following dialog:

From here browse to your library project that we created above and navigate to the debug directory where we have our newly created DLL. Click "OK" and we're good to go.

We can now start to reference our library in our web pages and make calls to our functions.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Utilities;

public partial class TestLibrary : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SetTimeAgo();
    }

    private void SetTimeAgo()
    {
        DateTime dt = DateTime.UtcNow.AddHours(-2);
        output.Text = DateUtil.GetTimeSince(dt);
    }
}

Keeping reusability in the back of the mind whenever developing will yield easier to maintain and more scalable applications. If any updates are needed to the functions in your library, you rebuild a new version, and your other applications using the library will automatically update themselves leading to a more seamless experience.

Walter Guevara is a software engineer, startup founder and currently teaches programming for a coding bootcamp. He is currently building things that don't yet exist.

Comments

No messages posted yet

Developer Poll

Q:

Stay up to date

Sign up for my FREE newsletter. Get informed of the latest happenings in the programming world.

Add a comment

Keep me up to date on the latest programming news
Add Comment

Stay up to date

Get informed of the latest happenings in the programming world.

No thanks