Archive

Posts Tagged ‘VS 2010’

Basic Internal Dsl for Creating a Table Fluently :)

March 30, 2011 1 comment
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Html.Table.Dsl
{

    public static class ObjectHtmlExtensions
    {
        public static string GetValuesAsSeperatedTdTags(this object @object)
        {
            var type = @object.GetType();
            string output = String.Empty;
            foreach (var property in type.GetProperties())
            {
                output += "<td>" + property.GetGetMethod().Invoke(@object, null) + "</td>";
            }
            return output;
        }
    }

    public class TableModel
    {
        public string Caption { get; set; }
        public string Id { get; set; }
        public string Class { get; set; }
        public IList<TableHeader> Headers { get; set; }
        public IList<object> Objects { get; set; }

        public IEnumerable Rows
        {
            get { return Objects; }
        }

        public TableModel()
        {
            Caption = String.Empty;
            Headers = new List<TableHeader>();
            Objects = new List<object>();
        }

        public void AddHeader(string name)
        {
            Headers.Add(new TableHeader(name));
        }

        public void SetClassFor(int index, string name)
        {
            Headers[index].Class = name;
        }

        public void SetAbsoluteWidthFor(int index, int width)
        {
            Headers[index].AbsoluteWidth = width;
        }

        public void SetPercentageWidthFor(int index, int width)
        {
            Headers[index].PercentageWidth = width;
        }

        public void AddObject(object entity)
        {
            Objects.Add(entity);
        }

        public void SetCaption(string caption)
        {
            Caption = caption;
        }

        public void SetClass(string @class)
        {
            Class = @class;
        }

        public void SetId(string id)
        {
            Id = id;
        }

        public void SetIdFor(int index, string id)
        {
            Headers[index].Id = id;
        }
    }

    public class TableHeader
    {
        public TableHeader(string name)
        {
            Name = name;
        }

        public string Id { get; set; }
        public string Name { get; set; }
        public string Class { get; set; }
        public int? AbsoluteWidth { get; set; }
        public int? PercentageWidth { get; set; }
    }

    public interface ITableCreater
    {
        ITableOptions CreateTable { get; }
    }

    public interface ITableOptions
    {
        ITableOptions WithCaption(string caption);
        ITableHeaders WithHeaders();
        ITableOptions WithClass(string @class);
        ITableOptions WithId(string id);
    }

    public interface ITableHeaders
    {
        ITableHeaders Add(string name);
        ITableHeaders Class(string name);
        ITableHeaders Id(string id);
        ITableHeaders WithAbsoluteWidth(int width);
        ITableHeaders WithPercentageWidth(int width);
        ITableData WithData();
    }

    public interface ITableData
    {
        ITableData Add(object entity);
        string Render();
    }

    class TableDataImpl : ITableData
    {
        public TableModel Model { get; set; }

        public TableDataImpl(TableModel model)
        {
            Model = model;
        }

        public ITableData Add(object entity)
        {
            Model.AddObject(entity);
            return this;
        }

        public string Render()
        {

            string output = String.Empty;
            output += "<table";

            output += AddIdFor();
            output += AddClassFor();
            output += AddCaptionFor();

            output += ">";

            output += AddHeaders();

            foreach (var entity in Model.Rows)
            {
                output += "<tr>";
                output += entity.GetValuesAsSeperatedTdTags();
                output += "</tr>";
            }

            return output + "</table>";
        }

        private string AddHeaders()
        {
            string output = String.Empty;
            if (Model.Headers.Any())
            {
                output += "<tr>";
                foreach (var header in Model.Headers)
                {
                    output += "<th";
                    output += AddIdForHeader(header);
                    output += AddClassForHeader(header);
                    output += AddWidthForHeader(header);
                    output += ">";
                    output += header.Name;
                    output += "</th>";
                }
                output += "</tr>";
            }
            return output;
        }

        private string AddWidthForHeader(TableHeader header)
        {
            string output = String.Empty;
            if (header == null || !(header.PercentageWidth.HasValue || header.AbsoluteWidth.HasValue))
                return output;

            if (header.AbsoluteWidth.HasValue)
            {
                output += " width='" + header.AbsoluteWidth + "' ";
                return output;
            }

            if (header.PercentageWidth.HasValue)
            {
                output += " width='" + header.AbsoluteWidth + "%' ";
                return output;
            }
            return output;
        }

        private string AddClassForHeader(TableHeader header)
        {
            string output = String.Empty;
            if (header == null || String.IsNullOrWhiteSpace(header.Class))
                return output;

            if (!String.IsNullOrWhiteSpace(Model.Class))
                output += " class='" + header.Class + "' ";
            return output;
        }

        private string AddIdForHeader(TableHeader header)
        {
            string output = String.Empty;
            if (header == null || String.IsNullOrWhiteSpace(header.Id))
                return output;

            if (!String.IsNullOrWhiteSpace(Model.Caption))
                output += " id='" + header.Id  + "' ";
            return output;
        }

        private string AddCaptionFor()
        {
            string output = String.Empty;
            if (!String.IsNullOrWhiteSpace(Model.Caption))
                output += " Caption='" + Model.Caption + "' ";
            return output;
        }

        private string AddClassFor()
        {
            string output = String.Empty;
            if (!String.IsNullOrWhiteSpace(Model.Class))
                output += " class='" + Model.Class + "' ";
            return output;
        }

        private string AddIdFor()
        {
            string output = String.Empty;
            if (!String.IsNullOrWhiteSpace(Model.Id))
                output += " id='" + Model.Id + "' ";
            return output;
        }
    }

    public class TableHeadersImpl : ITableHeaders
    {

        private readonly TableModel _model;
        private int _index = -1;

        public TableHeadersImpl(TableModel model)
        {
            _model = model;
            _index = -1;
        }

        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }

        public TableModel Model
        {
            get { return _model; }
        }

        public ITableHeaders Add(string name)
        {
            ++Index;
            Model.AddHeader(name);
            return this;
        }

        public ITableHeaders Class(string name)
        {
            Model.SetClassFor(Index, name);
            return this;
        }

        public ITableHeaders Id(string id)
        {
            Model.SetIdFor(Index, id);
            return this;
        }

        public ITableHeaders WithAbsoluteWidth(int width)
        {
            Model.SetAbsoluteWidthFor(Index, width);
            return this;
        }

        public ITableHeaders WithPercentageWidth(int width)
        {
            Model.SetPercentageWidthFor(Index, width);
            return this;
        }

        public ITableData WithData()
        {
            return new TableDataImpl(Model);
        }
    }

    public class TableOptionsImpl : ITableOptions
    {
        private readonly TableModel _model;

        public TableOptionsImpl(TableModel model)
        {
            _model = model;
        }

        public TableModel Model
        {
            get { return _model; }
        }

        public ITableOptions WithCaption(string caption)
        {
            Model.SetCaption(caption);
            return this;
        }

        public ITableHeaders WithHeaders()
        {
            return new TableHeadersImpl(Model);
        }

        public ITableOptions WithClass(string @class)
        {
            Model.SetCaption(@class);
            return this;
        }

        public ITableOptions WithId(string id)
        {
            Model.SetId(id);
            return this;
        }
    }

    public class Fluently : ITableCreater
    {
        public ITableOptions CreateTable
        {
            get { return new TableOptionsImpl(new TableModel()); }
        }
    }

    class Program
    {
        static void Main()
        {

            string output = new Fluently()
                .CreateTable
                .WithCaption("Cool table")
                .WithClass("Class")
                .WithId("#Id")
                .WithHeaders()
                    .Add("Name")
                        .Class("cool")
                        .WithAbsoluteWidth(100)
                    .Add("Age")
                        .Class("cool")
                        .WithAbsoluteWidth(100)
                .WithData()
                    .Add(new { Name = "Blair", Age = 100 })
                .Render();

            Console.WriteLine(output);
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }
    }
}

Covariance in C#

February 28, 2011 1 comment

We are going to have a quick look at covariance in C#.

Only elements that can be covariant:

  • Interfaces
  • Delegates

Convariance is similar to Inheritance in the sense that if you have a class like IEnumerable which is covariant then you can assign a subclass of IEnumerable to IEnumerable.

Example: Base Person, Sub Class is Student. With Covariance you can assign IEnumerable to IEnumerable

You would just naturally think this would work but is has not until .NET 4 was introduced.

To allow convariance you must mark the generic parameter as out T as shown below in IIterable and IIterator. I wrote my own iterator design pattern based classes below to illustrate this.

you can only assign using references as shown in the main method. Assigning to base classes not interfaces won’t work.

using System;
using System.Collections.Generic;

namespace Variance
{


    public abstract class Entity<TKey> : IEquatable<Entity<TKey>>
    {

        public TKey Id { get; set; }

        protected Entity()
        {
            Id = default(TKey);
        }

        protected Entity(TKey identity)
        {
            Id = identity;
        }

        public override bool Equals(object obj)
        {
            var entity = obj as Entity<TKey>;
            return Equals(entity);
        }

        public bool Equals(Entity<TKey> other)
        {
            return other != null && Equals(Id, other.Id);
        }

        public override int GetHashCode()
        {
            return Id.GetHashCode();
        }
    }

    public class Person : Entity<int>
    {
        public string Name { get; set; }

        public override string ToString()
        {
            return String.Format("{0} - {1}", Id, Name);
        }
    }

    public class Student : Person
    {
        public string Course { get; set; }
    }

    public interface IIterator<out T>
    {
        bool HasMoreElements { get; }
        T Current { get; }
        void Continue();
    }

    public interface IIterable<out T>
    {
        IIterator<T> GetIterator();
    }

    public class BasicList<T> : IIterable<T>
    {

        protected List<T> Items = new List<T>();

        public void AddItem(T item)
        {
            Items.Add(item);
        }

        public IIterator<T> GetIterator()
        {
            return new BlIterator<T>(Items);
        }

        public void PrinttoConsole()
        {
            foreach (var item in Items)
            {
                Console.WriteLine(item);
            }
        }

        public class BlIterator<T> : IIterator<T>
        {

            protected List<T> items = new List<T>(); 

            public BlIterator(List<T> items)
            {
                this.items = items;
            }

            private int index;

            public bool HasMoreElements
            {
                get { return index <= items.Count - 1; }
            }

            public T Current
            {
                get { return items[index]; }
            }

            public void Continue()
            {
                index++;
            }
        }


    }

    class Program
    {

        

        static void Main()
        {

            IIterable<Student> students = GetStudents();

            Print(students); // Covariance



            IIterable<Person> people = students; // Covariance

            Print(people);

            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }

        public static IIterable<Student> GetStudents()
        {
            BasicList<Student> students = new BasicList<Student>();
            students.AddItem(new Student { Id = 1, Name = "Blair" });
            students.AddItem(new Student { Id = 2, Name = "Tim" });
            students.AddItem(new Student { Id = 3, Name = "Tom" });
            return students;
        }

        public static void Print(IIterable<Person> print) // Covariance
        {

            var iterator = print.GetIterator();

            while (iterator.HasMoreElements)
            {
                Console.WriteLine(iterator.Current);
                iterator.Continue();
            }
        }
    }
}

Blair…

Redirect to a safe location Error Action Filter in ASP.NET MVC

February 15, 2011 Leave a comment

I have an example of an error action filter on how you can move the user to a previously know safe location in mvc with the following action filter. I and an error to tempdata which you could look for in the site master to show the error.

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;

namespace Learning.ActionFilters
{
    public class RedirectToRouteErrorAttribute : FilterAttribute, IExceptionFilter
    {
        public IEnumerable<string> RouteValues { get; private set; }
        public string ErrorMessage { get; private set; }

        public RedirectToRouteErrorAttribute(string errormessage, params string[] tokens)
        {
            ErrorMessage = errormessage;
            RouteValues = tokens;
        }

        public void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled)
                return;

            var items = from route in filterContext.RouteData.Values
                        join key in RouteValues on route.Key equals key
                        select route;

            var dict = new RouteValueDictionary(items);

            filterContext.Controller.TempData.Add("errormessage", ErrorMessage);
            filterContext.Result = new RedirectToRouteResult(dict);
            filterContext.HttpContext.Response.Clear();
            filterContext.ExceptionHandled = true;
        }
    }
}

// Here is an example of the usage
 [HttpPut]
 [ActionName("Edit")]
 [RedirectToRouteErrorAttribute("controller","action","id")]
 public virtual ActionResult EditUser(){}

You just specify which route values you want to keep so the RedirectToRouteResult work as required

Blair…

Windows Forms and TPL with BlockingCollections

November 18, 2010 Leave a comment

Just Love Task Parallel Library
Link

Blair..

Parallel Loops and Exiting Early

November 18, 2010 Leave a comment

Quick demo on how to exit early from a parallel loop. You can use either break nor stop. Stop terminate loops as early as possible while break lets early scheduled loops complete till completion.

using System;
using System.Threading.Tasks;

namespace ParallelLoops
{
    class Program
    {
        public static void Main()
        {
            Program program = new Program();
            program.ExitingLoopsPrematurely();

            Console.WriteLine("Press any key to quit...");
            Console.ReadKey();
        }

        public void ExitingLoopsPrematurely()
        {
            var result = Parallel.For(1, 100, (value, state) =>
                                     {
                                         if (value % 20 == 0)
                                             state.Break();

                                         Console.WriteLine("Value: {0}", value);

                                     });

            Console.WriteLine("Completed {0}, Exited at Iteration: {1}", result.IsCompleted, result.LowestBreakIteration );
        }
    }
}

APM and TPL – Reading Files

November 16, 2010 Leave a comment
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace APMExamples
{
    class Program
    {
        static void Main()
        {
            Program p = new Program();
            p.ReadFileContentsAsyncAndUseAContinuationToPrintResults();

            Console.WriteLine("Press any key to quit..");
            Console.ReadKey();

        }

        public void ReadFileContentsAsyncAndUseAContinuationToPrintResults()
        {
            var stream = new FileStream(@"C:\Projects\TPLSamples\TPLSamples.sln", FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 8 * 1024, FileOptions.Asynchronous);
            byte[] data = new byte[stream.Length];

            var t = Task<int>.Factory.FromAsync(stream.BeginRead, stream.EndRead, data, 0, data.Length, null);
            t.ContinueWith(x =>
                               {
                                   Console.WriteLine(Encoding.ASCII.GetString(data));
                                   stream.Close();
                               });
        }
    }
}

Producer Consumer Scenarios with BlockingCollection

November 15, 2010 Leave a comment

Producer Consumer is really easy with TPL. You create a blocking collection and one task(producer) uses Add() to add items to it and another task(consumer) reads data from it using TryTake().

The consumer must signal to the consumers that it has finished adding by calling CompleteAdding().

using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Threading;

namespace BlockingCollectionSample
{
    public class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.TestingBlockingCollection();

            Console.WriteLine("Press any key to quit..");
            Console.ReadKey();
        }

        public void TestingBlockingCollection()
        {

            BlockingCollection<string> collection = new BlockingCollection<string>();

            var p = Task.Factory.StartNew(() =>
            {
                Random r = new Random();
                for (int i = 0; i < 5; i++)
                {
                    Thread.Sleep(1000);
                    int value = r.Next(100);
                    Console.WriteLine("Task {0} produced value {1}", Task.CurrentId, value);
                    collection.Add(value.ToString());
                }
                collection.CompleteAdding();

            });

            var c = Task.Factory.StartNew(() =>
            {
                Task.Factory.StartNew(() =>
                {
                    while (!collection.IsCompleted)
                    {
                        string value = null;
                        collection.TryTake(out value);

                        if (value != null)
                        {
                            Console.WriteLine("Task {0} consumed value {1}", Task.CurrentId, value);
                        }
                    }

                },TaskCreationOptions.AttachedToParent);

                Task.Factory.StartNew(() =>
                {
                    while (!collection.IsCompleted)
                    {
                        
                        string value = null;
                        collection.TryTake(out value);

                        if (value != null)
                        {
                            Console.WriteLine("Task {0} consumed value {1}", Task.CurrentId, value);
                        }
                    }

                }, TaskCreationOptions.AttachedToParent);

            });
            Task.WaitAll(p, c);
        }
    }
}

Could also be coded like the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Concurrent;
using System.Threading.Tasks;
using System.Threading;

namespace BlockingCollectionSample
{
    public class Program
    {
        static void Main(string[] args)
        {
            Program p = new Program();
            p.TestingBlockingCollection();

            Console.WriteLine("Press any key to quit..");
            Console.ReadKey();
        }

        public void TestingBlockingCollection()
        {

            BlockingCollection collection = new BlockingCollection();

            var p = Task.Factory.StartNew(() =&gt;
            {
                Random r = new Random();
                for (int i = 0; i 
            {
                Parallel.ForEach(collection.GetConsumingEnumerable(), x =&gt;
                {
                    Console.WriteLine("Consumer processed: {0}", x);
                });
            });

            Task.WaitAll(p, c);

        }


    }
}