/// <summary> /// map the dao to the entity /// </summary> /// <param name="ssdao"></param> /// <returns></returns> public Tech MapToEntity(TechDAO tdao) { Tech t = null; Tech fromDB = null; //use automapper to map matching properties var mapper = TechMapper.CreateMapper(); if (tdao != null) { t = mapper.Map <Tech>(tdao); //get original object from db if (!string.IsNullOrWhiteSpace(tdao.Name)) { fromDB = db.Tech.Where(m => m.Email.Equals(tdao.Email)).FirstOrDefault(); } //if db object exist then use existing object and map properties sent from dao if (fromDB != null) { t = fromDB; if (!string.IsNullOrWhiteSpace(tdao.Email)) { t.Email = tdao.Email; } } //if db object does not exist use automapper version of object and set active to true else { t.IsActive = true; } } return(t); }
/// <summary> /// map the entity to the dao /// </summary> /// <param name="s"></param> /// <returns></returns> public TechDAO MapToDao(Tech t) { var mapper = TechMapper.CreateMapper(); if (t != null) { TechDAO tdao = mapper.Map <TechDAO>(t); return(tdao); } else { return(new TechDAO()); } }
public bool UpdateTech(string oldId, TechDAO tdao) { try { if (!string.IsNullOrWhiteSpace(oldId) && tdao != null) { Tech ns = mapper.MapToEntity(tdao); Tech old = ef.GetTechs().FirstOrDefault(a => a.TechID.Equals(oldId)); ns.TechID = old.TechID; return(ef.UpdateTech(old, ns)); } return(false); } catch (Exception e) { return(false); } }
/// <summary> /// delete tech /// </summary> /// <param name="tdao"></param> /// <returns></returns> public bool DeleteTech(TechDAO tdao) { try { if (tdao != null) { Tech t = mapper.MapToEntity(tdao); var toDelete = ef.GetTechs().FirstOrDefault((m => m.TechID == t.TechID)); return(ef.DeleteTech(toDelete)); } else { return(false); } } catch (Exception e) { throw; } }
/// <summary> /// insert tech /// </summary> /// <param name="t"></param> /// <returns>true if successful</returns> public bool InsertTech(TechDAO t) { try { if (t != null) { //map it to EF object var itm = mapper.MapToEntity(t); db.Tech.Add(itm); return(db.SaveChanges() > 0); } else { return(false); } } catch (Exception e) { throw; } }