Skip to content

Instantly share code, notes, and snippets.

@emiaj
Created March 17, 2011 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emiaj/874767 to your computer and use it in GitHub Desktop.
Save emiaj/874767 to your computer and use it in GitHub Desktop.
public class SparkLoader
{
private readonly ISparkViewFactory _sparkViewFactory;
private readonly ActionContext _actionContext;
private readonly IServiceLocator _serviceLocator;
private static readonly IDictionary<BuildDescriptorParams, ISparkViewEntry> _cache;
static SparkLoader()
{
_cache = new Dictionary<BuildDescriptorParams, ISparkViewEntry>();
}
public SparkLoader(ISparkViewFactory sparkViewFactory, ActionContext actionContext, IServiceLocator serviceLocator)
{
_sparkViewFactory = sparkViewFactory;
_actionContext = actionContext;
_serviceLocator = serviceLocator;
}
public virtual SparkView LoadPartial(string path)
{
return loadView(path, null);
}
public virtual SparkView LoadView(string path, string master)
{
return loadView(path, master);
}
private SparkView loadView(string path, string master)
{
var descriptorParams = getDescriptorParams(path, master);
var viewEntry = findViewEntry(descriptorParams);
var view = (SparkView)buildResult(viewEntry);
return view;
}
private BuildDescriptorParams getDescriptorParams(string path, string master)
{
var parameters = _sparkViewFactory.DescriptorBuilder.GetExtraParameters(_actionContext);
var actionNamespace = _actionContext.ActionNamespace;
var actionName = _actionContext.ActionName;
return new BuildDescriptorParams(actionNamespace, actionName, path, master, false, parameters);
}
private ISparkViewEntry findViewEntry(BuildDescriptorParams descriptorParams)
{
ISparkViewEntry viewEntry;
//TODO: IMPLEMENT A SMARTER LOCKING MECHANISM HERE
lock (_cache)
{
if (_cache.TryGetValue(descriptorParams, out viewEntry))
{
// We have the entry in cache, but it has changed, we must invalidate the cache
if (!viewEntry.IsCurrent())
{
_cache.Remove(descriptorParams);
viewEntry = createViewEntry(descriptorParams);
_cache.Add(descriptorParams, viewEntry);
}
}
else
{
viewEntry = createViewEntry(descriptorParams);
_cache.Add(descriptorParams, viewEntry);
}
}
return viewEntry;
}
private ISparkViewEntry createViewEntry(BuildDescriptorParams descriptorParams)
{
var searchedLocations = new List<string>();
var descriptor = _sparkViewFactory.DescriptorBuilder.BuildDescriptor(descriptorParams, searchedLocations);
var viewEntry = _sparkViewFactory.Engine.CreateEntry(descriptor);
return viewEntry;
}
private ISparkView buildResult(ISparkViewEntry entry)
{
var view = entry.CreateInstance();
if (view is SparkView)
{
var sparkView = (SparkView)view;
sparkView.ResourcePathManager = _sparkViewFactory.Engine.ResourcePathManager;
sparkView.CacheService = _sparkViewFactory.CacheServiceProvider.GetCacheService();
sparkView.ServiceLocator = _serviceLocator;
}
return view;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment