C#, EF Entity Framework, Reflection, UTC

Converting all date/time properties of an object graph to local time from UTC

Backstory: In a project I am working on, I am storing all times in the database as UTC. When I get the values from the database, I want to convert the times to local time to be displayed in my client. There’s many articles on this, that involve changing to local time on the server before shipping to client, which I didn’t care for (for example, hooking into the ObjectMaterialized event on the ObjectContext and using reflection). This had to be done on the client itself to take advantage of the client’s time zone.

I didn’t want to convert them all by hand, either. What if I added D/T properties later? If it’s nullable, then it’s an extra line of code to check that it’s not null first. What about an object graph (nested objects)? It sounded like a lot of work, until I cobbled together this code from a couple sources and reworked it a bit to do what I want:

	/// <summary>
	/// Since all dates in the DB are stored as UTC, this converts dates to the local time using the Windows time zone settings.
	/// </summary>
	public static class AllDateTimesAsUTC
	{

		/// <summary>
		/// Specifies that an object's dates are coming in as UTC.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="obj"></param>
		/// <returns></returns>
		public static T AllDatesAreUTC<T>(this T obj)
		{
			if (obj == null)
			{
				return default(T);
			}
			IterateDateTimeProperties(obj);
			return obj;
		}

		private static void IterateDateTimeProperties(object obj)
		{
			if (obj == null)
			{
				return;
			}
			var properties = obj.GetType().GetProperties();
			//Set all DaetTimeKinds to Utc
			foreach (var prop in properties)
			{
				var t = prop.PropertyType;
				if (t == typeof(DateTime) || t == typeof(DateTime?))
				{
					SpecifyUtcKind(prop, obj);
				}
				else if (t.IsEnumerable())
				{
					var vals = prop.GetValue(obj, null);
					if (vals != null)
					{
						foreach (object o in (IEnumerable)vals)
						{
							IterateDateTimeProperties(o);
						}
					}
				}
				else
				{
					var val = prop.GetValue(obj, null);
					if (val != null)
					{
						IterateDateTimeProperties(val);
					}
				}
			}
			//properties.ForEach(property => SpecifyUtcKind(property, obj));
			return; // obj;
		}

		private static void SpecifyUtcKind(PropertyInfo property, object value)
		{
			//Get the datetime value
			var datetime = property.GetValue(value, null);
			DateTime output;

			//set DateTimeKind to Utc
			if (property.PropertyType == typeof(DateTime))
			{
				output = DateTime.SpecifyKind((DateTime)datetime, DateTimeKind.Utc);
			}
			else if (property.PropertyType == typeof(DateTime?))
			{
				var nullable = (DateTime?)datetime;
				if (!nullable.HasValue) return;
				output = (DateTime)DateTime.SpecifyKind(nullable.Value, DateTimeKind.Utc);
			}
			else
			{
				return;
			}

			Debug.WriteLine("     ***** Converted date from {0} to {1}.", datetime, output);
			datetime = output.ToLocalTime();

			//And set the Utc DateTime value
			property.SetValue(value, datetime, null);
		}
		internal static Type[] ConvertibleTypes = {typeof(bool), typeof(byte), typeof(char),
   typeof(DateTime), typeof(decimal), typeof(double), typeof(float), typeof(int), 
   typeof(long), typeof(sbyte), typeof(short), typeof(string), typeof(uint), 
   typeof(ulong), typeof(ushort)};

		/// <summary>
		/// Returns true if this Type matches any of a set of Types.
		/// </summary>
		/// <param name="types">The Types to compare this Type to.</param>
		public static bool In(this Type type, params Type[] types)
		{
			foreach (Type t in types) if (t.IsAssignableFrom(type)) return true; return false;
		}

		/// <summary>
		/// Returns true if this Type is one of the types accepted by Convert.ToString() 
		/// (other than object).
		/// </summary>
		public static bool IsConvertible(this Type t) { return t.In(ConvertibleTypes); }

		/// <summary>
		/// Gets whether this type is enumerable.
		/// </summary>
		public static bool IsEnumerable(this Type t)
		{
			return typeof(IEnumerable).IsAssignableFrom(t);
		}

		/// <summary>
		/// Returns true if this property's getter is public, has no arguments, and has no 
		/// generic type parameters.
		/// </summary>
		public static bool SimpleGetter(this PropertyInfo info)
		{
			MethodInfo method = info.GetGetMethod(false);
			return method != null && method.GetParameters().Length == 0 &&
				 method.GetGenericArguments().Length == 0;
		}

	}

How do you use it? To change aall dates on an object sent to the client via WCF, this is how it’s done:

		void svc_ZOut_GetZOutSummaryCompleted(object sender, ZOut_GetZOutSummaryCompletedEventArgs e)
		{
			svc.ZOut_GetZOutSummaryCompleted -= new EventHandler<ZOut_GetZOutSummaryCompletedEventArgs>(svc_ZOut_GetZOutSummaryCompleted);
			svc = null;
			var whenDone = (Action<bool, ZOutResult>)e.UserState;
			if (e.Error != null)
			{
				FireOnExceptionRaised(e.Error);
				whenDone(false, null);
			}
			else
			{
				var res = e.Result.AllDatesAreUTC();
				FireOnSessionReceived(res.IsError, res.Session);
				if (res.IsError == true)
				{
					whenDone(false, null);
				}
				else
				{
					whenDone(true, res.Result);
				}
			}
		}

That’s it! It handles a whole object graph, DateTime and nullable DateTime obejcts.

C#, Generics, Reflection, Silverlight, Web Development

Map query string parameters to properties of an object, easily`

So, how many people have done this?


URL: http://.../something.aspx?PersonID=4&ChildID=16

void () {
// Assuming o = some object with properties PersonID and ChildID
PersonWithChild o = new PersonWithChild();
string _personID = Request.QueryString["PersonID"];
if (int.TryParse(_personID, out personID) {
o.PersonID = personID;
}
string _childID = Request.QueryString["ChildID"];
if (int.TryParse(_childID, out childID) {
o.ChildID = childID;
}
...
}

What a pain! This kind of plumbing is unnecessary, although frameworks like MVC have this taken care of.
Silverlight uses the concept of querystrings to pass data between pages of a navigation business application, so the code below can be used with Silverlight in addition to whatever .Net-based web framework of your choosing.

(I know code is cut off, copy and paste it and it will be fine)

Non-SL:

void init() { // This is where you initialization code may be
PersonWithChild o = new PersonWithChild();
MapQueryString(Request.QueryString, ref o, false);
// Properties are now set
}

Silverlight:

void init() { // This is where you initialization code may be
PersonWithChild o = new PersonWithChild();
// Silverlight querystring object is an IDictionary so use extension method ToQueryString()
MapQueryString(NavigationContext.QueryString.ToQueryString(), ref o, false);
// Properties are now set
}

Here are the functions: Put them into a static class in your project.

///

/// Transforms a dictionary of key/value pairs strongly typed as strings
/// into a querystring that can be parsed by MapQueryString of T.
///

///
///
public static string ToQueryString(this IDictionary kvps) {
string query = "";
foreach (var key in kvps) {
query += string.Format("{0}={1}&", key.Key, key.Value);
}
return query.TrimEnd('&');
}

///

Takes a query string and maps each key value pair to public properties of an object of type TEntity.
///

/// The class of the type of object.
/// A string containing key/value pairs.
/// The object whose properties will be populated.
/// If true, the property names and keys from the key-value pairs must match exactly.
/// An array of strings that separates each key-value pair. Passing null will default the list to {&, &}.
/// The string that separates each key and value in a key-value pair. By default this is "=".
/// If multiple parameters with the same key are specified, they are concatenated with this string, by default ",".
///
/// Since a query string can have multiple keys with the same name, a Dictionary generic was not used.
///
public static void MapQueryString(string queryString, ref TEntity o, bool caseSensitive = false, string[] separatorStrings = null, string keyValueSeparator = "=", string multipleValueJoinSeparator = ",") {
if (separatorStrings == null) { separatorStrings = new string[] { "&", "&" }; }
List kvps = GetKeyValuePairs(queryString, separatorStrings, keyValueSeparator);
Type type = o.GetType();
var properties = type.GetProperties(BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.Public);
foreach (var property in properties) {
List kv;
// Get all matching values
if (caseSensitive) {
kv = kvps.Where(p => p.Key == property.Name).ToList();
} else {
kv = kvps.Where(p => p.Key.ToLower() == property.Name.ToLower()).ToList();
}
if (kv.Count > 0) {
// Concatenate the values
string value = "";
foreach (var kvp in kv) {
if (value.Length > 0) { value += multipleValueJoinSeparator; }
value += kvp.Value;
}
object target = null;
Type ptype = property.PropertyType;
if (ptype.FullName.Contains("Nullable")) {
ptype = ptype.GetProperty("Value").PropertyType;
}
if (ptype != typeof(string)) {
try {
target = ptype.InvokeMember("Parse", BindingFlags.InvokeMethod, null, target, new object[] { value });
property.SetValue(o, target, null);
} catch (Exception ex) { }
} else {
property.SetValue(o, value, null);
}
}
}
//return o;
}

///

Turns a query string into a map of key-value pairs.
///

///
///
///
///
public static List GetKeyValuePairs(string queryString, string[] separatorStrings, string keyValueSeparator) {
List output = new List();
string[] kvps = queryString.Split(separatorStrings, StringSplitOptions.RemoveEmptyEntries);
foreach (string kvp in kvps) {
string[] t = kvp.Split(new string[] { keyValueSeparator }, StringSplitOptions.RemoveEmptyEntries);
NonUniqueKeyValuePairStruct op = new NonUniqueKeyValuePairStruct();
if (t.Length == 1) {
op.Key = null;
op.Value = t[0];
} else {
op.Key = t[0];
op.Value = t[1];
}
output.Add(op);
}
return output;
}

///

Used to hold non-unique key-value pairs.
///

public struct NonUniqueKeyValuePairStruct
{
public string Key;
public string Value;
}