public void GivenExceptionWithMultipeMembers_WhenAddModelErrors_ThenModelStateErrorAddedForEachMember()
        {
            string[] expected = new string[] { "Property1", "Property2", "AnotherProperty" };
            ModelStateDictionary target = new ModelStateDictionary();
            ValidationException exception = new ValidationException(new ValidationResult(null, expected), null, null);

            target.AddModelErrors(exception);

            CollectionAssert.AreEqual(expected, target.Keys.ToList());
        }
        public void GivenExceptionCreatedWithoutValidationResult_WhenAddModelErrors_ThenModelStateErrorAddedWithMessage()
        {
            string expected = "even without a validation result this should be the message";
            ModelStateDictionary target = new ModelStateDictionary();
            ValidationException exception = new ValidationException(expected);

            target.AddModelErrors(exception);

            Assert.AreEqual(expected, target.Values.Single().Errors.Single().ErrorMessage);
        }
Esempio n. 3
0
        public void SaveAlbum(AlbumProp albumProp)
        {
            var validationContext = new ValidationContext(albumProp);
            var validationResults = new List<ValidationResult>();
            if (!Validator.TryValidateObject(albumProp, validationContext, validationResults, true))
            {
                // Uppfyller inte objektet affärsreglerna kastas ett undantag med
                // ett allmänt felmeddelande samt en referens till samlingen med
                // resultat av valideringen.
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            //Customer-objektet sparas antingen genom att en ny post
            //skapas eller genom att en befintlig post uppdateras.
            if (albumProp.AlbumID == 0) // Ny post om CustomerId är 0!
            {
                AlbumDAL.InsertAlbum(albumProp);
            }
            else
            {
                AlbumDAL.InsertAlbum(albumProp);

            }
        }
        public void GivenExceptionNoMember_WhenAddModelErrors_ThenModelStateErrorAddedWithEmptyKey()
        {
            ModelStateDictionary target = new ModelStateDictionary();
            ValidationException exception = new ValidationException(new ValidationResult(null, null), null, null);

            target.AddModelErrors(exception);

            Assert.AreEqual(string.Empty, target.Keys.Single());
        }
        public void GivenExceptionNoMember_WhenAddModelErrors_ThenModelStateErrorAddedWithMessage()
        {
            string expected = "here is the error message";
            ModelStateDictionary target = new ModelStateDictionary();
            ValidationException exception = new ValidationException(new ValidationResult(expected, null), null, null);

            target.AddModelErrors(exception);

            Assert.AreEqual(expected, target.Values.Single().Errors.Single().ErrorMessage);
        }
Esempio n. 6
0
        /* Validation */

        public void Validate(Object validationObject)
        {
            ICollection<ValidationResult> validationResults;
            if (!validationObject.Validate(out validationResults))
            {
                ValidationResult[] validationException = new ValidationResult[1];
                validationResults.CopyTo(validationException, 0);
                var ex = new ValidationException(validationException[0].ErrorMessage);
                throw ex;
            }
        }
 public void createMedlem(Medlem medlem)
 {
     ICollection<ValidationResult> validationResult;
     if (!medlem.Validate(out validationResult))
     {
         var vx = new ValidationException("Objektet klarade inte valideringen.");
         vx.Data.Add("validationResult", validationResult);
         throw vx;
     }
     Medlem.AddMedlem(medlem);
 }
        public void SavePhysicalRecord(PhysicalRecord physrecord)
        {
            ICollection<ValidationResult> validationResults;
            if(!physrecord.Validate(out validationResults)){

             var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

                PhysicalRecordDAL.InsertPhysicalRecord(physrecord);
        }
 public void AddPatient(Patient patient)
 {
     var validationContext = new ValidationContext(patient);
     var validationResults = new List<ValidationResult>();
     if (!Validator.TryValidateObject(patient, validationContext, validationResults, true))
     {
         var ex = new ValidationException("Objektet klarade inte valideringen.");
         ex.Data.Add("ValidationResults", validationResults);
         throw ex;
     }
     PatientDAL.AddPatient(patient);
 }
 public void AddDoctor(Doctor doctor)
 {
     var validationContext = new ValidationContext(doctor);
     var validationResults = new List<ValidationResult>();
     if (!Validator.TryValidateObject(doctor, validationContext, validationResults, true))
     {
         var ex = new ValidationException("Objektet klarade inte valideringen.");
         ex.Data.Add("ValidationResults", validationResults);
         throw ex;
     }
     DoctorDAL.AddDoctor(doctor);
 }
        public void GivenExceptionWithMultipeMembers_WhenAddModelErrors_ThenErrorMessageAddedForEachMember()
        {
            string expected = "this error message should appear for each property";
            string[] properties = new string[] { "Property1", "Property2", "AnotherProperty" };
            ModelStateDictionary target = new ModelStateDictionary();
            ValidationException exception = new ValidationException(new ValidationResult(expected, properties), null, null);

            target.AddModelErrors(exception);

            string[] actual = target.Values.Select(v => v.Errors.Single().ErrorMessage).ToArray();
            Assert.AreEqual(properties.Length, actual.Length);
            Assert.AreEqual(expected, actual.Distinct().Single());
        }
Esempio n. 12
0
        /// <summary>
        /// Test la validité d'une dépendance et ses enfants
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="ex">Retourne la première exception trouvée</param>
        /// <returns>True, si l'ojet et les objets enfants sont valides. Sinon, False.</returns>
        public static bool IsValid(DependencyObject obj, out Exception ex, bool ignoreDisabledElements)
        {
            List<DependencyObject> list = new List<DependencyObject>();
            EnumChildren(obj, list, ignoreDisabledElements);
            for (int i = 0; i < list.Count; i++)
            {
                if (Validation.GetHasError(list[i]))
                {
                    //obtient la premiere erreur
                    ValidationError err = Validation.GetErrors(list[i])[0];
            /*
                    Console.WriteLine("GetErrors.count =" + Validation.GetErrors(list[i]).Count);
                    Console.WriteLine("GetErrors.ErrorContent =" + err.ErrorContent);
                    Console.WriteLine("GetErrors.BindingInError =" + err.BindingInError);
                    Console.WriteLine("GetErrors.RuleInError =" + err.RuleInError);
                    Console.WriteLine("Exception =" + err.Exception);
            */
                    // erreur venant d'une exception ?
                    if (err.Exception != null)
                    {
                        ex = err.Exception;
                        return false;
                    }

                    // message d'erreur ?
                    if (err.ErrorContent != null)
                    {
                        ex = new ValidationException(err.ErrorContent.ToString());
                        return false;
                    }

                    // autre ?
                    ex = new ValidationException("Erreur de validation inconnue");
                    return false;
                }
            }

            if (Validation.GetHasError(obj))
            {
                ex = Validation.GetErrors(obj)[0].Exception;
                Console.WriteLine("IsValid error :" + Validation.GetErrors(obj)[0].Exception);
                return false;
            }

            ex = null;
            return true;
        }
        public void SaveBookedRoom(BookedRoom bookedRoom)
        {
            // Uppfyller inte objektet affärsreglerna...
            ICollection<ValidationResult> validationResults;
            if (!bookedRoom.Validate(out validationResults)) // Använder "extension method" för valideringen!
            {                                              // Klassen finns under App_Infrastructure.
                // ...kastas ett undantag med ett allmänt felmeddelande samt en referens
                // till samlingen med resultat av valideringen.
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            // BookedRoom-objektet sparas genom att en ny post
            // skapas
            BookedRoomDAL.InsertBookedRoom(bookedRoom);
        }
        // INSERT & UPDATE
        public void SaveContact(Contact contact)
        {
            var validationContext = new ValidationContext(contact);
            var validationResults = new List<ValidationResult>();

            // Check if data passes objectvalidation
            if (!Validator.TryValidateObject(contact, validationContext, validationResults, true))
            {
                var ex = new ValidationException("Inmatade värden uppfyllder ej valideringskraven");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if(contact.ContactID == 0)
                ContactDAL.InsertContact(contact);
            else
                ContactDAL.UpdateContact(contact);
        }
Esempio n. 15
0
        // Creates or updates a note.
        public void CreateNoteAndCollection(QnoteCollectionID qnoteCollectionID)
        {
            ICollection<ValidationResult> validationResults;
            if (!qnoteCollectionID.Validate(out validationResults))
            {
                var ex = new ValidationException("The Qnote object did not pass the data validation!");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (qnoteCollectionID.NoteID == 0)
            {
                NoteDAL.CreateNoteAndCollection(qnoteCollectionID);
            }
            else
            {
                NoteDAL.UpdateNoteAndCollection(qnoteCollectionID);
            }
        }
Esempio n. 16
0
        // Creates or updates a collection(name).
        public void CreateCollectionName(CollectionName collectionName)
        {
            ICollection<ValidationResult> validationResults;
            if (!collectionName.Validate(out validationResults))
            {
                var ex = new ValidationException("The CollectionName object did not pass the data validation!");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (collectionName.CollectionNameID == 0)
            {
                CollectionNameDAL.CreateCollectionName(collectionName);
            }
            else
            {
                CollectionNameDAL.UpdateCollectionName(collectionName);
            }
        }
        public void SaveContact(Contact contact)
        {
            ICollection<ValidationResult> validationResults;
            if (!contact.Validate(out validationResults))
            {
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (contact.ContactID == 0)
            {
                ContactDAL.InsertContact(contact);
            }
            else
            {
                ContactDAL.UpdateContact(contact);
            }
        }
        public void SaveContact(Contact contact)
        {
            ICollection<ValidationResult> validationResults;
            if (!contact.Validate(out validationResults))
            {
                var ex = new ValidationException("The object did not pass the validation.");
                ex.Data.Add("ValidationResult", validationResults);
                throw ex;
            }

            if (contact.ContactId == 0)
            {
                ContactDAL.InsertContact(contact);
            }
            else
            {
                ContactDAL.UpdateContact(contact);
            }
        }
 public void SaveContact(Contact contact)
 {
     var validationContext = new ValidationContext(contact);
     var validationResults = new List<ValidationResult>();
     if (!Validator.TryValidateObject(contact, validationContext, validationResults, true))
     {
         var ex = new ValidationException("Objektet klarade inte valideringen.");
         ex.Data.Add("ValidationResults", validationResults);
         throw ex;
     }
     if (contact.ContactID == 0)
     {
         ContactDAL.InsertContact(contact);
     }
     else
     {
         ContactDAL.UpdateContact(contact);
     }
 }
Esempio n. 20
0
        public void InsertSupplier(Supplier supplier)
        {
            //validering
            ICollection<ValidationResult> validationResult;
            if (!supplier.Validate(out validationResult))
            {
                var ex = new ValidationException("Leverantören klarade inte valideringen");
                ex.Data.Add("ValidationResult", validationResult);
                throw ex;
            }

            if(supplier.SupplierID==0)
            {
                SupplierDAL.InsertSupplier(supplier);
            }
            else
            {
               SupplierDAL.UpdateSupplier(supplier);
            }
        }
        public void SaveQuestion(Question question)
        {
            // Validering
            ICollection<ValidationResult> validationResults;
            if (!question.Validate(out validationResults))
            {
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (question.QuestionID == 0)
            {
                QuestionDAL.AddQuestion(question);
            }
            else
            {
                QuestionDAL.EditQuestion(question);
            }
        }
        // Uppdaterar ändringar för vald åtgärd i databasen
        public void SaveAction(Action action)
        {
            // Validering
            ICollection<ValidationResult> validationResults;
            if (!action.Validate(out validationResults))
            {
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (action.ÅtgID == 0)
            {
                ActionDAL.InsertAction(action);
            }
            else
            {
                ActionDAL.UpdateAction(action);
            }
        }
        // spara
        public void SaveContact(Contact contact)
        {
            ICollection<ValidationResult> validres;

            if (!contact.Validate(out validres))
            {
                var ex = new ValidationException("Det gick inte att spara kunden!!");
                ex.Data.Add("ValidationResults", validres);

                throw ex;
            }

            if (contact.ContactID == 0)
            {
                ContactDAL.InsertContact(contact);
            }
            else
            {
                ContactDAL.UpdateContact(contact);
            }
        }
        //Saves one employee
        public void SaveEmployee(Employee employee)
        {
            //Valididation in BLL
            ICollection<ValidationResult> validationResults;
            if (!employee.Validate(out validationResults))
            {
                var ex = new ValidationException("Objektet klarade inte validerinen");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }


            if (employee.EmpID == 0)
            {
                EmployeeDAL.InsertEmployee(employee);
            }
            else
            {
                EmployeeDAL.UpdateEmployee(employee);
            }
        }
Esempio n. 25
0
        public void SaveContact(Contact contact)
        {
            // Validera affärsreglerna
            var validationContext = new ValidationContext(contact);
            var validationResults = new List<ValidationResult>();
            if (!Validator.TryValidateObject(contact, validationContext, validationResults, true))
            {
                var ex = new ValidationException("Kunden kunde inte sparas.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (contact.ContactID == 0)
            {
                ContactDAL.InsertContact(contact);
            }
            else
            {
                ContactDAL.UpdateContact(contact);
            }
        }
Esempio n. 26
0
        //spara uppdatera kontakt
        public void Savecontact(Contact contact)
        {
            //validering -valdiationextension
            ICollection<ValidationResult> validationResult;
            if (!contact.Validate(out validationResult))
            {
                var ex = new ValidationException("Kontakten klarade inte valideringen");
                ex.Data.Add("ValidationREsult", validationResult);
                throw ex;
            }

            //spara
            if (contact.ContactID == 0)
            {
                ContactDAL.InsertContact(contact);
            }
            //uppdatera
            else
            {
                ContactDAL.UpdateContact(contact);
            }
        }
Esempio n. 27
0
        //spara /uppdatera
        public void InsertCustomer(Customer customer)
        {
            //validering
            ICollection<ValidationResult> validationResult;
            if (!customer.Validate(out validationResult))
            {
                var ex = new ValidationException("Kunden klarade inte valideringen");
                ex.Data.Add("ValidationResult", validationResult);
                throw ex;
            }

            //spara kund
            if (customer.CustomerID == 0)
            {
                CustomerDAL.InsertCustomer(customer);
            }
            //uppdatera kund
            else
            {
                CustomerDAL.UpdateCustomer(customer);
            }
        }
 public static void AddModelErrors(this ModelStateDictionary modelState, ValidationException exception)
 {
     if (modelState == null)
     {
         throw new ArgumentNullException("modelState");
     }
     if (exception == null)
     {
         throw new ArgumentNullException("exception");
     }
     if (exception.ValidationResult.MemberNames.Any())
     {
         foreach (string property in exception.ValidationResult.MemberNames)
         {
             modelState.AddModelError(property, exception.ValidationResult.ErrorMessage);
         }
     }
     else
     {
         modelState.AddModelError(string.Empty, exception.Message);
     }
 }
        public void SaveContact(Contact contact)
        {
            // VALIDERING I AFFÄRSLOGIKLAGRET
            ICollection<ValidationResult> validationResults;
            if (!contact.Validate(out validationResults)) // extension method för valideringen i App_Infrastructure
            {
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (contact.ContactID == 0) // Ny post om CustomerId är 0!
            {
                ContactDAL.InsertContact(contact); // InsertCustomer UpdateCustomer
            }
            else
            {
                ContactDAL.UpdateContact(contact);
            }

            //throw new NotImplementedException();
        }
Esempio n. 30
0
        public void SaveContact(CustomerProp customerProp)
        {
            var validationContext = new ValidationContext(customerProp);
            var validationResults = new List<ValidationResult>();
            if (!Validator.TryValidateObject(customerProp, validationContext, validationResults, true))
            {
                // Uppfyller inte objektet affärsreglerna kastas ett undantag med
                // ett allmänt felmeddelande samt en referens till samlingen med
                // resultat av valideringen.
                var ex = new ValidationException("Objektet klarade inte valideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            // //Uppfyller inte objektet affärsreglerna...
            //ICollection<ValidationResult> validationResults;
            //if (!contact.Validate(out validationResults))
            //    // Använder "extension method" för valideringen!
            //{   // Klassen finns under App_Infrastructure.
            //    // ...kastas ett undantag med ett allmänt felmeddelande samt en referens
            //    // till samlingen med resultat av valideringen.
            //    var ex = new ValidationException("Objektet klarade inte valideringen.");
            //    ex.Data.Add("ValidationResults", validationResults);
            //    throw ex;
            //}

             //Customer-objektet sparas antingen genom att en ny post
             //skapas eller genom att en befintlig post uppdateras.
            if (customerProp.CustomerId == 0) // Ny post om CustomerId är 0!
            {
                CustomerDAL.InsertContact(customerProp);
            }
            else
            {
                CustomerDAL.UpdateContact(customerProp);

            }
        }