- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
Silverlight converter: Async Operation updating Entity. Ok?
Joined: 06-Sep-2011
Hello,
We are using LLBLGEN as our DAL for a Silverlight 5, RIA, OOB application. We have an entity, LicenseEntity, that contains an expiration date, which is stored in UTC in the database. We need to convert the expiration date from UTC to another, specific timezone (not just the Silverlight client's local timezone). For example, the user is in California, the expiration date is in UTC, and the expiration date needs to be converted to Alaska Standard time zone. We also need to convert a DateTime from a specific time zone, back to UTC.
Unfortunately, TimeZoneInfo.ConvertTimeToUtc() and TimeZoneInfo.ConvertTimeFromUtc() do not exist in Silverlight 5, so I've created a Domain Data Service with three functions: One that returns the list of TimeZoneInfoId values from the server, and the others to provide the aforementioned conversions.
So, everything looks okay, right? Hmmmm... Well, I need to figure out how to pass in a second parameter to the converter (a time zone id), but this will not be difficult.
But... I don't like one aspect of my solution... As you can see in the following code, it is making an asynchronous call to the domain data service to perform the conversion. Going from the database (UTC) to the UI control to display the result does not seem to be risky, but going the other way is where I am concerned... I need to ensure that our LLBLGEN LicenseEntity will persist the value that is returned from our domain data service... But, I suspect that my current code would not always work properly. How can I ensure that the Entity stores the correct result? I am worrying about the sequence of operations and saving the correct value into the LLBLGEN entity.
I would really appreciate your suggestions and want to thank you for reading my question.
Mike
P.S. I was totally shocked that TimeZoneInfo in Silverlight lacked the ability to convert UTC <--> Specific Time Zone! This is such a common task in a globally aware Silverlight application. If my solution doesn't have any major technical flaws, hopefully it will help others. If I cannot make this solution work, I will probably implement the Olson time zone database in our application. Hopefully, my domain data service solution will work!
<summary> Converts a DateTime value from UTC to a specific time zone.
Generally, converting DateTime value in UTC from the database
to a specifc time zone, which is displayed in the UI.
</summary>
[Description("Converts DateTime values between GMT and time zones.")]
public class DateTimeGMT2LocalConverter : IValueConverter
{
/// <summary>
/// Convert a DateTime from GMT to a particular time zone.
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter">Format specification</param>
/// <param name="culture"></param>
/// <returns></returns>
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(DateTime))
{
return null;
}
if (value is DateTime)
{
DateTime dt = System.Convert.ToDateTime(value);
DateTime result = new DateTime(DateTime.MinValue.Ticks);
if (dt == null || dt == DateTime.MinValue)
{
return null;
}
TimeZoneDomainContext context = new TimeZoneDomainContext();
InvokeOperation<DateTime> io = context.ConvertUtcToTargetTimeZone(dt, "??? Need to get the TimeZoneInfoId from a parameter");
io.Completed += (s1, ea1) =>
{
if (!io.HasError)
{
result = io.Value;
}
};
// Since this is a asynchronous operation, the initial result will be returned
// but its value will be updated when the asyc operation completes.
return result;
}
else
{
return null;
}
}
/// <summary> Converts DateTime value from a specific time zone to UTC.
/// Generally, converting DateTime value from a UI control using a
/// specifc time zone to UTC for storage in the database.
/// </summary>
/// <param name="value"></param>
/// <param name="targetType"></param>
/// <param name="parameter"></param>
/// <param name="culture"></param>
/// <returns></returns>
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (targetType != typeof(DateTime))
{
return null;
}
if (value is DateTime)
{
DateTime dt = System.Convert.ToDateTime(value);
DateTime result = new DateTime(DateTime.MinValue.Ticks);
if (dt == null || dt == DateTime.MinValue)
{
return null;
}
// Create an unspecified version of the value.
DateTime val = new DateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, DateTimeKind.Unspecified);
// Convert the DateTime (unspecific) to Universal time using the time zone.
TimeZoneDomainContext context = new TimeZoneDomainContext();
InvokeOperation<DateTime> io = context.ConvertSourceTimeZoneToUtc(val, "??? Need to get the TimeZoneInfoId from a parameter");
io.Completed += (s1, ea1) =>
{
if (!io.HasError)
{
result = io.Value;
}
};
// Since this is a asynchronous operation, the initial result will be returned
// but its value will be updated when the asyc operation completes.
return result;
}
else
{
return null;
}
}
}