One thing that always surprised me very pleasantly of. NET framework since its first version, was the flexibility and extensibility that has. These qualities I believe, gradually increased version after version, and could say without committing an error that has become a philosophy. In this way, there is a small example that I now address the problem to be solved is to add a section customized to an application configuration file, either an App.config or Web.config, for this example I will assume that working with a GUI application that supports multi -language, for that I will define a new section called AppIdiomas in App.config and then in the same configuration file I define the languages \u200b\u200bsupported by the application. Let's look a bit like
define my configuration file:
define my configuration file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name ="AppIdiomas" type ="CR.Util.Lenguaje.IdiomaSection, CR.Util" />
</configSections>
<!-- Seccion de Idiomas-->
<AppIdiomas name ="AppIdiomas" >
<idiomas>
<add name ="en-US" Idioma ="English (United States)" />
<add name ="es-AR" Idioma ="English (Argentina)" />
</idiomas>
</AppIdiomas>
</configuration>
Now let's see how flexible is the mechanism that gives me. NET to extend the configuration file using the ConfigurationElement class, and ConfigurationElementCollection ConfigurationSection. First I will define a new configuration element in this particular case call idiomaConfigElement and that class inherits from ConfigurationElement
using System ;
using System . Collections . Generic ;
using System . Text ;
using System. Configuration ;
namespace CR. Useful . Language
{public class IdiomaConfigElement:
ConfigurationElement {public
IdiomaConfigElement ( String newName, String newIdioma )
{Name
= newName;
Language = newIdioma ;
} public
IdiomaConfigElement ()
{} public
IdiomaConfigElement ( string elementName) {
Name elementName = ;}
[ ConfigurationProperty ( "name" , DefaultValue = "English (Argentina)" , IsRequired = true , IsKey = true )]
public string Name
{
get
{
return ( string ) this [ "name" ];
}
set {
this [ "name" ] = value ;
}}
[ ConfigurationProperty ( "Language" , DefaultValue = "es-AR" , IsRequired = true)] public
Language string
{get
{
return (string ) this [ "Language" ];}
September
{
this [ "Language" ] = value;}
}}}
As I can have a list of languages \u200b\u200bsupported by the application and will be listed in the configuration file implement another class that inherits this time ConfigurationElementCollection:
System using ;
using System. Collections . Generic ;
using System. Text ;
using System. Configuration ;
namespace CR. Useful . Language
{
public class IdiomaCollection: ConfigurationElementCollection
{
public IdiomaCollection () {
IdiomaConfigElement idiom = ( IdiomaConfigElement ) CreateNewElement ();}
public override ConfigurationElementCollectionType CollectionType
{
get
{
return ConfigurationElementCollectionType . AddRemoveClearMap ;
}
}
protected override ConfigurationElement CreateNewElement ()
{
return new IdiomaConfigElement ();
}
protected override Object GetElementKey ( ConfigurationElement element )
{
return (( IdiomaConfigElement ) element ). Name ;
}
public IdiomaConfigElement this [ int index ]
{
get
{
return ( IdiomaConfigElement ) BaseGet ( index );
}
set
{
if ( BaseGet ( index ) != null )
{
BaseRemoveAt ( index );
}
BaseAdd ( index , value );
}
}
new public IdiomaConfigElement this [ string Name ] {
get {return
( IdiomaConfigElement ) BaseGet ( Name )
}}
public int IndexOf ( IdiomaConfigElement language )
{return BaseIndexOf ( language);
}}}
Finally I have left "join all" with a class that represents my section that inherits from ConfigurationSecction customized
using System ;
using System . Collections . Generic ;
using System . Text ;
using System. Configuration ;
namespace CR. Useful . Language
{public class IdiomaSection: ConfigurationSection
{
IdiomaConfigElement language; public
IdiomaSection () {
language = new IdiomaConfigElement ();}
[ ConfigurationProperty ( "name" , DefaultValue = "Languages" , IsRequired = true , IsKey = false)] public
string Name {
get {return
(string ) this [ "name" ];}
September
{
this [ "name" ] = value ;
}}
[ ConfigurationProperty ( "languages" , IsDefaultCollection = false)] public
IdiomaCollection languages
{get {
IdiomaCollection IdiomaCollection =
( IdiomaCollection ) base [ "languages" ]
return IdiomaCollection ;}
}}}
If we examine this mechanism that gives me the framework. NET to extend its functioning is the same it gives me when I define an exception in particular and I inherit my "new" exception from the Exception class. As we see in the code, the three classes that implement the definitions section, element, and the collection of these then take shape in the configuration file. Using the method attribute [ConfigurationProperty], I can define the attributes of the configuration item such as if required (IsRequired) among others.
gives me the flexibility to define attributes such languages \u200b\u200bsupported by the user interface from the application, from a xml configuration file, make my code much more extensible, and provide greater readability to be in an xml file.
This extensibility mechanism, although in this example was used for "multi-language" can be considered for any other "variable" of an application that can be configured from an xml configuration file, since the mechanism is what is important here.
gives me the flexibility to define attributes such languages \u200b\u200bsupported by the user interface from the application, from a xml configuration file, make my code much more extensible, and provide greater readability to be in an xml file.
This extensibility mechanism, although in this example was used for "multi-language" can be considered for any other "variable" of an application that can be configured from an xml configuration file, since the mechanism is what is important here.
0 comments:
Post a Comment