• Share
    • Twitter
    • LinkedIn
    • Facebook
    • Email
  • Feedback
  • Improve this Doc

Class SettingsRepository<T>

The SettingsRepository is used to persist settings to one of DNNs internal settings stores

Inheritance
System.Object
SettingsRepository<T>
Implements
ISettingsRepository<T>
Namespace: DotNetNuke.Entities.Modules.Settings
Assembly: DotNetNuke.dll
Syntax
public abstract class SettingsRepository<T> : object, ISettingsRepository<T> where T : class, new()
Type Parameters
Name Description
T
Remarks

The SettingsRepository allows you to write concise classes for persisting settings to either ModuleSettings, TabModuleSettings or PortalSettings. All you need to do is to implement a static retrieval method (GetSettings in the sample below) and a storage method (SaveSettings below) on a Repository that is typed to your class (MyModuleSettingsRepository below). You can then label each settings you need by providing the right attribute to each property.

Setting up the settings class

using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Settings;

public class MyModuleSettings
{
    [ModuleSetting]
    public int Foo { get; set; } = -1;
    [ModuleSetting]
    public string Bar { get; set; } = "My default string";
    [TabModuleSetting]
    public bool Baz { get; set; } = true;

    public static MyModuleSettings GetSettings(ModuleInfo module)
    {
        var repo = new MyModuleSettingsRepository();
        return repo.GetSettings(module);
    }

    public void SaveSettings(ModuleInfo module)
    {
        var repo = new MyModuleSettingsRepository();
        repo.SaveSettings(module, this);
    }
}

public class MyModuleSettingsRepository : SettingsRepository<MyModuleSettings>
{
}

Consuming the settings in your module

Once you have the settings class you can use this anywhere in your code. The most powerful way is to add this to a base class underlying your front end code. If working in an MVC type scenario you could create a class that inherits from DnnController (e.g. MyModuleController) that your controllers inherit from:

public class HomeController : MyModuleController

Then, in MyModuleController you could add the following snippet:

private MyModuleSettings _settings;
public MyModuleSettings Settings
{
    get { return _settings ?? (_settings = MyModuleSettings.GetSettings(ModuleContext.Configuration)); }
}

Now in any of your controllers you can access these settings using Settings.Foo and Settings.Bar, etc. You can use a similar approach for the WebPage/WebPage to get the same syntax in your razor files. This will declutter your front end code. A more extensive example can be found in this module which implements this:

https://github.com/DNN-Connect/Conference

Constructors

| Improve this Doc View Source

SettingsRepository()

Initializes a new instance of the SettingsRepository<T> class.

Declaration
protected SettingsRepository()

Properties

| Improve this Doc View Source

MappingCacheKey

Gets cache key for this class. Used for parameter mapping storage as well as entire class persistence.

Declaration
protected virtual string MappingCacheKey { get; }
Property Value
Type Description
System.String

Methods

| Improve this Doc View Source

CreateMapping()

Rebuilds parameter mapping of the class.

Declaration
protected virtual IList<ParameterMapping> CreateMapping()
Returns
Type Description
IList<ParameterMapping>

List of parameters.

| Improve this Doc View Source

GetSettings(ModuleInfo)

Retrieve module settings. This can optionally include TabModule settings, Portal settings and host settings as well. Note the result is cached.

Declaration
public T GetSettings(ModuleInfo moduleContext)
Parameters
Type Name Description
ModuleInfo moduleContext

Your module's context.

Returns
Type Description
T

Your settings class.

| Improve this Doc View Source

GetSettings(Int32)

Retrieve portal/host settings. This will ignore Module and TabModule settings. Note the result is cached.

Declaration
public T GetSettings(int portalId)
Parameters
Type Name Description
System.Int32 portalId

The portal ID for which to retrieve the settings.

Returns
Type Description
T

Your settings class.

| Improve this Doc View Source

LoadMapping()

Retrieves the parameter mapping from cache if still there, otherwise recreates it.

Declaration
protected IList<ParameterMapping> LoadMapping()
Returns
Type Description
IList<ParameterMapping>

List of parameters.

| Improve this Doc View Source

SaveSettings(ModuleInfo, T)

Save your module's settings. This can optionally include TabModule settings, Portal settings and host settings as well.

Declaration
public void SaveSettings(ModuleInfo moduleContext, T settings)
Parameters
Type Name Description
ModuleInfo moduleContext

Your module's context.

T settings

Your settings class.

| Improve this Doc View Source

SaveSettings(Int32, T)

Save your settings. Module and TabModule settings will be ignored. Only Portal settings and host settings will be saved.

Declaration
public void SaveSettings(int portalId, T settings)
Parameters
Type Name Description
System.Int32 portalId

Your portal Id for these settings.

T settings

Your settings class.

Implements

ISettingsRepository<T>

Extension Methods

JsonExtensionsWeb.ToJson(Object)
  • View Source
Back to top by the community, for the community... #DNNCMS