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 CountryService [Serializable] public class CountryService : ServiceTemplate, ICountryService { #region Private functions and declaration Cache _cache = new Cache(typeof(Country)); #endregion #region constructor public CountryService() { } #endregion #region Object private void MapObject(Country oCountry, DataReader oReader) { base.SetObjectID(oCountry, oReader.GetID("COUNTRYID")); oCountry.Code = oReader.GetString("CODE"); oCountry.Name = oReader.GetString("NAME"); oCountry.Sequence = oReader.GetInt32("SequenceNo").Value; oCountry.Status = (EnumStatus)oReader.GetInt32("Status").Value; oCountry.CreatedBy = oReader.GetID("CreatedBy"); oCountry.CreatedDate = oReader.GetDateTime("CreationDate").Value; oCountry.ModifiedBy = oReader.GetID("ModifiedBy"); oCountry.ModifiedDate = oReader.GetDateTime("ModifiedDate"); this.SetObjectState(oCountry, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader oReader) { Country oCountry = new Country(); MapObject(oCountry, oReader); return oCountry as T; } protected Country CreateObject(DataReader oReader) { Country oCountry = new Country(); MapObject(oCountry, oReader); return oCountry; } #endregion #region Service implementation public Country Get(ID id) { Country oCountry = new Country(); #region Cache Header oCountry = _cache["Get", id] as Country; if (oCountry != null) return oCountry; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(CountryDA.Get(tc, id)); if (oreader.Read()) { oCountry = 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(oCountry, "Get", id); #endregion return oCountry; } public Country Get(string sCode) { Country oCountry = new Country(); #region Cache Header oCountry = _cache["Get", sCode] as Country; if (oCountry != null) return oCountry; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(CountryDA.Get(tc, sCode)); if (oreader.Read()) { oCountry = 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(oCountry, "Get", sCode); #endregion return oCountry; } public ObjectsTemplate Get() { #region Cache Header ObjectsTemplate countries = _cache["Get"] as ObjectsTemplate; if (countries != null) return countries; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CountryDA.Get(tc)); countries = 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(countries, "Get"); #endregion return countries; } public ObjectsTemplate Get(EnumStatus status) { #region Cache Header ObjectsTemplate countries = _cache["Get"] as ObjectsTemplate; if (countries != null) return countries; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader dr = new DataReader(CountryDA.Get(tc, status)); countries = 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(countries, "Get", status); #endregion return countries; } public ID Save(Country oCountry) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oCountry.IsNew) { int id = tc.GenerateID("COUNTRY", "COUNTRYID"); base.SetObjectID(oCountry, ID.FromInteger(id)); CountryDA.Insert(tc, oCountry); } else { CountryDA.Update(tc, oCountry); } tc.End(); return oCountry.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); CountryDA.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 }