public void Delete(Contact contactToDelete)
        {
            var originalContact = Get(contactToDelete.Id);
            _entities.DeleteObject(originalContact);
            _entities.SaveChanges();

        }
        public Contact Create(Contact contactToCreate)
        {
            _entities.AddToContactSet(contactToCreate);
            _entities.SaveChanges();
            return contactToCreate;

        }
        public ContactViewModel(Contact contact)
        {
            //creating the elements for contact status and contact priority drop down lists
            var statuses = Enum.GetNames(typeof(ContactStatus));

            Contact = contact;
            Statuses = new SelectList(statuses, Contact.Status);
        }
        public Contact Update(Contact contactToUpdate)
        {
            var originalContact = Get(contactToUpdate.Id);
            _entities.ApplyCurrentValues(originalContact.EntityKey.EntitySetName, contactToUpdate);
            _entities.SaveChanges();
            return contactToUpdate;

        }
        public bool EditContact(Contact contactToEdit)
        {
            // Validation logic
            if (!ValidateContact(contactToEdit))
                return false;

            // Database logic
            try
            {
                _repository.Update(contactToEdit);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool CreateContact(Contact contactToCreate)
        {
            // Validation logic
            if (!ValidateContact(contactToCreate))
                return false;

            // Database logic
            try
            {
                _repository.Create(contactToCreate);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public bool ValidateContact(Contact contactToValidate)
        {
            //convert null values to empty strings
            contactToValidate.FirstName = contactToValidate.FirstName ?? "";
            contactToValidate.LastName = contactToValidate.LastName ?? "";
            contactToValidate.Address = contactToValidate.Address ?? "";
            contactToValidate.Email = contactToValidate.Email ?? "";

            if (contactToValidate.FirstName.Trim().Length == 0)
                _validationDictionary.AddError("Contact.FirstName", "First name is required.");
            if (contactToValidate.LastName.Trim().Length == 0)
                _validationDictionary.AddError("Contact.LastName", "Last name is required.");
            if (contactToValidate.Address.Trim().Length == 0)
                _validationDictionary.AddError("Contact.Address", "Address is required.");
            if (contactToValidate.Email.Length > 0 && !Regex.IsMatch(contactToValidate.Email, @"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$"))
                _validationDictionary.AddError("Contact.Email", "Invalid email address.");

            return _validationDictionary.IsValid;
        }
 public ActionResult Delete(Contact contact)
 {
     if (!_contactService.DeleteContact(contact))
         return View(contact);
     return RedirectToAction("Index");
 }
 /// <summary>
 /// Create a new Contact object.
 /// </summary>
 /// <param name="id">Initial value of the Id property.</param>
 /// <param name="lastName">Initial value of the LastName property.</param>
 /// <param name="firstName">Initial value of the FirstName property.</param>
 /// <param name="address">Initial value of the Address property.</param>
 /// <param name="status">Initial value of the Status property.</param>
 /// <param name="email">Initial value of the Email property.</param>
 public static Contact CreateContact(global::System.Int32 id, global::System.String lastName, global::System.String firstName, global::System.String address, global::System.String status, global::System.String email)
 {
     Contact contact = new Contact();
     contact.Id = id;
     contact.LastName = lastName;
     contact.FirstName = firstName;
     contact.Address = address;
     contact.Status = status;
     contact.Email = email;
     return contact;
 }
 /// <summary>
 /// Deprecated Method for adding a new object to the ContactSet EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToContactSet(Contact contact)
 {
     base.AddObject("ContactSet", contact);
 }
 public bool DeleteContact(Contact contactToDelete)
 {
     try
     {
         _repository.Delete(contactToDelete);
     }
     catch
     {
         return false;
     }
     return true;
 }