using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ease.CoreV35.Caching; using Ease.CoreV35.DataAccess; using Ease.CoreV35.Model; using Payroll.BO; namespace Payroll.Service { public class VendorService : ServiceTemplate, IVendorService { #region Cache Cache _cache = new Cache(typeof(Vendor)); #endregion #region Map Functions private void MapObject(Vendor oVendor, DataReader oReader) { base.SetObjectID(oVendor, oReader.GetID("VendorID")); oVendor.Name = oReader.GetString("Name"); oVendor.Address = oReader.GetString("Address"); oVendor.Balance = oReader.GetDouble("Balance").GetValueOrDefault(0); oVendor.ContactPerson = oReader.GetString("ContactPerson"); oVendor.EmailAddress = oReader.GetString("EmailAddress"); oVendor.Mobile = oReader.GetString("Mobile"); oVendor.Telephone = oReader.GetString("Telephone"); oVendor.VendorType = (EnumVendorType)oReader.GetUInt32("VendorType").GetValueOrDefault(); oVendor.Status = (EnumStatus) oReader.GetUInt32("Status").GetValueOrDefault(); oVendor.CreatedBy = oReader.GetID("CreatedBy"); oVendor.CreatedDate = oReader.GetDateTime("CreatedDate").GetValueOrDefault(DateTime.MinValue); oVendor.ModifiedBy = oReader.GetID("ModifiedBy"); oVendor.ModifiedDate = oReader.GetDateTime("ModifiedDate").GetValueOrDefault(DateTime.MinValue); this.SetObjectState(oVendor, Ease.CoreV35.ObjectState.Saved); } protected override T CreateObject(DataReader dr) { Vendor oVendor = new Vendor(); MapObject(oVendor, dr); return oVendor as T; } #endregion #region Implementation of IVendorService public Vendor Get(ID id) { Vendor oVendor = new Vendor(); #region Cache Header oVendor = _cache["Get", id] as Vendor; if (oVendor != null) return oVendor; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, id)); if (oreader.Read()) { oVendor = CreateObject(oreader); } oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(oVendor, "Get", id); #endregion return oVendor; } public ObjectsTemplate Get() { ObjectsTemplate vendors = new ObjectsTemplate(); #region Cache Header vendors = _cache["Get"] as ObjectsTemplate; if (vendors != null) return vendors; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(vendors, "Get"); #endregion return vendors; } public ObjectsTemplate Get(EnumVendorType vendorType) { ObjectsTemplate vendors = new ObjectsTemplate(); #region Cache Header vendors = _cache["Get"] as ObjectsTemplate; if (vendors != null) return vendors; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc,vendorType)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(vendors, "Get"); #endregion return vendors; } public ObjectsTemplate Get(EnumStatus status) { ObjectsTemplate vendors = new ObjectsTemplate(); #region Cache Header vendors = _cache["Get"] as ObjectsTemplate; if (vendors != null) return vendors; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, status)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(vendors, "Get"); #endregion return vendors; } public ObjectsTemplate Get(string subStr) { ObjectsTemplate vendors = new ObjectsTemplate(); #region Cache Header vendors = _cache["Get"] as ObjectsTemplate; if (vendors != null) return vendors; #endregion TransactionContext tc = null; try { tc = TransactionContext.Begin(); DataReader oreader = new DataReader(VendorDA.Get(tc, subStr)); vendors = CreateObjects(oreader); oreader.Close(); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } #region Cache Footer _cache.Add(vendors, "Get"); #endregion return vendors; } public ID Save(Vendor oVendor) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); if (oVendor.IsNew) { int id = tc.GenerateID("Vendor", "VendorID"); base.SetObjectID(oVendor, ID.FromInteger(id)); VendorDA.Insert(tc, oVendor); } else { VendorDA.Update(tc, oVendor); } return oVendor.ID; } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } public void Delete(ID id) { TransactionContext tc = null; try { tc = TransactionContext.Begin(true); VendorDA.Delete(tc, id); } catch (Exception e) { #region Handle Exception if (tc != null) tc.HandleError(); ExceptionLog.Write(e); throw new ServiceException(e.Message, e); #endregion } finally { tc.End(); } } #endregion } }