This is the first part of a four-part blog entry about enabling users to personalize a datagrid view by hiding, showing columns and setting columnwidths and order of columns.
This first part describes the assembly that enables saving and retrieving settings set for a specific users.
PersonalSettings
The personalsettings enables the program to save information for a certain user. The information is uniquely identified by an ApplicationName, SectionName and Setting.
How the PersonalSettings saves the information is dependant on the specific implementation of the IPersonalSettings interface.
The following sourcefiles make up the project :
· IPersonalSettings.cs
· PersonalSettings.cs
· PersonalSettingsFactory.cs
· PersonalSettingsWinXml.cs
IPersonalSettings.cs
IPersonalSettings is the interface that should be implemented by every specific PersonalSettings implementation (see PersonalSettingsWinXml.cs).
The interface defines the following members:
string ApplicationName
Defines the current application the SavePersonalApplicationSetting, GetPersonalApplicationSetting and SectionNameExist methods apply to.
string SectionName
Defines the current section the SavePersonalApplicationSetting, GetPersonalApplicationSetting methods apply to.
bool SectionNameExist(string sectionName);
Checks if a section exists for the current application.
void SavePersonalApplicationSetting(string setting,string value);
Save the value of a setting in the current application/section.
The method should take care of the creation of :
the repositary for the application if not created yet.
the section within the application if not created yet.
the setting within the section if not created yet.
string GetPersonalApplicationSetting(string setting);
Gets the value of a setting in the current application/section.
The method returns null if the setting and/or section and/or application is not defined.
As you can see there is no property to set a username. This is because (in my opinion) the class implementing the IPersonalSettings interface, should internally determin the current user. Offcourse this policy could be adjusted if the policy can’t be hold true for certain reasons.
PersonalSettings.cs
Is a base class that can be inherited in other assemblies to define easy access to settings.
The class defines all the same methods as the IPersonalSettings interface and accesses these methods via an IPersonalSettings implementation. The specific IPersonalSettings interface is accessed via a factory class (see PersonalSettingsFactory.cs).
Example of class that inherits PersonalSettings
public class PersonalSettings : Shorty.Configuration.PersonalSettings
{
public PersonalSettings():base()
{
}
public static void SetTopLocationForm(string sectionName,int top)
{
ApplicationName = “MyApplication”;
SectionName = sectionName;
SavePersonalApplicationSetting("Top",top.ToString());
}
public static int GetTopLocationForm(string sectionName)
{
SectionName = sectionName;
ApplicationName = “MyApplication”;
return Convert.ToInt32(GetPersonalApplicationSetting("Top"));
}
}
PersonalSettingsFactory.cs
This class has one static property PersonalSettings which returns a class implementing the IPersonalSettings interface.
The class is created based on params set in the app.config file.
The class makes use of AssemblySettings class to retrieve settings of the app.config file.
PersonalSettingsWinXml.cs
This class implements the IPersonalSettings interface saving the information in a Xml-file and under the Application Data Folder in the users profile.
I won’t explain all the different methods here because everything is well commented in code.
In short the class uses the System.Security.Principal.WindowsIdentity.GetCurrent() to determine the current user.
The class also makes use of the AssemblySettings class for defining/creating sections.
On-The-Side : AssemblySettings.cs
AssemblySettings is a base-class for retrieving settings set in the app.config file.
As an example of using the assembly settings class:
public class MyAssemblySettings: AssemblySettings
{
public static string ApplicationName
{
get
{
SectionName = "MySection";
return GetAssemblySetting("ApplicationName");
}
}
}
The app.config file looks like this:
<configuration>
<configSections>
<section name="MySection"
type="System.Configuration.NameValueSectionHandler, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
<MySection>
<add key="ApplicationName" value="MyApp"></add>
</MySection> </configuration>
You can also work with sectiongroups. Just set the SectionName in the getter to [SectionGroup]/[SectionName]. e.g. ’’MySectionGroup/MySection’’
This zipfile contains the sourcefiles for the Shorty.Configuration assembly.
This zipfile contains an example of using the PersonalSettings and the AssemblySettings classes.
update : There seems to be a new and easier way in Visual Basic 2005 to store Settings see the MSDN Article
6:23:13 PM
|