using System; using System.Collections.Generic; using System.Data; using System.Text; using System.Web; using SubSonic; namespace DAL { public class ControllerBase where ItemType : RepositoryRecord, new() where ListType : RepositoryList, new() { public static void Delete(int id) { DB.Delete(id); } public static ItemType Get(object id) { if (id == null) return default(ItemType); ItemType item = DB.Get(id); return item; } public static ItemType GetFirst(Query q) { ListType coll = List(q); if (coll.Count == 0) { return default(ItemType); } else { return coll[0]; } } public static ItemType GetFirst(SqlQuery q) { return q.ExecuteSingle(); } public static ItemType GetFirst(StoredProcedure sproc) { ListType coll = List(sproc); if (coll.Count == 0) { return default(ItemType); } else { return coll[0]; } } public static ListType List() { return DB.Select().From().ExecuteAsCollection(); } public static ListType List(Query query) { ListType coll = GetCollectionFromReader(query.ExecuteReader()); return coll; } public static ListType List(SqlQuery q) { ListType coll = q.ExecuteAsCollection(); return coll; } public static ListType List(StoredProcedure s) { ListType coll = GetCollectionFromReader(s.GetReader()); return coll; } public static void Save(ItemType item, string username) { DB.Save(item, username); } protected static ListType GetCollectionFromReader(IDataReader rdr) { ListType coll = new ListType(); coll.LoadAndCloseReader(rdr); return coll; } } }