Saturday 4 February 2012

ENRICH ENUMS WITH ATTRIBUTES AND RETRIEVE VALUES GENERICALLY



Working on repayment frequency enumeration and its annual occurence,  I came up with the idea of implementing field attributes. Then found a good way to obtain the value of annual occurences which is embedded to the attribute. I implemented an enumeration extension to achieve this.

This is a neat and tidy way to enrich enumerations with custom attributes and get the values of these attributes by an enumeration extension.

The enumeration class:

namespace MyApplication.Domain.Enumerations
{
  public enum RepaymentFrequency
  {
     [AnnualOccurence(12)] 
     [DaysImplied(30)]
     Monthly, 

     [AnnualOccurence(52)] 
     [DaysImplied(7)]
     Weekly,
        ....
        ....
}


The custom attribute class for AnnualOccurence (it will be pretty much the same for DaysImplied):

using System;
 
namespace MyApplication.Domain.Enumerations
{
    [AttributeUsage(AttributeTargets.Field)]
    public class AnnualOccurence : Attribute
    {
        public static readonly short Unassigned = -1;
 
        public AnnualOccurence()
        {
            _numberOfAnnualRepayments = Unassigned;
        }
 
        private short _numberOfAnnualRepayments;
        public short NumberOfAnnualRepayments
        {
            get { return _numberOfAnnualRepayments;}
        }
 
        public AnnualOccurence(short numberOfOccurences)
        {
            _numberOfAnnualRepayments = numberOfOccurences;
        }
 
        public override string ToString()
        {
            return "Annual Occurence = " + _numberOfAnnualRepayments.ToString();
        }
    }
}
Now, there is a good way to get the values of such attributes from an 'enumeration instance', by implementing the following extension:

using System;
using System.Linq;
 
namespace HomeStart.LoanCalculators.Domain.Enumerations
{
    public static class EnumExtensions
    {
        public static Expected GetAttributeValue<T, Expected>
                      (this Enum enumeration, Func<T, Expected> expression)
            where T : Attribute
        {
         T attribute = enumeration.GetType().GetMember(enumeration.ToString())[0]
                            .GetCustomAttributes(typeof(T), false)
                               .Cast<T>().SingleOrDefault();
 
            if (attribute == null)
                return default(Expected);
 
            return expression(attribute);
        }
    }
}
 
with the following usage:  
 
RepaymentFrequency repaymentFrequency = repaymentFrequency.Weekly;
... 
var numberOfAnnualRepayments = 
                 repaymentFrequency.GetAttributeValue<AnnualOccurence, short>
                                            (x => x.NumberOfAnnualRepayments);
var numberOfDaysImplied =  
                 repaymentFrequency.GetAttributeValue<DaysImplied, short> 
                                            (x => x.NumberOfDaysImplied);
...


where 52 will be assigned to numberOfAnnualRepayments
and 7 will be assigned to numberOfDaysImplied in this case.


Thursday 2 February 2012

JAVASCRIPT AND JQUERY CHART LIBRARIES


There are many javascript / jquery based client side web graphic tools that can be used for various purposes. So many that it is hard to decide which one to pick.

I have looked into the most popular and useful ones and assessed them in detail, based on their features and other factors like compatibility, learning curve.


Remember, the best tool changes depends on your purpose, type & size of chart you want to use and whether you are after more advanced features like dynamic rendering and interactivity.