Adding a settings section for a module
In this article, we explain how to add your own settings section for a module, and to manage the connection to an MSSQL Server database with the following settings: User Name, Password, DataSource, InitialCatalog.
To implement a settings section you have to inherit from the following classes:
- ELMA.Runtime.Settings.GlobalSettingsBase– base class for global settings, it stores settings in an object
- ELMA.Runtime.Settings.GlobalSettingsModuleBase<TSettings>- this class defines the name of the module, its section in the system, and the methods for loading/saving settings.
- EleWise.ELMA.Web.Mvc.Models.Settings.GlobalSettingsModuleControllerBase<TSettings, TSettingsModule> - this class is needed to define the position of the module’s settings, and to render view/edit forms.
Example of Data Display
Fig. 1. Settings section of a module
For a simple block of settings, you have to implement three classes inherited from GlobalSettingsBase, GlobalSettingsModuleBase and GlobalSettingsModuleControllerBase. Below you can see their examples. To see a more thorough example, read this article.
public class Settings : GlobalSettingsBase
{
/// <summary>
/// Determines that the password has not been changed
/// </summary>
public const string PASSWORD_NOT_CHANGED = "($PASSWORD_NOT_CHANGED$)";
public Settings()
{
DataSource = "(local)";
InitialCatalog = "BASEMESSAGES";
}
[DisplayName(typeof(@__Resources_MSSQLConnectionSettings), "DataSource")]
[Description(typeof(@__Resources_MSSQLConnectionSettings), "DataSourceDescription")]
[Required(true)] //Required field
public string DataSource { get; set; }
[DisplayName(typeof(@__Resources_MSSQLConnectionSettings), "InitialCatalog")]
[Description(typeof(@__Resources_MSSQLConnectionSettings), "InitialCatalogDescription")]
[Required(true)] // Required field
public string InitialCatalog { get; set; }
[DisplayName(typeof(@__Resources_MSSQLConnectionSettings), "UserId")]
[Description(typeof(@__Resources_MSSQLConnectionSettings), "UserIdDescription")]
[Required(true)] // Required field
public string UserId { get; set; }
}
/// <summary>
/// Resources
/// </summary>
internal class @__Resources_MSSQLConnectionSettings
{
public static string DataSource { get { return SR.T("DataSource"); } }
public static string DataSourceDescription { get { return SR.T("Connect to database"); } }
public static string InitialCatalog { get { return SR.T("InitialCatalog"); } }
public static string InitialCatalogDescription { get { return SR.T("Database name"); } }
public static string UserId { get { return SR.T("User name"); } }
public static string UserIdDescription { get { return SR.T("User name for connection to database"); } }
}
Example of a class inherited from GlobalSettingsModuleBase
[Component]
public class SettingsModule : GlobalSettingsModuleBase<Settings>
{
public static Guid UID = new Guid("{658A7D31-873B-4aa1-B183-54EE55DE0EAD}");
public override Guid ModuleGuid
{
get { return UID; }
}
/// <summary>
/// Module name
/// </summary>
public override string ModuleName
{
get { return SR.T("Settings for connecting to MSSQL database for messaging"); }
}
}
Example of a class inherited from GlobalSettingsModuleControllerBase
[Component(Order = 260)]
public class SettingsModuleController : GlobalSettingsModuleControllerBase<Settings, SettingsModule>
{
public SettingsModuleController(SettingsModule module)
: base(module)
{
}
}
To apply the settings in your module, load the settings: var settings = Locator.GetService<SettingsModule>();
Then you can use the settings value in your implementation.
To implement a more complex visualization of settings, read this article describing the implementation of custom settings with user display of view and edit form.