Class SettingsRepository<T>
The SettingsRepository is used to persist settings to one of DNNs internal settings stores
Implements
Inherited Members
Namespace: DotNetNuke.Entities.Modules.Settings
Assembly: DotNetNuke.dll
Syntax
public abstract class SettingsRepository<T> : 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
Constructors
SettingsRepository()
Declaration
protected SettingsRepository()
Properties
MappingCacheKey
Declaration
protected virtual string MappingCacheKey { get; }
Property Value
Type | Description |
---|---|
string |
Methods
CreateMapping()
Declaration
protected virtual IList<ParameterMapping> CreateMapping()
Returns
Type | Description |
---|---|
IList<ParameterMapping> |
GetSettings(ModuleInfo)
Declaration
public T GetSettings(ModuleInfo moduleContext)
Parameters
Type | Name | Description |
---|---|---|
ModuleInfo | moduleContext |
Returns
Type | Description |
---|---|
T |
GetSettings(int)
Declaration
public T GetSettings(int portalId)
Parameters
Type | Name | Description |
---|---|---|
int | portalId |
Returns
Type | Description |
---|---|
T |
LoadMapping()
Declaration
protected IList<ParameterMapping> LoadMapping()
Returns
Type | Description |
---|---|
IList<ParameterMapping> |
SaveSettings(ModuleInfo, T)
Declaration
public void SaveSettings(ModuleInfo moduleContext, T settings)
Parameters
Type | Name | Description |
---|---|---|
ModuleInfo | moduleContext | |
T | settings |
SaveSettings(int, T)
Declaration
public void SaveSettings(int portalId, T settings)
Parameters
Type | Name | Description |
---|---|---|
int | portalId | |
T | settings |