Click here to Skip to main content
15,886,110 members
Articles / Programming Languages / Visual Basic

Declarative Codesnippet Automation with T4 Templates

Rate me:
Please Sign up or sign in to vote.
4.77/5 (15 votes)
20 Apr 2011CPOL15 min read 56.5K   1.4K   36  
This article describes a technique for automating codesnippets which are associated with a class via attributes. This results in a declarative approach to the generation of boiler-plate code.
using System;
using System.Collections.Generic;

namespace TelemetryData.Telemetry
{
    /// <summary>
    /// Represents a series of TimestampedDataPoints along with a Name and the Index of the
    /// column the series is in the .csv file.
    /// </summary>
    public class TelemetrySeries
    {
        #region Fields

        private double _maximumValue;
        private double _minimumValue;

        #endregion

        #region Properties

        /// <summary>
        /// The Name of the Series.
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// The column index of the series in the csv file.
        /// </summary>
        public int IndexInCSV { get; set; }

        /// <summary>
        /// The List of TimestampedDataPoints that the series contains.
        /// </summary>
        public List<TimestampedDataPoint> DataList { get; set; }

        /// <summary>
        /// Returns the Maximum Value in the DataList.
        /// </summary>
        public double Maximum
        {
            get
            {
                FindExtremeValues();
                return _maximumValue;
            }
        }

        /// <summary>
        /// Returns the Minimum Value in the DataList.
        /// </summary>
        public double Minimum
        {
            get
            {
                FindExtremeValues();
                return _minimumValue;
            }
        }

        #endregion

        #region Methods

        #region Constructors

        /// <summary>
        /// Creates a TelemetrySeries when given a name and IndexInCSV value.
        /// </summary>
        /// <param name="name">The name of the series.</param>
        /// <param name="indexInCSV">The index of the series in the .csv file.</param>
        public TelemetrySeries(string name, int indexInCSV)
        {
            Name = name;
            IndexInCSV = indexInCSV;
            DataList = new List<TimestampedDataPoint>();
        }

        #endregion

        /// <summary>
        /// Finds the maximum and the minimum values in the DataList.
        /// </summary>
        private void FindExtremeValues()
        {
            _maximumValue = 0;
            _minimumValue = 0;
            foreach (TimestampedDataPoint point in DataList)
            {
                if (point.Value > _maximumValue)
                    _maximumValue = point.Value;
                if (point.Value < _minimumValue)
                    _minimumValue = point.Value;
            }
        }

        #endregion
    }

    /// <summary>
    /// Represents a point in time and a value for a series.
    /// </summary>
    public class TimestampedDataPoint
    {
        #region Properties

        /// <summary>
        /// The time of the Data Point
        /// </summary>
        public DateTime Time { get; set; }

        /// <summary>
        /// The value of the Data Point
        /// </summary>
        public double Value { get; set; }

        #endregion

        #region Constructors

        /// <summary>
        /// Creates a TimestampedDataPoint with a time and value.
        /// </summary>
        /// <param name="time">The time of the Data Point.</param>
        /// <param name="v">The value of the Data Point.</param>
        public TimestampedDataPoint(DateTime time, double v)
        {
            Time = time;
            Value = v;
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Scott Logic
United Kingdom United Kingdom
I am CTO at ShinobiControls, a team of iOS developers who are carefully crafting iOS charts, grids and controls for making your applications awesome.

I am a Technical Architect for Visiblox which have developed the world's fastest WPF / Silverlight and WP7 charts.

I am also a Technical Evangelist at Scott Logic, a provider of bespoke financial software and consultancy for the retail and investment banking, stockbroking, asset management and hedge fund communities.

Visit my blog - Colin Eberhardt's Adventures in .NET.

Follow me on Twitter - @ColinEberhardt

-

Comments and Discussions