Mar 19, 2013

Generics

http://stackoverflow.com/questions/692540/examples-of-usage-of-generics-in-net-c-vb-net http://www.dotnetperls.com/generic

Generics

http://www.codeproject.com/Articles/563878/Generic-Base-class

Generics

public abstract class BaseDao { public string GetConectionString() { return ""; } public abstract void Create(T t); public abstract T GetById(int id); public abstract List Search(SearchCriteria obj); } public class EmployeeDal : BaseDao { public override void Create(Employee t) { using (SqlConnection con = new SqlConnection(GetConectionString())) { } } public override Employee GetById(int id) { throw new NotImplementedException(); } public override List Search(SearchCriteria obj) { throw new NotImplementedException(); } } public class ITTicketsDal : BaseDao { public override void Create(Ticket t) { throw new NotImplementedException(); } public override Ticket GetById(int id) { using (SqlConnection con = new SqlConnection(GetConectionString())) { return null; } } public override List Search(SearchCriteria obj) { throw new NotImplementedException(); } } public class Employee { public string Name { get; set; } } public class Ticket { public string TicketId { get; set; } } public class SearchCriteria { public string FirstName { get; set; } public string LastName { get; set; } }

Singleton

The singleton pattern is a software design pattern that is used to restrict instantiation of a class to one object. This is useful when we require exactly one object of a class to perform our operations. In this pattern we ensure that the class has only one instance and we provide a global point of access to this object public class Singleton { // Static object of the Singleton class. private static volatile Singleton _instance = null; /// /// The static method to provide global access to the singleton object. /// /// Singleton object of class Singleton. public static Singleton Instance() { if (_instance == null) { lock (typeof(Singleton)) { _instance = new Singleton(); } } return _instance; } /// /// The constructor is defined private in nature to restrict access. /// private Singleton() { } }

Abstraction Vs Encapsulation

Encapsulation is hiding the implementation details which may or may not be for generic or specialized behavior(s). Abstraction is providing a generalization (say, over a set of behaviors). Encapsulation: Is hiding unwanted/un-expected/propriety implementation details from the actual users of object. e.g. List list = new List(); list.Sort(); /* Here, which sorting algorithm is used and hows its implemented is not useful to the user who wants to perform sort, that's why its hidden from the user of list. */ Abstraction: Is a way of providing generalization and hence a common way to work with objects of vast diversity. e.g. class Aeroplane : IFlyable, IFuelable, IMachine { // Aeroplane's Design says: // Aeroplane is a flying object // Aeroplane can be fueled // Aeroplane is a Machine } // But the code related to Pilot, or Driver of Aeroplane is not bothered // about Machine or Fuel. Hence, // pilot code: IFlyable flyingObj = new Aeroplane(); flyingObj.Fly(); // fighter Pilot related code IFlyable flyingObj2 = new FighterAeroplane(); flyingObj2.Fly(); // UFO related code IFlyable ufoObj = new UFO(); ufoObj.Fly(); // **All the 3 Above codes are genaralized using IFlyable, // Interface Abstraction** // Fly related code knows how to fly, irrespective of the type of // flying object they are. // Similarly, Fuel related code: // Fueling an Aeroplane IFuelable fuelableObj = new Aeroplane(); fuelableObj.FillFuel(); // Fueling a Car IFuelable fuelableObj2 = new Car(); // class Car : IFuelable { } fuelableObj2.FillFuel(); // ** Fueling code does not need know what kind of vehicle it is, so far // as it can Fill Fuel**