Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.32 KB | None | 0 0
  1.     public class SparkLoader
  2.     {
  3.         private readonly SparkViewFactory _sparkViewFactory;
  4.         private readonly ActionContext _actionContext;
  5.         private static readonly IDictionary<BuildDescriptorParams, ISparkViewEntry> _cache;
  6.  
  7.         static SparkLoader()
  8.         {
  9.             _cache = new Dictionary<BuildDescriptorParams, ISparkViewEntry>();
  10.         }
  11.  
  12.         public SparkLoader(SparkViewFactory sparkViewFactory, ActionContext actionContext)
  13.         {
  14.             _sparkViewFactory = sparkViewFactory;
  15.             _actionContext = actionContext;
  16.         }
  17.  
  18.         public virtual SparkView LoadPartial(string path)
  19.         {
  20.             var descriptorParams = getDescriptorParams(path, null);
  21.             var viewEntry = findViewEntry(descriptorParams);
  22.             var view = buildResult(viewEntry);
  23.  
  24.             return (SparkView)view;
  25.         }
  26.  
  27.         public virtual SparkView LoadView(string path, string master)
  28.         {
  29.             var descriptorParams = getDescriptorParams(path, master);
  30.             var viewEntry = findViewEntry(descriptorParams);
  31.             var view = buildResult(viewEntry);
  32.             return (SparkView)view;
  33.         }
  34.  
  35.         private BuildDescriptorParams getDescriptorParams(string path, string master)
  36.         {
  37.             var parameters = _sparkViewFactory.DescriptorBuilder.GetExtraParameters(_actionContext);
  38.             var actionNamespace = _actionContext.ActionNamespace;
  39.             var actionName = _actionContext.ActionName;
  40.             return new BuildDescriptorParams(actionNamespace, actionName, path, master, false, parameters);
  41.         }
  42.  
  43.         private ISparkViewEntry findViewEntry(BuildDescriptorParams descriptorParams)
  44.         {
  45.             ISparkViewEntry viewEntry;
  46.             //TODO: IMPLEMENT A SMARTER LOCKING MECHANISM HERE
  47.             lock (_cache)
  48.             {
  49.                 if (_cache.TryGetValue(descriptorParams, out viewEntry))
  50.                 {
  51.                     // We have the entry in cache, but it has changed, we must invalidate the cache
  52.                     if (!viewEntry.IsCurrent())
  53.                     {
  54.                         _cache.Remove(descriptorParams);
  55.                         viewEntry = createViewEntry(descriptorParams);
  56.                         _cache.Add(descriptorParams, viewEntry);
  57.                     }
  58.                 }
  59.                 else
  60.                 {
  61.                     viewEntry = createViewEntry(descriptorParams);
  62.                     _cache.Add(descriptorParams, viewEntry);
  63.                 }
  64.             }
  65.             return viewEntry;
  66.         }
  67.  
  68.         private ISparkViewEntry createViewEntry(BuildDescriptorParams descriptorParams)
  69.         {
  70.             var searchedLocations = new List<string>();
  71.             var descriptor = _sparkViewFactory.DescriptorBuilder.BuildDescriptor(descriptorParams, searchedLocations);
  72.             return _sparkViewFactory.Engine.CreateEntry(descriptor);
  73.         }
  74.  
  75.         private ISparkView buildResult(ISparkViewEntry entry)
  76.         {
  77.             var view = entry.CreateInstance();
  78.             if (view is SparkView)
  79.             {
  80.                 var sparkView = (SparkView)view;
  81.                 sparkView.ResourcePathManager = _sparkViewFactory.Engine.ResourcePathManager;
  82.                 sparkView.CacheService = _sparkViewFactory.CacheServiceProvider.GetCacheService();
  83.             }
  84.             return view;
  85.         }
  86.  
  87.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement