Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SparkLoader
- {
- private readonly SparkViewFactory _sparkViewFactory;
- private readonly ActionContext _actionContext;
- private static readonly IDictionary<BuildDescriptorParams, ISparkViewEntry> _cache;
- static SparkLoader()
- {
- _cache = new Dictionary<BuildDescriptorParams, ISparkViewEntry>();
- }
- public SparkLoader(SparkViewFactory sparkViewFactory, ActionContext actionContext)
- {
- _sparkViewFactory = sparkViewFactory;
- _actionContext = actionContext;
- }
- public virtual SparkView LoadPartial(string path)
- {
- var descriptorParams = getDescriptorParams(path, null);
- var viewEntry = findViewEntry(descriptorParams);
- var view = buildResult(viewEntry);
- return (SparkView)view;
- }
- public virtual SparkView LoadView(string path, string master)
- {
- var descriptorParams = getDescriptorParams(path, master);
- var viewEntry = findViewEntry(descriptorParams);
- var view = buildResult(viewEntry);
- return (SparkView)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);
- return _sparkViewFactory.Engine.CreateEntry(descriptor);
- }
- 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();
- }
- return view;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement