Abstract Class Implementing Interface

Abstract Class Implementing Interface.

Recenlty I took a technically test and I have write an employee class for an aspx page that will
use an abstract class that implements an Interface.

What is abstract class ?
Abstract class is a base class or a parent class. Abstract classes can have empty abstract methods
or it can have implemented methods which can be overridden by child classes.

What are interfaces?
Interface is a contract class with empty methods, properties and functions.
Any class which implements the interface has to compulsory implement all the
empty methods, functions and properties of the interface.

Interface

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

namespace Employee.Interface
{
public interface IEmployee
{
String Name
{
get;
set;
}

String DateJoin
{
get;
set;
}

String Department
{
get;
set;
}

bool Save(String mName, String mDate, String mDepartment);
}
}

Abstract Class


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Employee.Interface;

namespace Employee.Abstract
{
public abstract class AEmployee:IEmployee
{
public abstract String Name
{
get;
set;
}

public abstract String DateJoin
{
get;
set;
}

public abstract String Department
{
get;
set;
}

public abstract bool Save(String mName, String mDate, String mDepartment);

}
}

Employee Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Employee.Abstract;

namespace Employee
{
public class Employee : AEmployee
{
private String _Name = String.Empty;
private String _DateJoin = String.Empty;
private String _Department = String.Empty;

public override String Name
{
get
{

return _Name;
}
set
{
_Name = value;
}
}

public override String DateJoin
{
get
{
return _DateJoin;
}
set
{
_DateJoin = value;
}
}

public override String Department
{
get
{
return _Department;
}
set
{
_Department = value;
}
}

public override bool Save(String mName, String mDate, String mDepartment)
{
bool success = false;
try
{
_Name = mName;
_DateJoin = mDate;
_Department = mDepartment;

//Code to save
//
success = true;
}
catch (Exception)
{
throw;
}
return success;
}
}
}

Ayschronous Data Loading

This code improves data loading for large database, very useful for windows applications which do not implement paging ffor data grids.

private DataTable dt = null;

private void LoadGrid(object sender, DoWorkEventArgs e)
{
dt = ...;
}

private void WorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
dgView.DataSource = dt;
dgView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
}

private void LoadinBackground()
{
System.ComponentModel.BackgroundWorker bgWrkr = new System.ComponentModel.BackgroundWorker();
bgWrkr.DoWork += new System.ComponentModel.DoWorkEventHandler(LoadGrid);
bgWrkr.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(WorkerCompleted);
bgWrkr.RunWorkerAsync();
}

C#Windows forms Caching

When developing C# windows application, we tend to reuse common datasource in serveral forms fo controls like combo bo, listbox. Caching is a good strategy reduce the round trip to database. I cache most of all my commonly used datasets improve loading time.

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

namespace Cache
{
public class Cache
{
static Dictionary _cache = new Dictionary();

public Cache() { }

public static bool AddToCache(string key, object obj)
{
try
{
_cache.Add(key, obj);
}
catch (ArgumentException)
{
return false;
}
return true;
}

public static object GetFromCache(string key)
{
object obj = null;
try
{
if (_cache.ContainsKey(key))
_cache.TryGetValue(key, out obj);
}
catch
{
}
return obj;
}

public static void ClearCache()
{
_cache.Clear();
}

}
}