Example #1
0
        private void EnsureSettings()
        {
            if (_settings == null)
            {
                // Guard for null SiteCache
                if (PluginCache == null)
                {
                    throw new PluginException(null, "The PluginCache property is null for {0} when it should be injected by the DI container. " +
                                              "If you're unit testing, set the PluginCache and Repository properties with stubs before calling the Settings properties.", GetType().FullName);
                }

                _settings = PluginCache.GetPluginSettings(this);
                if (_settings == null)
                {
                    // Guard for null Repository
                    if (Repository == null)
                    {
                        throw new PluginException(null, "The Repository property is null for {0} and it wasn't found in the cache - it should be injected by the DI container. " +
                                                  "If you're unit testing, set the PluginCache and Repository properties with stubs before calling the Settings properties.", GetType().FullName);
                    }

                    // Load from the database
                    _settings = Repository.GetTextPluginSettings(this.DatabaseId);

                    // If this is the first time the plugin has been used, new up the settings
                    if (_settings == null)
                    {
                        EnsureIdIsValid();
                        string version = EnsureValidVersion();
                        _settings = new Settings(Id, version);

                        // Allow derived classes to add custom setting values
                        OnInitializeSettings(_settings);

                        // Update the repository
                        Repository.SaveTextPluginSettings(this);
                    }

                    // Cache the settings
                    PluginCache.UpdatePluginSettings(this);
                }
            }
        }