Tuesday, December 18, 2018

.NET Generic Configuration Section

If we need to extend an app.config file of .net application with a deep level configuration section like this
  
  <DevicesConfiguration>
    <devices>
      <device type="SM_G920I###6.0.1">
        <touchActivities>
          <touchActivity type="ToggleFlightMode">
            <points>
              <point x="500" y="1800" id="1"/>
              <point x="1300" y="430" id="2"/>
            </points>
          </touchActivity>
          <touchActivity type="ToggleVOLTE">
            <points>
              <point x="500" y="2400" id="1"/>
              <point x="1300" y="1000" id="2"/>
            </points>
          </touchActivity>
        </touchActivities>
      </device>
      <device type="SM_J510FN###7.1.1">
        <touchActivities>
          <touchActivity type="ToggleFlightMode">
            <points>
              <point x="340" y="220" id="1"/>
              <point x="340" y="610" id="2"/>
              <point x="640" y="200" id="3"/>
            </points>
          </touchActivity>
          <touchActivity type="Enable3G">
            <points>
              <point x="340" y="220" id="1"/>
              <point x="360" y="960" id="2"/>
              <point x="370" y="390" id="3"/>
              <point x="270" y="580" id="4"/>
            </points>
          </touchActivity>
        </touchActivities>
      </device>
    </devices>
  </DevicesConfiguration>
we have to create at least 3 classes inherited from ConfigurationElementCollection to incapsulate 'devices', 'touchActivities' and 'points' sections. It could be too much code unless we create generic ConfigurationElementCollection and use it everywhere instead of creation separate classes.