Saturday, September 1, 2012

.NET Simple Recursive ConfigurationElementCollections

I've seen plenty of quite complicated examples of how to load recursive configuration element collections from app.config. Here's a really simple example.

I have the app.config below. You'll see that a person can have children. Each child is also a person and so each child can have children, and so on.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
      <section name="myPeople" type="NestedCustomConfig.PeopleConfigSection, NestedCustomConfig" />
  </configSections>

  <myPeople>
    <people>
      <add name="Frank" />
      <add name="Stanley">
        <children>
          <add name="Pauline">
            <children>
              <add name="Augustus"/>
              <add name="Zebedee" />
            </children>
          </add>
        </children>
      </add>
    </people>
  </myPeople>
  
</configuration>
To load this, I have the following classes:


using System.Configuration;

namespace NestedCustomConfig
{
    public class PersonConfigurationElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true, IsKey = true)]
        public string Name
        {
            get { return (string) base["name"]; }
            set { base["name"] = value; }
        }

        [ConfigurationProperty("children", IsDefaultCollection = false, IsRequired = false)]
        public People Children
        {
            get { return (People) base["children"]; }
            set { base["children"] = value; }
        }
    }

    public class People : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new PersonConfigurationElement();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((PersonConfigurationElement)element).Name;
        }
    }

    public class PeopleConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("people", IsDefaultCollection = false)]
        public People People
        {
            get
            {
                return (People)base["people"];
            }
        }
    }
}
I created a Console App for the example and have the following in Program.cs:
using System.Configuration;

namespace NestedCustomConfig
{
    class Program
    {
        static void Main()
        {
            var peopleSection = ConfigurationManager.GetSection("myPeople");
        }
    }
}
Good luck.

Saturday, June 2, 2012

Swiss Mortgage Example

In case you fancy buying a 1,000,000 CHF residential property in beautiful Switzerland:

Purchase price: 1,000,000
20% Deposit: 200,000
First mortgage 66% of purchase price: 660,000 - renegotiate every 10 (max) years
Second mortgage 14% of purchase price: 140,000 - must be paid off in (max) 24 years

5% * First Mortgage amount has to be less than 30% of your gross income

Simple!

Thursday, May 17, 2012

Old laptop running hot after Windows 7 upgrade

So, I finally upgraded my aging Windows XP laptop to Windows 7. The Windows 7 Upgrade Advisor assured me my pc had enough resources to handle the (not so) new OS. The install went fine, but I noticed that the machine was getting very hot. Installing Speedfan allowed me to monitor the temp, which was over 90C at times.

A quick look at the Performance tab of the Task Manager (while watching an excellent talk on Reactive Extensions) showed me the CPU was maxing out. Next stop was the processes tab, showing processes from all users and ordering by CPU usage. Pretty close to the top was wmpnetwk.exe.

This is Windows Media Player Network Sharing Service, which, according to the its description in Services, "Shares Windows Media Player libraries to other networked players and media devices using Universal Plug and Play".

I decided I didn't need this, especially at the expense of running my CPU at over 90C (I already have a device for those kind of temperatures: a kettle). So, I went to Services, selected "Windows Media Player Network Sharing Service", right-clicked and chose properties from the context menu, set Start Up Type to disabled and stopped the service.

Since then, the temp has reduced to between 40 and 50C. Still fairly high maybe (need to clean the cooling system probably), but manageable.