Category Archives: PropertyReplacers

Cinchoo – Property Replacer, Part 2

Customizing Property Replacer – Dynamic Properties

We have learnt about defining and using static property in the previous section. In this section, I’m going to talk about defining and using Dynamic properties in your application. Dynamic properties are properties their values are resolved at the time of calling.

Below is the sample Property Replacer,

public class ChoDynamicPropertyReplacer : IChoKeyValuePropertyReplacer
{
    #region Instance Data Members

    private readonly Dictionary<string, string> _availPropeties = new Dictionary<string, string>()
        {
            { "HOME_PAGE", "Cinchoo framework website." },
            { "LAST_VISIT", "The last visit to the home page." }
        };

    #endregion Instance Data Members

    #region IChoKeyValuePropertyReplacer Members

    //Called by framework to see it is valid property replacer for the passed propertyName
    public bool ContainsProperty(string propertyName)
    {
        return _availPropeties.ContainsKey(propertyName);
    }

    //Yes it is, then framework calls this method to find the value for the property
    public string ReplaceProperty(string propertyName, string format)
    {
        switch (propertyName)
        {
            case "HOME_PAGE":
                return "http://www.cinchoo.com";
            case "LAST_VISIT":
                return ChoObject.Format(DateTime.Now, format);
            default:
                return null;
        }
    }

    //This method will be used to generate help
    public string GetPropertyDescription(string propertyName)
    {
        if (_availPropeties.ContainsKey(propertyName))
            return _availPropeties[propertyName];
        else
            return null;
    }

    #endregion

    #region IChoPropertyReplacer Members

    //Name of the property replacer
    public string Name
    {
        get { return "Dynamice Property Replacer"; }
    }

    //This property used to get list of supported properties and their descriptions
    public IEnumerable<KeyValuePair<string, string>> AvailablePropeties
    {
        get
        {
            foreach (KeyValuePair<string, string> keyValue in _availPropeties)
                yield return keyValue;
        }
    }

    #endregion
}

Here is the code to plug-in the above property replacer to the framework and using it.

static void Main(string[] args)
{
    ChoPropertyManagerSettings.Me.Add(new ChoDynamicPropertyReplacer());

    Console.WriteLine("Output of %%HOME_PAGE%% : %HOME_PAGE%".ExpandProperties());
    Console.WriteLine("Output of %%LAST_VISIT%% : %LAST_VISIT%".ExpandProperties());

    return;
}

When you run the above statements in an application, the output will be

Output of HOME_PAGE : http://www.cinchoo.com
Output of LAST_VISIT : 1/17/2012 1:07:23 PM

Hope this helps.

Cinchoo – String.ExpandProperties(), Part 2

Here I’ll show you how you can resolve text contains expressions and properties together.

ExpandProperties will do the expression evaluation along with property replacement in a string, and returns the output as string value. Please see the below samples on how to use it. Expressions are given with {{ and }} delimiters and properties are surrounded by %% delimiters.

How to use

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace System

Sample:

static void Main(string[] args)
{
    Console.WriteLine("Output of {{%%PROCESSOR_COUNT%% * 2}} :" + "{{%%PROCESSOR_COUNT%% * 2}}".ExpandProperties());
    Console.WriteLine("Output of {{%%TICK_COUNT%% / (60 * 60)}} :" + "{{%%TICK_COUNT%% / (60 * 60)}}".ExpandProperties());
    Console.WriteLine(@"Output of {{'%%SYSTEM_DIRECTORY%%' + '\Logs'}} :" + @"{{'%SYSTEM_DIRECTORY%' + '\Logs'}}".ExpandProperties());
}

In the above sample code, all the properties are resolved before expressions evaluated.

When you run the above code, the output will be

Output of {{%%PROCESSOR_COUNT%% * 2}} :4
Output of {{%%TICK_COUNT%% / (60 * 60)}} :384854
Output of {{'%%SYSTEM_DIRECTORY%%' + '\Logs'}} :%SYSTEM_DIRECTORY%\Logs
Press any key to continue . . .

Try it out! Happy coding!!!

Cinchoo – String.ExpandProperties(), Extension Method

In this section, I’ll talk about ExpandProperties extension method. This method used to find and replace any system / user defined properties found in a string. Properties are string begins and ends in %% separatorPlease see the below samples on how to use it. This routine returns string value.

Sample Properties are

%%APPLICATION_NAME%%
%%TODAY%%

How to use

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace System

Sample:

static void Main(string[] args)
{
    //Simple property expansion
    Console.WriteLine("Output of '%%APPLICATION_NAME%%' property:" + " %%APPLICATION_NAME%%".ExpandProperties());
    Console.WriteLine("Output of '%%TODAY%%' property:" + " %%TODAY%%".ExpandProperties());
    Console.WriteLine("Output of '%%PROCESSOR_COUNT%%' property:" + " %%PROCESSOR_COUNT%%".ExpandProperties());

    Console.WriteLine();

    //String containing properties
    Console.WriteLine("This machine has %%PROCESSOR_COUNT%% processors.".ExpandProperties());
}

When you run the above code, the output will be

Output of '%%APPLICATION_NAME%%' property: Cinchoo.Core.Test.exe
Output of '%%TODAY%%' property: 2/16/2012
Output of '%%PROCESSOR_COUNT%%' property: 2

This machine has 2 processors.

Press any key to continue . . .

Formatting Property Value

Some cases, you may want to format the property value. It can be done by passing format specification along with property name. Property Name and format specification are separated by ^ character. All .NET specific format specifiers are valid.

PS: Please refer Formatting Types from MSDN for more information about format specifiers used in .NET

%%PROPERTY_NAME^FORMAT_STRING%%

Sample:

static void Main(string[] args)
{
    //Formatted property expansion
    Console.WriteLine("Output of %%TODAY^yyyyMMdd%% property:" + " %%TODAY^yyyyMMdd%%".ExpandProperties());
    Console.WriteLine("Output of %%PROCESSOR_COUNT^#,#.00#;(#,#.00#)%% property:" + " %%PROCESSOR_COUNT^#,#.00#;(#,#.00#)%%".ExpandProperties());
    Console.WriteLine("Output of %%RANDOM_NO^C%% property:" + " %%RANDOM_NO^C%%".ExpandProperties());
}

When you run the above code, the output will be

Output of %%TODAY^yyyyMMdd%% property: 20120216
Output of %%PROCESSOR_COUNT^#,#.00#;(#,#.00#)%% property: 2.00
Output of %%RANDOM_NO^C%% property: ($92,640,205.00)

Press any key to continue . . .

Handling of Unknown Property

Cinchoo framework gracefully skips any unknown properties specified in the input string. Look below the sample

Sample:

static void Main(string[] args)
{
    Console.WriteLine("Output of %%UNKNOWN_PROPERTY^C%% property:" + " %UNKNOWN_PROPERTY^C%".ExpandProperties());
}

When you run the above code, the output will be

Output of %%UNKNOWN_PROPERTY^C%% property: %UNKNOWN_PROPERTY^C%

Press any key to continue . . .

How to resolve Unknown Property

Cinchoo framework provides a event handler to resolve any unknown properties failed to resolve. The event is exposed in ChoApplication.PropertyResolve event. Look at the sample below on how to handle this situation

Sample:

static void Main(string[] args)
{
    //Hook up the event handler
    ChoApplication.PropertyResolve += new EventHandler<Property.ChoUnknownProperyEventArgs>(PropertyResolve);

    Console.WriteLine("Output of %%UNKNOWN_PROPERTY^C%% property:" + " %%UNKNOWN_PROPERTY^C%%".ExpandProperties());
}

//Event handler
private static void PropertyResolve(object sender, Property.ChoUnknownProperyEventArgs e)
{
    if (e.PropertyName == "UNKNOWN_PROPERTY")
    {
        e.PropertyValue = "RESOLVED_VALUE";
        e.Resolved = true;
    }
}

When you run the above code, the output will be

Output of %%UNKNOWN_PROPERTY^C%% property: RESOLVED_VALUE

Press any key to continue . . .

Try it out! Happy coding!!!

Cinchoo – Property Replacer, Part 1

Customizing Property Replacer – Static Properties

If you need that you need to define and use list of your own properties for your requirements, you can do so by adding <key, value> pair to ChoStaticDictionaryPropertyReplacer class. There values are static, will not be evaluated for each time.  Cinchoo framework will take these values for property replacement in various places like Command Line parsing, Configuration Management, Object.ToString() etc.

Example, add CLUSTER_NAME property at the start of the application as below

ChoStaticDictionaryPropertyReplacer.Me.Add("CLUSTER_NAME", "CNYC100123023");

Then, you can use that property by using the below code

string msg = "Cluster Name is %CLUSTER_NAME%";
Console.WriteLine(ChoString.ExpandProperties(msg));

When you run the above statements in an application, the output will be

Cluster Name is CNYC100123023

Cinchoo – Command Line Argument Parser, Part 11

Inside Command Line Argument Parser – Available Properties

In here I’ll list all the available properties in Cinchoo framework.

Properties can be specified with % delimiters as below

%NOW%

1. Add a reference to Cinchoo.Core.dll

2. Namespace: Cinchoo.Core.Shell

Cinchoo framework allows you to pass property as command line argument. At run time, the framework replaces with property value.

Global Properties

Below are the list of global properties available for use as command line arguments

  • %APPLICATION_NAME% – Application Name.
  • %PROCESS_ID% – Process Identifier.
  • %THREAD_ID% – Thread Identifier.
  • %THREAD_NAME% – Managed .NET Thread Name, if any.
  • %RANDOM_NO% – Random integer value.
  • %TODAY% – Current Date.
  • %NOW% – The current date and time on this computer, expressed as the local time.

Environment Properties

Below are the list of Environment properties available for use as command line arguments

  • %CURRENT_DIRECTORY% – The fully qualified path of the current directory; that is, the directory from which this process starts.
  • %MACHINE_NAME% – The NetBIOS name of this local computer
  • %OS_VERSION% – An OperatingSystem object that contains the current platform identifier and version number.
  • %PROCESSOR_COUNT% – The number of processors on the current machine.
  • %SYSTEM_DIRECTORY% – The fully qualified path of the system directory.
  • %SYSTEM_PAGE_SIZE% – The amount of memory for an operating system’s page file.
  • %TICK_COUNT% – The number of milliseconds elapsed since the system started.
  • %USER_DOMAIN_NAME% – The network domain name associated with the current user.
  • %USER_NAME% – The user name of the person who is currently logged on to the Windows operating system.
  • %VERSION% – A Version object that describes the major, minor, build, and revision numbers of the common language runtime.
  • %WORKING_SET% – The amount of physical memory mapped to the process context.

Please visit PropertyReplacer section for more information about Property re-placers.