I have added the following property to my ApplicationUser
class, which is supposed to return current user on top of other results.
public static IComparer<string> IdComparer
{
get
{
return Comparer<string>.Create((x, y) =>
{
var curUser = HttpContext.Current.User;
if (curUser != null)
{
var curId = curUser.Identity.GetUserId();
if (x == curId)
return -1;
else if (y == curId)
return 1;
}
return string.Compare(x, y);
});
}
}
Anyway, does generating a comparer cost more than storing it? Should I add a static field and return a singleton for this property?
I'm thinking about returning the same comparer:
private static object sync = new object();
private static IComparer<string> _IdComparer;
public static IComparer<string> IdComparer
{
get
{
if (_IdComparer == null)
lock (sync)
if (_IdComparer == null)
_IdComparer = Comparer<string>.Create((x, y) =>
{
var curUser = HttpContext.Current.User;
if (curUser != null)
{
var curId = curUser.Identity.GetUserId();
if (x == curId)
return -1;
else if (y == curId)
return 1;
}
return string.Compare(x, y);
});
return _IdComparer;
}
}
Is this safe? Any corrections or enhancements?