Как выполнить обновление представления во время выполнения при использовании ResourceViewLocationProvider

Это сработало для меня:

    self.gnuplot_bt = Button(
        self.run_but_container, text="Plot with Gnuplot", font="Helvetica", command=self.gnuplot,
        highlightbackground ="#8EF0F7", pady=2, relief=FLAT
    )
0
задан Xiao Wii 16 January 2019 в 08:12
поделиться

1 ответ

Ну, я, наконец, выяснил, как сделать это сам, на тот случай, если кто-то еще захочет использовать тот же метод, что и я. Вот как вы обновляете свое представление в памяти:

  1. Make интерфейс
    public interface INewViewLocationResultProvider
    {
        bool UseCachedView { get; set; }
        ViewLocationResult GetNewerVersion(string viewName, NancyContext context);
        void UpdateCachedView(IDictionary<string, ViewLocationResult> replacements);
    }
  1. Создайте новый ViewLocationResultProvider
public class ConcurrentNewViewLocationResultProvider : INewViewLocationResultProvider
    {
        private Dictionary<string, ViewLocationResult> _cachedViewLocationResults;
        private readonly object _cacheLock = new object();
        public bool UseCachedView { get; set; }

        public ConcurrentNewViewLocationResultProvider()
        {
            lock (_cacheLock)
            {
                if(_cachedViewLocationResults == null)_cachedViewLocationResults = new Dictionary<string, ViewLocationResult>();
            }
        }

        public ViewLocationResult GetNewerVersion(string viewName, NancyContext context)
        {
            if (UseCachedView)
            {
                if (Monitor.TryEnter(_cacheLock, TimeSpan.FromMilliseconds(20)))
                {
                    try
                    {
                        if (_cachedViewLocationResults != null && _cachedViewLocationResults.ContainsKey(viewName))
                        {
                            return _cachedViewLocationResults[viewName];
                        }
                    }
                    finally
                    {
                        Monitor.Exit(_cacheLock);
                    }
                }
            }

            return null;
        }

        public void UpdateCachedView(IDictionary<string, ViewLocationResult> replacements)
        {
            lock (_cacheLock)
            {
                if(_cachedViewLocationResults == null)_cachedViewLocationResults = new Dictionary<string, ViewLocationResult>();
                foreach (var replace in replacements)
                {
                    if (_cachedViewLocationResults.ContainsKey(replace.Key))
                    {
                        _cachedViewLocationResults[replace.Key] = replace.Value;
                    }
                    else
                    {
                        _cachedViewLocationResults.Add(replace.Key,replace.Value);
                    }                   
                }
            }
        }
    }
  1. В вашем загрузчике зарегистрируйте новый ViewLocationResultProvider с tinyIoc или аналогичным ему [119 ]
container.Register<INewViewLocationResultProvider, ConcurrentNewViewLocationResultProvider>().AsSingleton();
  1. Создайте производный класс из ViewLocationResult
    public class OneTimeUsedViewLocationResult : ViewLocationResult
    {
        private bool _used = false;
        public OneTimeUsedViewLocationResult(string location, string name, string extension, Func<TextReader> contents)
            : base(location, name, extension, contents)
        {
        }

        public override bool IsStale()
        {
            if (_used) return false;
            _used = true;
            return true;
        }
    }
  1. И нового IViewLocator:
public class CachedViewLocator : IViewLocator
    {
        private readonly INewViewLocationResultProvider _newVersion;
        private readonly DefaultViewLocator _fallbackViewLocator;
        public CachedViewLocator(IViewLocationProvider viewLocationProvider, IEnumerable<IViewEngine> viewEngines, INewViewLocationResultProvider newVersion)
        {
            _fallbackViewLocator = new DefaultViewLocator(viewLocationProvider, viewEngines);
            _newVersion = newVersion;
        }

        public ViewLocationResult LocateView(string viewName, NancyContext context)
        {
            if (_newVersion.UseCachedView)
            {
                var result = _newVersion.GetNewerVersion(viewName, context);
                if (result != null) return result;
            }
            return _fallbackViewLocator.LocateView(viewName, context);
        }

        public IEnumerable<ViewLocationResult> GetAllCurrentlyDiscoveredViews()
        {
            return _fallbackViewLocator.GetAllCurrentlyDiscoveredViews();
        }

    }
}
[1119 ]
  • Расскажите Нэнси о новом ViewLocator
    1.         protected override NancyInternalConfiguration InternalConfiguration
              {
                  get
                  {
                      return NancyInternalConfiguration.WithOverrides
                      (
                          nic =>
                          {
                              nic.ViewLocationProvider = typeof(ResourceViewLocationProvider);//use this or your equivalent
                              nic.ViewLocator = typeof(CachedViewLocator);
                          }
                      );
                  }            
              }
      
      1. Затем вы можете обновить его через API, например:
      public class YourModule : NancyModule
      {
          public YourModule(INewViewLocationResultProvider provider)
          {
             Get["/yourupdateinterface"] = param =>
             {
                if(!provider.UseCachedView) return HttpStatusCode.BadRequest;//in case you turn off the hot update
                //you can serialize your OneTimeUsedViewLocationResult with Newtonsoft.Json and store those views in any database, like mysql, redis, and load them here
                //data mock up
                TextReader tr = new StringReader(Resources.TextMain);                
                var vlr = new OneTimeUsedViewLocationResult("","index","cshtml",()=>tr);
                var dir = new Dictionary<string, ViewLocationResult> {{"index",vlr}};
                //mock up ends
                provider.UpdateCachedView(dir);
                return HttpStatusCode.OK;
             }
          }
      
      }
      

      Примечание: Приведенный выше код не решает вручную сопоставить все элементы Location, Name, Extension для элементов ViewLocationResult в моем вопросе, но, поскольку я в конечном итоге создаю редактор представлений для моих колледжей, чтобы загружать их представления, мне не нужно его решать больше.

    0
    ответ дан Xiao Wii 16 January 2019 в 08:12
    поделиться
    Другие вопросы по тегам:

    Похожие вопросы: