App.Config or XAML

I am currently evaluating my options for rewriting the projects I'm working on, and I'm a little annoyed by the strict nature of our app.config files.

I would like to move on to a more structured approach, so I have two options:

  • Use custom SectionHandlers in app.config
  • Change app.config and use XAML instead.

I would like to get your opinion and horrors on this, what are the pros and cons of using XAML for this?

Cheers, Florian

+7
xaml configuration
source share
3 answers

If we are talking about configuration files, would I use app.config? What for? Why is this important? If we are talking about resources (images, messages), I would put them in the XAML resource directory.

There is a guide on the Internet where you can post something, but it was still in the draft the last time I checked and did not mention app.config afaik.

But do what works best for you :)

+3
source share

In terms of development efforts, 6 out of half a dozen others.

If you use Xaml, you will need to create a set of class instances from which Xaml will be created.

If you use Custom SectionHandlers, you will still need to instantiate the classes that will be represented by these sections. You also need to create SectionHandlers.

1 - 0 to Xaml.

In the case of Xaml, although you will need to provide your own infrastructure for loading xaml at startup and accessing the configuration throughout the application.

Using partition handlers, on the other hand, the existing .NET ConfigurationManager provides the infrastructure for accessing them.

1 all

+1
source share

If you are working with .NET 4, why not combine your two options and put XAML inside App.config ?

 using System.Configuration; using System.Xaml; using System.Xml; public class XamlConfigurationSection : IConfigurationSectionHandler { public object Create(object parent, object configContext, XmlNode section) { return XamlServices.Parse(section.OuterXml); } } 

This custom configuration section allows you to include any object described as XAML in App.config :

 <configSections> <section name="SomeType" type="XamlConfigurationSection, …" /> </configSections> <SomeType xmlns="clr-namespace:SomeNamespace;assembly=…" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"></SomeType> 

provided that you have a type:

 namespace SomeNamespace { public class SomeType { public SomeType() { … } // XAML requires a parameterless constructor … } } 

and finally get an instance of this type from App.config with:

 var objectOfSomeType = ConfigurationManager.GetSection("SomeType") as SomeType; 
+1
source share

All Articles