using System; using System.Data; using System.Linq; using Ease.CoreV35; using Ease.CoreV35.Model; using Ease.CoreV35.DataAccess; using System.Collections.Generic; using Payroll.BO; using Ease.CoreV35.Caching; namespace Payroll.Service { #region class District Service [Serializable] public class DistrictService : ServiceTemplate, IDistrictService { #region Private functions and declaration Cache _cache = new Cache(typeof(District)); #endregion #region constructor public DistrictService() { } #endregion #region object private void MapObject(District oDistrict, DataReader oReader) { base.SetObjectID(oDistrict, oReader.GetID("DISTRICTID")); oDistrict.Code = oReader.GetString("CODE"); oDistrict.Name = oReader.GetString("NAME"); oDistrict.DivisionID = oReader.GetID("DIVISIONID"); oDistrict.Sequence = oReader.GetInt32("SequenceNo").Value; oDistrict.Status = (EnumStatus)oReader.GetInt32("Status").Value; oDistrict.CreatedBy = oReader.GetID("CreatedBy"); oDistrict.CreatedDate = oReader.GetDateTime("CreationDate").Value; oDistrict.ModifiedBy = oReader.GetID("ModifiedBy"); oDistrict.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oDistrict, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { District oDistrict = new District(); MapObject(oDistrict, oReader); return oDistrict as T; } protected District CreateObject(DataReader oReader) { District oDistrict = new District(); MapObject(oDistrict, oReader); return oDistrict; } #endregion #region Service implementation public District Get(ID id) { District oDistrict = new District(); #region Cache Header oDistrict = _cache["Get", id] as District; if (oDistrict != null) return oDistrict; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(DistrictDA.Get(tc, id)); if (oreader.Read()) { oDistrict = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } #region Cache Footer _cache.Add(oDistrict, "Get", id); #endregion return oDistrict; } public District Get(string sCode) { District oDistrict = new District(); #region Cache Header oDistrict = _cache["Get", sCode] as District; if (oDistrict != null) return oDistrict; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(DistrictDA.Get(tc, sCode)); if (oreader.Read()) { oDistrict = this.CreateObject(oreader); } oreader.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } #region Cache Footer _cache.Add(oDistrict, "Get", sCode); #endregion return oDistrict; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate districts = _cache["Get"] as ObjectsTemplate; if (districts != null) return districts; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DistrictDA.Get(tc)); districts = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } #region Cache Footer _cache.Add(districts, "Get"); #endregion return districts; } public ObjectsTemplate Get(EnumStatus status) { #region Cache Header ObjectsTemplate districts = _cache["Get"] as ObjectsTemplate; if (districts != null) return districts; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(DistrictDA.Get(tc, status)); districts = this.CreateObjects(dr); dr.Close(); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } #region Cache Footer _cache.Add(districts, "Get", status); #endregion return districts; } public ID Save(District oDistrict) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oDistrict.IsNew) { int id = tc.GenerateID("DISTRICT", "DISTRICTID"); base.SetObjectID(oDistrict, ID.FromInteger(id)); DistrictDA.Insert(tc, oDistrict); } else { DistrictDA.Update(tc, oDistrict); } tc.End(); return oDistrict.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } public void Delete(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); DistrictDA.Delete(tc, id); tc.End(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } } #endregion } #endregion }