JG Vimalan's Blog

It is all about coding!

Creating an extended method

This post will discuss on creating extended methods,

1. Create a abstract class which implements the IEnumertor and ICollection interface.

using System;
using System.Collections;
using System.Collections.Generic;

namespace ClientManager.Base
{
/// <summary>
/// Create a class which implements the IEnumertor and ICollection interface.
/// This class will act as a base class for all collection classe’s.
/// </summary>
/// <typeparam name=”list”></typeparam>
[Serializable]
public abstract class Extender<list>:IEnumerable<list>, ICollection
{
private List<list> baseList = new List<list>();

#region IEnumerable Members

public IEnumerator<list> GetEnumerator()
{
return baseList.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return baseList.GetEnumerator();
}

#endregion

#region ICollection Members

public bool IsSynchronized { get { return (baseList as ICollection).IsSynchronized; } }

public int Count { get { return baseList.Count; } }

public object SyncRoot { get { return (baseList as ICollection).SyncRoot; } }

public void CopyTo(Array array, int index) { (baseList as ICollection).CopyTo(array, index); }

#endregion

#region List Members

protected void Add(list item)
{
baseList.Add(item);
}

protected void RemoveAt(int index)
{
baseList.RemoveAt(index);
}

protected void Clear()
{
baseList.Clear();
}

#endregion
}
}

2. Create a class which will hold the values,

using System;

namespace ClientManager
{
/// <summary>
/// This class will hold the patient basic information.
/// </summary>
[Serializable]
public class Patient
{
public string Name { get; set; }
public string Gender { get; set; }
public DateTime DateOfBirth { get; set; }
}
}

3. Create a class which inherits the Extender class and uses the Patient class to store values,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClientManager
{
/// <summary>
/// This class holds the collection of patient records.
/// Add’s custom methods which can hold the business logic.
/// </summary>
public class PatientCollection:Base.Extender<Patient>
{
public void AddPatient(Patient patient)
{
base.Add(patient);
}

public PatientCollection SortByBirthDate()
{
var patients = from Patient patient in this orderby patient.DateOfBirth descending select patient;
return PrepareResult(patients);
}

public PatientCollection GetByGender(string gender)
{
var patients = from Patient patient in this where patient.Gender.ToLower().Equals(gender.ToLower()) orderby patient.DateOfBirth descending select patient;
return PrepareResult(patients);
}

public PatientCollection PrepareResult(IOrderedEnumerable<Patient> patients)
{
var patientCollection = new PatientCollection();

if (patients.Count() > 0)
{
foreach (var patient in patients)
patientCollection.AddPatient(patient);
}

return patientCollection;
}
}
}

4. Now, pull the data from a data base and set it to the patient collection object. Now you can filter the data as you prefer using the extended methods,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ExtendedMethod
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// set patients from database or other sources
ClientManager.PatientCollection patientCollection = null;
patientCollection.GetByGender(“Male”); //set to repeater
patientCollection.SortByBirthDate(); //set to gridview
}
}
}

using System;
using System.Collections;
using System.Collections.Generic;

namespace ClientManager.Base
{
/// <summary>
/// Create a class which implements the IEnumertor and ICollection interface.
/// This class will act as a base class for all collection classe’s.
/// </summary>
/// <typeparam name=”list”></typeparam>
[Serializable]
public abstract class Extender<list>:IEnumerable<list>, ICollection
{
private List<list> baseList = new List<list>();

#region IEnumerable Members

public IEnumerator<list> GetEnumerator()
{
return baseList.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return baseList.GetEnumerator();
}

#endregion

#region ICollection Members

public bool IsSynchronized { get { return (baseList as ICollection).IsSynchronized; } }

public int Count { get { return baseList.Count; } }

public object SyncRoot { get { return (baseList as ICollection).SyncRoot; } }

public void CopyTo(Array array, int index) { (baseList as ICollection).CopyTo(array, index); }

#endregion

#region List Members

protected void Add(list item)
{
baseList.Add(item);
}

protected void RemoveAt(int index)
{
baseList.RemoveAt(index);
}

protected void Clear()
{
baseList.Clear();
}

#endregion
}
}

Advertisements

March 31, 2010 Posted by | ASP.NET | 4 Comments