Category Archives: Core

Cinchoo – Framework Tips

Here I’m going to talk about couple of tips on using Cinchoo framework in your project.

At the start of the application, you must call ChoFramework.Initialize() to initialize the framework and start necessary services. Usually you must place this statement in application Main entry point as below.

Calling ChoFramework.Shutdown() method is not mandatory to call. But it is safe to make this call for graceful shutdown of all the background services.

static void Main(string[] args)
{
    ChoFramework.Initialize();
    try
    {
        ChoApplication.ApplyFrxParamsOverrides += new EventHandler<ChoFrxParamsEventArgs>(ChoApplication_ApplyFrxParamsOverrides);

        SampleConfigSection ApplicationSettings = new SampleConfigSection();
    }
    catch (Exception ex)
    {
        Console.WriteLine("ERROR: " + ex.Message);
    }
    finally
    {
        ChoFramework.Shutdown();
    }
}

ChoAppDomain.


Cinchoo – Overriding Framework Configuration Parameters

In this section, I’ll go over the list of Cinchoo framework configuration parameters can be overridden to control the run-time environment of the framework either through configuration or programmatically.

Framework will run fine without overriding these parameters. Only when you need, you can override them. In those situation, it can be done through configuration or programmatically.

First, we will see how to override them through configuration. All the framework parameters must be stored in ChoCoreFrx.config file under application executable folder. This is the default behavior of discovering this file by the framework. But this can be overridden by specifying the path to this file through AppSettings in App.config file as below

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="CinchooFrxConfigPath" value="C:\Config\CoreFrx.config"/>
  </appSettings>
</configuration>

GlobalApplicationSettings

There are the application level settings used by framework. Below are the attributes and their usages.

  • globalApplicationSettings – Global application parameters used by framework. All the variables are optional.
    • singleInstanceApp [bool] – true, to ensure there is only one instance of the application running at any point in time. false, lets you to run multiple instances of the application. Default is false.
    • applicationId [string] – the custom application name. Default value is the executable name.
    • eventLogSourceName [string] – Event log source name where all the errors are posted on this name, default value is applicationId.
    • useApplicationDataFolderAsLogFolder [bool] – true, it uses the current roaming user directory. Default is false.
    • logFolder [string] – A log directory, where all the log files are created by framework. Default is ‘[appExePath]\Logs’.
    • appConfigPath [string] – configuration file path. If not specified, this will be [appExeFolder].[appExeName].xml
    • logTimeStampFormat [string] – A time stamp format used when logging information to log files. Default value is ”yyyy-MM-dd hh:mm:ss.fffffff”
    • traceLevel [int] – Specifies what message to output for the Trace/ChoTrace classes. 0-Off, 1-Error, 2-Warning, 3-Info, 4-Verbose.

Here is the sample Global Application Settings section in ChoCoreFrx.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <globalApplicationSettings singleInstanceApp="false" applicationId="TestApplication" eventLogSourceName="TestApplication" useApplicationDataFolderAsLogFolder="false">
    <logFolder>C:\Logs</logFolder>
    <appConfigPath>c:\config\TestApplication.xml</appConfigPath>
    <logTimeStampFormat>yyyy-MM-dd hh:mm:ss.fffffff</logTimeStampFormat>
  </globalApplicationSettings>
</configuration>

MetaDataFilePathSettings

Cinchoo framework creates as well as uses number of Meta-Data files, these file paths can be configured through ChoCoreFrx.config file.

  • configurationMetaDataFilePath – meta data file path used by Configuration manager. By default, it’s value is [appExeName].config.meta
  • pcMetaDataFilePath – meta data file path used by Performance Counter manager. By default, its value is [appExeName].perf.meta

Here is the sample xml section looks like

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <metaDataFilePathSettings>
    <configurationMetaDataFilePath>c:\Config\configurationMetaFile.meta</configurationMetaDataFilePath>
    <pcMetaDataFilePath>c:\Config\PCMetaFile.meta</pcMetaDataFilePath>
  </metaDataFilePathSettings>
</configuration>

Run-Time approach

Lets see how we can override the system parameters at run-time in here. Cinchoo framework exposes ChoApplication.ApplyFrxParamsOverrides event. By subscribing to this event, you have a chance to override the framework parameters at run-time. See below the sample code on how to override some of the system parameters

static void Main(string[] args)
{
    try
    {
        ChoApplication.ApplyFrxParamsOverrides += new EventHandler<ChoFrxParamsEventArgs>(ChoApplication_ApplyFrxParamsOverrides);
        XmlApplicationSettings ApplicationSettings = new XmlApplicationSettings();
    }
    finally
    {
        ChoAppDomain.Exit();
    }
}

static void ChoApplication_ApplyFrxParamsOverrides(object sender, ChoFrxParamsEventArgs e)
{
    e.GlobalApplicationSettings.SingleInstanceApp = true;
    e.MetaDataFilePathSettings.ConfigurationMetaDataFilePath = @"C:\Config\MetaDataFilePath.meta";
}

Cinchoo – Simplified Object Equality

In this post I present a way to implement equality between objects. Implicit support for == and != operators. There is the big writeup on Guidelines for Overloading Equals() and Operator == (C# Programming Guide) at MSDN that you can follow when you implement Equals() method for your custom type. But Cinchoo framework provides easy way to accomplish this. Read below on how to do it.

Reference Types

The Equals method is just a virtual one defined in System.Object, and overridden by whichever classes choose to do so. By default it provides reference equality.

The == operator is an operator which can be overloaded by classes, but which usually has identity behavior. The == operator has not been overloaded, it compares whether two references refer to the same object – which is exactly what the implementation of Equals does in System.Object.

Cinchoo framework provides abstraction through ChoEquatableObject<T> class, which exposes Equals() method, == and != operators for you. The default implementation of Equals() method uses reflection to make the comparison. This can be overloaded in your derived class.

1. Add reference to Cinchoo.Core.dll assembly

2. Namespace Cinchoo.Core

For the sample class derived from ChoEuatableObject<T>, uses the default implementation of object equality

public class Security : ChoEquatableObject<Security>
{
    public string Symbol;
    [ChoIgnoreEqual]
    public double Price;
    [ChoIgnoreEqual]
    public int Qty;
}

ChoIgnoreEqual tell the framework to ignore those members while making equality of the objects. In the above Security class, it only uses Symbol member while making equality and ignore other members.

If you worry about performance of using reflection when making equality of objects, you can consider overriding Equals() method as below

public class Security : ChoEquatableObject<Security>
{
    public string Symbol;
    public double Price;
    public int Qty;

    public override bool Equals(Security other)
    {
        if (object.ReferenceEquals(other, null))
            return false;

        return Symbol == other.Symbol;
    }
}

The sample test code below

class Program
{
    static void Main(string[] args)
    {
        Security sec1 = new Security() { Symbol="AAPL", Price = 555.10, Qty = 10 };
        Security sec2 = new Security() { Symbol="AAPL", Price = 655.55, Qty = 500 };

        Console.WriteLine("ChoObject.Equals() : " + ChoObject.Equals(sec1, sec2));
        Console.WriteLine("Object.Equals() : " + sec1.Equals(sec2));
        Console.WriteLine("== operator: " + (sec1 == sec2));
        Console.WriteLine("!= operator: " + (sec1 != sec2));
    }
}

The output will be

ChoObject.Equals() : True
Object.Equals() : True
== operator: True
!= operator: False

In cases where you do not want to derive from ChoEquableObject<T> or your class already in the inheritance chain, please follow the next section in implementing object equality.

Value Types

Value types do not provide an overload for == by default. However, most of the value types provided by the framework provide their own overload. The default implementation of Equals for a value type is provided by ValueType , and uses reflection to make the comparison.

Below is the way to implement ValueType equality or a reference type equality through Interface

public struct Security : IEquatable<Security>
{
    public string Symbol;
    public double Price;
    public int Qty;

    #region IEquatable<Security> Members

    public override bool Equals(object obj)
    {
        return Equals((Security)obj);
    }

    public bool Equals(Security other)
    {
        if (object.ReferenceEquals(other, null))
            return false;

        return Symbol == other.Symbol;
    }

    public static bool operator ==(Security a, Security b)
    {
        return ChoObject.Equals(a, b);
    }

    public static bool operator !=(Security a, Security b)
    {
        return !ChoObject.Equals(a, b);
    }

    #endregion
}



Follow

Get every new post delivered to your Inbox.