3
\$\begingroup\$

I am trying to design XML with configuration info so I can parse it into this code:

...
services.Configure<IdentityOptions>(options =>
{
    // Password settings
    options.Password.RequireDigit = true;
    options.Password.RequiredLength = 8;
    options.Password.RequireNonAlphanumeric = false;
    options.Password.RequireUppercase = true;
    options.Password.RequireLowercase = false;
    options.Password.RequiredUniqueChars = 6;
});
...

I have already 4 versions of the XML code, but I don't know which one to choose:

<identity>
    <!-- version 1 -->
    <password>
        <RequireDigit>true</RequireDigit>
        <RequiredLength>8</RequiredLength>
        <RequireNonAlphanumeric>false</RequireNonAlphanumeric>
        <RequireUppercase>true</RequireUppercase>
        <RequireLowercase>false</RequireLowercase>
        <RequiredUniqueChars>6</RequiredUniqueChars>
    </password>
    <!-- version 2 -->
    <password   RequireDigit="true" 
                RequiredLength="8" 
                RequireNonAlphanumeric="false" 
                RequireUppercase="true" 
                RequireLowercase="false" 
                RequiredUniqueChars="6"
    />
    <!-- version 3 -->
    <password>
        <RequireDigit value="true"/>
        <RequiredLength value="8"/>
        <RequireNonAlphanumeric value="false"/>
        <RequireUppercase value="true"/>
        <RequireLowercase value="false"/>
        <RequiredUniqueChars value="6"/>
    </password>
    <!-- version 4 -->
    <password>
        <RequireDigit/>
        <RequiredLength value="8"/>
        <RequireUppercase/>
        <RequiredUniqueChars value="6"/>
    </password>
</identity>
  • Are there any rules when to choose which one?
  • Or how to decide which is the best or the most suitable?
\$\endgroup\$
5
  • 2
    \$\begingroup\$ Clarification needed. The code snippet looks like .net-core startup code. This would lead me to ask why XML and not JSON? Next I am uncertain what you want reviews here? \$\endgroup\$
    – Nkosi
    Mar 21, 2018 at 10:21
  • \$\begingroup\$ Yes, it is .net-core startup file. I want to use XML because except of information for the startup file, the XML file contains also other data (I provided just part of the whole file). So I want to know other's opinion on which version of XML data format is the best / is used. \$\endgroup\$
    – sapoi
    Mar 21, 2018 at 10:32
  • 3
    \$\begingroup\$ You don't know this yet but you want JSON. \$\endgroup\$
    – t3chb0t
    Mar 21, 2018 at 10:49
  • 2
    \$\begingroup\$ And why would I want JSON? \$\endgroup\$
    – sapoi
    Mar 21, 2018 at 10:53
  • 2
    \$\begingroup\$ It's easier to read, easier to maintain, lighter, it's natively supported by asp.net-core, and there is only one way for storing your configuration so your question would answer itself with json. \$\endgroup\$
    – t3chb0t
    Mar 21, 2018 at 12:01

1 Answer 1

7
\$\begingroup\$

You are trying to recreate functionality that is already provided out of the box.

Reference Configure an ASP.NET Core App

From documentation it supports providers for INI, JSON, and XML.

Each configuration value maps to a string key. There's built-in binding support to deserialize settings into a custom POCO object (a simple .NET class with properties).

Reference ASP.NET Core 1.0 Configuration Deep Dive

For XML, you need to add this NuGet package: Microsoft.Extensions.Configuration.Xml and the format that provider uses would look like this for your settings

Note: It matches closely to version 1 in your example

<configuration>
    <identityOptions>
        <password>
            <RequireDigit>true</RequireDigit>
            <RequiredLength>8</RequiredLength>
            <RequireNonAlphanumeric>false</RequireNonAlphanumeric>
            <RequireUppercase>true</RequireUppercase>
            <RequireLowercase>false</RequireLowercase>
            <RequiredUniqueChars>6</RequiredUniqueChars>
        </password>
    </identity>
</configuration>

And the following would be added as part of configuration

//...
.AddXmlFile("appsettings.xml", optional: true, reloadOnChange: true)
//...

If using JSON then

{  
  "IdentityOptions": {
    "Password": {
      "RequireDigit": true
      "RequiredLength": 8
      "RequireNonAlphanumeric": false
      "RequireUppercase": true
      "RequireLowercase": false
      "RequiredUniqueChars": 6
    }
  }
}

And the following would be added as part of configuration

//...
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
//...

You appear to already be setting up IOptions, which is good.

//...
// Register the ConfigurationBuilder instance which IdentityOptions binds against.
services.Configure<IdentityOptions>(Configuration);

//...

This will allow constructor dependency injection with IOptions<TOptions> to access settings

private readonly IdentityOptions options;

public MyClass(IOptions<IdentityOptions> identity) {
    options = identity.Value;
}
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.