Skip to content

5. Custom Section format

Emrys edited this page Jan 25, 2018 · 1 revision

1. original configuration file Section there may not be necessary operation

In the original configuration file, we need custom configuration files, so we need to configure section in the configuration file, such as our common EntityFramework.

If we define the location of the configuration file, that is to say, without the original configuration file, then we don't need to build a new Section** in configSections at the same time according to the previous configuration method, and then configure it.

2. solutions, we need a configuration file

<?xml version="1.0" encoding="utf-8" ?> 
<config>
  <userInfo userName="CEmrys" email="i@emrys.me" age="17">
    <blogUrl>http://www.cnblogs.com/emrys5/</blogUrl>
    <favoriteColor>Blue</favoriteColor>
    <dislikeColor>2</dislikeColor>
  </userInfo>
  <arrayString>
    <item>a</item>
    <item>b</item>
    <item>c</item>
    <item>d</item>
    <item>e</item>
  </arrayString>
</config>

If the file is located in ** /Cfg/SetSection.config**, in the custom Section, the file path must be specified, because the original configuration file does not support custom Section format.

3. Setting

var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cfg", "SetSection.config");

SuperConfig<UserInfo>.Setting(configFilePath, (sectionName, filePath, convertCaseStrategy) =>
{
    XElement xElement = XElement.Load(filePath);
    var caseSectionName = convertCaseStrategy.ConvertCase(sectionName);
    var sectionElement = xElement.Elements().Where(i => i.Name == caseSectionName).FirstOrDefault();
    if (sectionElement == null)
    {
        throw new Exception("dot find section:" + sectionName);
    }

    return new Section() { XElement = sectionElement };
});

Analysis code

void Setting(string filePath, Func<string, string, IConvertCaseStrategy, Section> funcSection)

The first parameter of SuperConfig's Setting method needs a filePath. The second parameter is a * * two String and IConvertCaseStrategy as a parameter, and the return value is Section Commissioned.

the first parameter is sectionName, the second parameter is filePath, the third parameter is IConvertCaseStrategy (i.e., naming rules, name calling method of ConvertCase gets converted), then returns a Section value, that is to say as long as the properties of Section XElement can be assigned.

4. a Section configuration file

We may also encounter a configuration file that corresponds to a physical object. There are no multiple Section. A Section is also a configuration file, for example, someone likes to divide different configurations into different configuration files.

4.1 Config

<?xml version="1.0" encoding="utf-8" ?>
<userInfo userName="SUEmrys" email="i@emrys.me" age="17">
  <blogUrl>http://www.cnblogs.com/emrys5/</blogUrl>
  <favoriteColor>Blue</favoriteColor>
  <dislikeColor>2</dislikeColor>
</userInfo> 

4.2 Setting

var configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Cfg", "SectionUserInfo.config");

SuperConfig<UserInfo>.Setting(configFilePath, (sectionName, filePath, convertCaseStrategy) =>
{
     return new Section() { XElement = XElement.Load(filePath) };
});

4.3 Get value

var user = SuperConfig<UserInfo>.Value;
Assert.AreEqual(user.UserName, "SUEmrys");