I currently have a VaryByCustom function implemented in classes that implement the interface IOutputCacheVaryByCustom
public interface IOutputCacheVaryByCustom
{
string CacheKey { get; }
HttpContext Context { get; }
}
A class that implements this interface has several conventions, the class name will be "OutputCacheVaryBy_______", where the space is the value that is passed from the variableByCustom property on the pages. Another convention is that the Context will be set via the constructor injection.
I am currently basing this on an enumeration and a switch statement similar to
public override string GetVaryByCustomString(HttpContext context,
string varyByCustomTypeArg)
{
if (context.Request.RequestType.Equals("POST"))
{
return "post" + DateTime.Now.Ticks;
}
var varyByCustomType = EnumerationParser.Parse<VaryByCustomType?>
(varyByCustomTypeArg).GetValueOrDefault();
IOutputCacheVaryByCustom varyByCustom;
switch (varyByCustomType)
{
case VaryByCustomType.IsAuthenticated:
varyByCustom = new OutputCacheVaryByIsAuthenticated(context);
break;
case VaryByCustomType.Roles:
varyByCustom = new OutputCacheVaryByRoles(context);
break;
default:
throw new ArgumentOutOfRangeException("varyByCustomTypeArg");
}
return context.Request.Url.Scheme + varyByCustom.CacheKey;
}
Since I always know that the class will be OutputCacheVaryBy + varyByCustomTypeArg, and the only argument to the constructor will be context, I realized that I could bypass this illustrious if else block and simply create an instance of my own object with Activator.
, , , Activator . , , Activator ?
http://www.smelser.net/blog/post/2010/03/05/When-Activator-is-just-to-slow.aspx, , , T.