Go to the documentation of this file.00001 using System;
00002 using System.Configuration;
00003
00004 namespace N2.Configuration
00005 {
00009 public class ConfigurationManagerWrapper
00010 {
00011 readonly string sectionGroup;
00012
00013 public ConfigurationManagerWrapper()
00014 : this("n2")
00015 {
00016 }
00017 public ConfigurationManagerWrapper(string sectionGroup)
00018 {
00019 this.sectionGroup = sectionGroup;
00020 }
00021
00022 public ContentSectionTable Sections { get; protected set; }
00023
00024 public virtual T GetSection<T>(string sectionName) where T : ConfigurationSection
00025 {
00026 object section = ConfigurationManager.GetSection(sectionName);
00027 if (section == null) throw new ConfigurationErrorsException("Missing configuration section at '" + sectionName + "'");
00028 T contentSection = section as T;
00029 if (contentSection == null) throw new ConfigurationErrorsException("The configuration section at '" + sectionName + "' is of type '" + section.GetType().FullName + "' instead of '" + typeof(T).FullName + "' which is required.");
00030 return contentSection;
00031 }
00032
00033 public virtual T GetContentSection<T>(string relativeSectionName) where T : ConfigurationSectionBase
00034 {
00035 return GetSection<T>(sectionGroup + "/" + relativeSectionName);
00036 }
00037
00038 public virtual ConnectionStringsSection GetConnectionStringsSection()
00039 {
00040 return GetSection<ConnectionStringsSection>("connectionStrings");
00041 }
00042
00043 public virtual string GetConnectionString()
00044 {
00045 string connectionStringName = GetContentSection<DatabaseSection>("database").ConnectionStringName;
00046 return GetConnectionStringsSection().ConnectionStrings[connectionStringName].ConnectionString;
00047 }
00048
00049
00053 public class ContentSectionTable
00054 {
00055 public ContentSectionTable(HostSection web, EngineSection engine, DatabaseSection database, EditSection management)
00056 {
00057 Web = web;
00058 Engine = engine;
00059 Database = database;
00060 Management = management;
00061 }
00062
00063 public HostSection Web { get; protected set; }
00064 public EngineSection Engine { get; protected set; }
00065 public DatabaseSection Database { get; protected set; }
00066 public EditSection Management { get; protected set; }
00067 }
00068
00069 #region IAutoStart Members
00070
00071 public void Start()
00072 {
00073 Sections = new ContentSectionTable(
00074 GetContentSection<HostSection>("host"),
00075 GetContentSection<EngineSection>("engine"),
00076 GetContentSection<DatabaseSection>("database"),
00077 GetContentSection<EditSection>("edit"));
00078 }
00079
00080 public void Stop()
00081 {
00082 Sections = null;
00083 }
00084
00085 #endregion
00086 }
00087 }