I'm having some problem to implement a clonable version of the ControllerContext
. I need this because i'm trying to cache it and access it later in a part of my system that can't access this information and i can't pass it as a parameter. This is what i have achieved:
public class ControllerContextClonable : ControllerContext, ICloneable
{
public object Clone()
{
var cloneContext = (ControllerContext)MemberwiseClone();
return cloneContext;
}
public static List<ControllerContextClonable> ToList( ControllerContextClonable context )
{
return new List<ControllerContextClonable>{ context };
}
}
The problem is: When i'm caching it, i have the ControllerContext
but i need to pass to my caching method a ControllerContextClonable
object. How can i convert my ControllerContext
to ControllerContextClonable
? This is how i'm trying to pass it and the caching methods:
//If i try to call my method with the safe convertion i get a null object
CacheMethods.SetContextCache( context as ControllerContextClonable );
public static void SetContextCache( ControllerContextClonable context, string cacheKey = "SiteContext" )
{
CacheList<ControllerContextClonable>.RemoveCache( cacheKey );
CacheList<ControllerContextClonable>.Add( new List<ControllerContextClonable> { context } );
CacheList<ControllerContextClonable>.CacheIt( cacheKey );
}
public static ControllerContextClonable GetCachedContext( string cacheKey = "SiteContext" )
{
CacheList<ControllerContextClonable>.LoadCached( cacheKey );
if( CacheList<ControllerContextClonable>.LoadCached( cacheKey ) )
{
return CacheList<ControllerContextClonable>.DataList.FirstOrDefault();
}
return null;
}
How can i correctly convert the executing ControllerContext
to my ControllerContextClonable
class to be used on the cache ? Also if i'm doing this wrong could anyone point me the right way ?
EDIT:
I'm going to explain my workaround: I'm creating a template parsing system where it will parse a template using a tag system, in this tag the user will provide the ViewModel
class and the template View
he wishes to use.
Having that i will instantiate this ViewModel
using this code to do it. This code doesn't let me pass a controllercontext as a parameter, i tried to modify it to accept the parameter but it would give me an Common language runtime detected an invalid program
error.
Now the problem, on this certain case i have a ViewModel that needs to fetch information that was saved on a ViewBag
using my Filter
. The only solution i could think of was cache the context and access it but i'm failing on the convertion part. Could i solve this any other way ? What i need is to access the Context
inside some methods that are called by my ViewModel
constructor.
This is the ViewModel
:
public class LanguageTestViewModel
{
//Properties
public LanguageTestViewModel() { Initialize( "", "", 0 ); }
public LanguageTestViewModel( string where, string order, int take ) { Initialize( where, order, take ); }
private void Initialize( string where, string order, int take )
{
var Context = CacheMethods.GetCachedContext();
var EList = HelperMethods.CreateSelectListItem( "SELECT A STATE", Context );
var States = CacheMethods.GetStatesForm( Context );
//Do stuff
}
}
EDIT 2
I have to change my storage method since i'm using HttpRuntime.Cache
, which is a shared storage and i can't storage context information on that can change based on user, device, location, etc.. Said that i need this information on a per-user
storage. Any ideas ?