The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Documenting is more Dynamic than you think

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jonathan Crossland

Posts: 630
Nickname: jonathanc
Registered: Feb, 2004

Jonathan Crossland is a software architect for Lucid Ocean Ltd
Documenting is more Dynamic than you think Posted: Feb 10, 2009 5:40 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Jonathan Crossland.
Original Post: Documenting is more Dynamic than you think
Feed Title: Jonathan Crossland Weblog
Feed URL: http://www.jonathancrossland.com/syndication.axd
Feed Description: Design, Frameworks, Patterns and Idioms
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Jonathan Crossland
Latest Posts From Jonathan Crossland Weblog

Advertisement
When we create objects and use config settings to ascertain context and things of that nature, it makes it harder to know what settings there are later on.

This is a significant fact across all things within software. Don't be scared to create small utilities and tools to aid your development. (As long as it does not affect your timelines).
Have you never got to a point in a project when you are wondering why something is not working and it ends up being a config setting?

Here is a simple solution for simple key/value settings

Create an Attribute to house the "documentation".

[code:c#]

[AttributeUsage(AttributeTargets.Class)]
    public sealed class ConfigurationRequiredAttribute : Attribute
    {

        private string[] _ConfiguratonNames;

      
        public ConfigurationRequiredAttribute(params string[] configNames)
        {
            _ConfiguratonNames = configNames;
        }

        /// <summary>
        /// 
        /// </summary>
        public string[] ConfiguratonNames
        {
            get
            {
                return _ConfiguratonNames;
            }
            set
            {
                _ConfiguratonNames = value;
            }
        }
       
    }
[/code]

Next, mark your classes that use simple key/value pair config settings.
[code:c#]
[ConfigurationRequiredAttribute("CopyDestinationDirectory", "MyKey2")]
    public class CopyDirectory : IAction
    {}
[/code]

Then write yourself a CustomAttribute tool.
Simply put, use reflection on all your classes, look for the Attribute and declare what the names of config settings you need across the app.

[code:c#]
	public static class StaticValidator
{
private static bool ConfigKeyExists(string settingName, Configuration config)
{
  
    AppSettingsSection appSettings = config.AppSettings;

    if (appSettings != null)
    {
        if (appSettings.Settings != null)
        {
            if (appSettings.Settings.Count > 0)
            {
                KeyValueConfigurationElement setting = 
                     appSettings.Settings[settingName];

                if (setting != null)
                {
                    return true;
                }
            }
        }
    }

    return false;
}

public static bool HasConfigSettings(object toValidate, Configuration config)
{
    
    //Check to make sure the object parameter is not null - 
    //if it is, throw an exception.
    if (toValidate == null)
    {
        throw 
        new ArgumentNullException(
           "toValidate", "Object not set");
    }
    
    bool ret = false;

    ConfigurationRequiredAttribute[] required = null;

    
    //Get all the property info for the properties on the object to validate
    //that are instance and public properties.
    required = (ConfigurationRequiredAttribute[])
               toValidate.GetType().GetCustomAttributes( 
    typeof(ConfigurationRequiredAttribute), true);

   
    //if any were found then...
    if (required.Length > 0)
    {
       
        //Enumerate around the requiredTypes attribute that were found.
        foreach (ConfigurationRequiredAttribute requiredType in required)
        {
           
            //Enumerate through all of the types that are required in the 
            // requiredType attribute.
            foreach (string key in requiredType.ConfiguratonNames)
            {
                if (!ConfigKeyExists(key, config))
                {
                    throw new Exception("The AppSettings.Key: " + 
key + " does not exist");
                }
                else
                    ret = true;
            }

            return ret;
        }
        
    }

    return ret;
}

}
[/code]

And you call it like this

[code:c#]
 //my config to check
 Configuration config = 
  ConfigurationManager.OpenExeConfiguration( 
     ConfigurationUserLevel.None);

 StaticValidator.HasConfigSettings(
     myObjWithAttribute, config);
[/code]

The point

Documentation is one thing, but dynamic documentation, or Code MetaData is required for us to move ahead in understanding code from the outside.

This example is a simple one, but hopefully demonstrates the importance of MetaData on code, which can lead to better inspection tools and more robustness.

Comments?

Read: Documenting is more Dynamic than you think

Topic: Get Started with the Microsoft Security Development Lifecycle Pro Network Previous Topic   Next Topic Topic: Tech&#183;Ed North America 2009 May 11-15: Register Today and Save US$200

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use