Created
April 20, 2019 16:27
-
-
Save morales-franco/cb687c79fe154688210fccfacd467973 to your computer and use it in GitHub Desktop.
customSectionInAppConfigs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Configuration; | |
namespace JungleApp.Batch.Infraestructure | |
{ | |
//Configuration of AppCountriesSection in the app.config | |
class ConfigurationAppCountriesSection : ConfigurationSection | |
{ | |
private const string _CountriesCollectionName = "Countries"; | |
[ConfigurationProperty(_CountriesCollectionName)] | |
[ConfigurationCollection(typeof(CountriesCollection), AddItemName = "Country")] //Element name of Collection | |
public CountriesCollection Countries { | |
get | |
{ | |
return (CountriesCollection)base[_CountriesCollectionName]; | |
} | |
} | |
} | |
//This class teach to the framework to read the Countries Collection | |
public class CountriesCollection : ConfigurationElementCollection | |
{ | |
protected override ConfigurationElement CreateNewElement() | |
{ | |
return new CountryConfigurationElement(); | |
} | |
protected override object GetElementKey(ConfigurationElement element) | |
{ | |
return ((CountryConfigurationElement)element).Name; | |
} | |
} | |
//This class teach to the framework to read the Country element | |
public class CountryConfigurationElement : ConfigurationElement | |
{ | |
[ConfigurationProperty("name", IsRequired = true)] | |
public string Name | |
{ | |
get { return (string)this["name"]; } | |
set { this["name"] = value; } | |
} | |
[ConfigurationProperty("continent", IsRequired = true)] | |
public string Continent | |
{ | |
get { return (string)this["continent"]; } | |
set { this["continent"] = value; } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Country> GetCountriesFromAppContriesSection() | |
{ | |
var countries = new List<Country>(); | |
var appCountriesSection = ConfigurationManager.GetSection("AppCountriesSection") as ConfigurationAppCountriesSection; | |
foreach (CountryConfigurationElement countryElement in appCountriesSection.Countries ) | |
countries.Add(new Country(countryElement.Name, countryElement.Continent)); | |
return countries; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<configuration> | |
<configSections> | |
<section name="AppCountries" type="System.Configuration.NameValueSectionHandler" /> | |
<section name="AppCountriesSection" type="JungleApp.Batch.Infraestructure.ConfigurationAppCountriesSection, JungleApp.Batch" /> | |
</configSections> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | |
</startup> | |
<AppCountriesSection> | |
<Countries> | |
<Country name = "Argentina" continent="America" /> | |
<Country name = "New Zealand" continent="Oceania" /> | |
<Country name = "Spain" continent="Europe" /> | |
<Country name = "China" continent="Asia" /> | |
<Country name = "South Africa" continent="Africa" /> | |
</Countries> | |
</AppCountriesSection> | |
<AppCountries> | |
<add key="Argentina" value="America"/> | |
<add key="New Zealand" value="Oceania"/> | |
<add key="Spain" value="Europe"/> | |
<add key="China" value="Asia"/> | |
<add key="South Africa" value="Africa"/> | |
</AppCountries> | |
<appSettings> | |
<add key="Argentina" value="America"/> | |
<add key="New Zealand" value="Oceania"/> | |
<add key="Spain" value="Europe"/> | |
<add key="China" value="Asia"/> | |
<add key="South Africa" value="Africa"/> | |
</appSettings> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var argentinaValue = ConfigurationManager.AppSettings["Argentina"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Country> GetCountriesFromAppSettings() | |
{ | |
return new List<Country>() | |
{ | |
new Country("Argentina", ConfigurationManager.AppSettings["Argentina"]), | |
new Country("New Zealand", ConfigurationManager.AppSettings["New Zealand"]), | |
new Country("Spain", ConfigurationManager.AppSettings["Spain"]), | |
new Country("Africa", ConfigurationManager.AppSettings["Africa"]), | |
new Country("South Africa", ConfigurationManager.AppSettings["South Africa"]), | |
}; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configSections> | |
<section name="AppCountries" type="System.Configuration.NameValueSectionHandler" /> | |
</configSections> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<AppCountries> | |
<add key="Argentina" value="America"/> | |
<add key="New Zealand" value="Oceania"/> | |
<add key="Spain" value="Europe"/> | |
<add key="China" value="Asia"/> | |
<add key="South Africa" value="Africa"/> | |
</AppCountries> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public List<Country> GetCountriesFromKeyValuePairCustomSection() | |
{ | |
var countries = new List<Country>(); | |
var appCountriesCollection = ConfigurationManager.GetSection("AppCountries") as NameValueCollection; | |
foreach (var countryKey in appCountriesCollection.AllKeys) | |
countries.Add(new Country(countryKey, appCountriesCollection[countryKey])); | |
return countries; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<AppCountriesSection> | |
<Countries> | |
<Country name = "Argentina" continent="America" /> | |
<Country name = "New Zealand" continent="Oceania" /> | |
<Country name = "Spain" continent="Europe" /> | |
<Country name = "China" continent="Asia" /> | |
<Country name = "South Africa" continent="Africa" /> | |
</Countries> | |
</AppCountriesSection> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<configSections> | |
<section name="AppCountriesSection" type="JungleApp.Batch.Infraestructure.ConfigurationAppCountriesSection, JungleApp.Batch" /> | |
</configSections> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<AppCountriesSection> | |
<Countries> | |
<Country name = "Argentina" continent="America" /> | |
<Country name = "New Zealand" continent="Oceania" /> | |
<Country name = "Spain" continent="Europe" /> | |
<Country name = "China" continent="Asia" /> | |
<Country name = "South Africa" continent="Africa" /> | |
</Countries> | |
</AppCountriesSection> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<appSettings> | |
<add key="Argentina" value="America"/> | |
<add key="New Zealand" value="Oceania"/> | |
<add key="Spain" value="Europe"/> | |
<add key="China" value="Asia"/> | |
<add key="South Africa" value="Africa"/> | |
</appSettings> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment