Sitecore allows you to create configuration sections in /configuration/sitecore/ that consists of hierarchies of configurations including lists, and it will even help you serialize the configuration to object instances.
Observe the following real life example of a configuration I created to configure lists of IP adresses and IP ranges to ignore in my web application:
<pt_restrictions> <restriction type="Restrictions.Model.Configuration, Restrictions"> <ipraddresses hint="raw:AddIpAddresses" > <address ip="127.0.0.1" /> <address ip="127.0.0.2" /> </ipraddresses> <ipranges hint="raw:AddIpRanges"> <range from="192.168.0.1" to="192.168.0.255"/> <range from="192.169.0.1" to="192.169.0.255"/> </ipranges> </restriction> </pt_restrictions>
The configuration section is added to a Sitecore .config file under /configuration/sitecore/.
Please notice the “type” property “Restrictions.Model.Configuration, Restrictions” and the 2 “hint” properties. These allows Sitecore to perform some reflection magic that will map the configuration section to classes.
So let’s code. First I will create an interface that outlines my configuration, a list of Ip Addresses and a list of Ip Ranges
namespace Restrictions.Model { public interface IConfiguration { IEnumerable<IpAddress> IpAddresses { get; } IEnumerable<IpRange> IpRanges { get; } } }
I need implementations of IpAddress and IpRange. The implementations contains the XML attributes as properties:
namespace Restrictions.Model { public class IpAddress { public string Ip { get; set; } } public class IpRange { public string From { get; set; } public string To { get; set; } } }
For the final touch, I will assemble it all in a Configuration class. And here is where the magic lies. Remember the “hint=”raw:AddIpAddresses” and “hint=”raw.AddIpRanges”” attributes? These are method names in my class. Sitecore calls these for each Xml node:
using System.Collections.Generic; using System.Xml; namespace Restrictions.Model { public class Configuration : IConfiguration { private readonly List<IpAddress> _ipAddresses = new List<IpAddress>(); public IEnumerable<IpAddress> IpAddresses { get { return _ipAddresses; } } private readonly List<IpRange> _ipRanges = new List<IpRange>(); public IEnumerable<IpRange> IpRanges { get { return _ipRanges; } } protected void AddIpAddresses(XmlNode node) { if (node == null) return; if (node.Attributes == null) return; if (node.Attributes["ip"] == null) return; if (node.Attributes["ip"].Value == null) return; _ipAddresses.Add(new IpAddress() { Ip = node.Attributes["ip"].Value }); } protected void AddIpRanges(XmlNode node) { if (node == null) return; if (node.Attributes == null) return; if (node.Attributes["from"] == null) return; if (node.Attributes["from"].Value == null) return; if (node.Attributes["to"] == null) return; if (node.Attributes["to"].Value == null) return; _ipRanges.Add(new IpRange() { From = node.Attributes["from"].Value, To = node.Attributes["to"].Value }); } } }
The final class is the repository that will use reflection magic to convert an XmlNode to a class implementation:
using System.Xml; namespace Restrictions.Model.Repositories { public class ConfigurationRepository { public IConfiguration GetConfiguration(string restrictionName) { XmlNode xmlNode = Sitecore.Configuration.Factory.GetConfigNode("pt_restrictions/restriction"); return Sitecore.Configuration.Factory.CreateObject<IConfiguration>(xmlNode); } } }
I use the class like this:
ConfigurationRepository rep = new ConfigurationRepository(); IConfiguration config = rep.GetConfiguration(); foreach (var ipaddress in config.IpAddresses) // do stuff; foreach (var iprange in config.IpRanges) // do stuff
It looks like a big job (and it probably is) but once you get it running, it is easy to extend and use elsewhere.
MORE TO READ:
- Sitecore web.config include files (auto-include) by Briancaos
- THE SITECORE ASP.NET CMS CONFIGURATION FACTORY by John West
- Using Sitecore Configuration Factory For Dependency Injection by Jeffrey Rondeau
- Un-tangling Sitecore configuration includes by Alan Coates
- Structured, Type Safe Settings in Sitecore 9 by Alan Coates
Pingback: Sitecore Pipelines – The great walkthrough | Brian Pedersen's Sitecore and .NET Blog
Pingback: Which of my old Sitecore posts are still valid in Sitecore 9? | Brian Pedersen's Sitecore and .NET Blog
Pingback: Inject and resolve custom configurations to your Sitecore Helix solution – Visions In Code