Xtreme Xtreme - 1 month ago 10
ASP.NET (C#) Question

Where should I put convert time funtion to access it in several controllers?

I have a function like this to convert DateTime.

private static string ToRelativeDate(DateTime dateTime)
{
var timeSpan = DateTime.Now - dateTime;

if (timeSpan <= TimeSpan.FromSeconds(60))
return timeSpan.Seconds > 5 ? string.Format("{0} sekunder sedan", timeSpan.Seconds) : "Precis nu";

if (timeSpan <= TimeSpan.FromMinutes(60))
return timeSpan.Minutes > 1 ? String.Format("{0} minuter sedan", timeSpan.Minutes) : "minut sedan";

if (timeSpan <= TimeSpan.FromHours(24))
return timeSpan.Hours > 1 ? String.Format("{0} timmar sedan", timeSpan.Hours) : "timme sedan";

if (timeSpan <= TimeSpan.FromDays(30))
return timeSpan.Days > 1 ? String.Format("{0} dagar sedan", timeSpan.Days) : "igår";

if (timeSpan <= TimeSpan.FromDays(365))
return timeSpan.Days > 30 ? String.Format("{0} månader sedan", timeSpan.Days / 30) : "månad sedan";

return timeSpan.Days > 365 ? String.Format("{0} år sedan", timeSpan.Days / 365) : "år sedan";
}


I have it in the controller where I use ut right now but I need to use it in multiple controllers. Where should I put it (don't want to duplicate my code) and how can I call it? Guessing it should be in a separated class, but where? My file structure is a standard asp.net MVC project.

Answer

It's something of a matter of opinion, and also a matter of exactly how widely you want to share this code.

The simplest option is to put it into another class. In an MVC project, you have the option to put this file anywhere, but convention suggests you either put it in the "Models" folder, or, since it's not strictly a model but a utility function, you could create a new folder for "Utility" code. The class could look like this:

public class DateUtils
{
  public static string ToRelativeDate(DateTime dateTime)
  {
    //your code here
  }
}

Then in your controller, you can simply write:

string result = DateUtils.ToRelativeDateTime(myDateTimeVar);

(N.B. If you have put your class in a different namespace than the controller, then you'll need to include the namespace at the top of the controller class with a using statement.)

If you want to share this and other generic functionality more widely, for instance so that you can easily re-use it in other applications, you could make an entirely separate VS project for it and then include that in your solution and reference it in the MVC project. That way you can include the same functionality in other solutions in future without duplicating the files.

The last option I will suggest is to compile that project into a NuGet package (which you can either share to the world online at nuget.org, or just put on an internal file share for your own/your team's use). That way you can maintain multiple versions in different solutions if necessary (and work on new versions without affecting other development work), plus it gives you an easy way to manage how you include re-usable code in your applications.

The concept of code re-use is essentially the same no matter what scale you are at - you abstract and separate the code out to the level you think necessary for all the scenarios you can envisage it might be used, and then share it using the simplest method that will allow it to (securely) reach all the people who might need it. You aim to write it once, and maintain it/version it in one place in your source control, without copy/pasting multiple identical files around the place.

Comments