public void RulesExceptionUseCaseModelFoo() { var modelFoo = new ModelFoo { ModelFooId = 1, Name = "Test", ModelBar = new ModelBar { ModelBarId = 1, Name = "Test" } }; var rulesException = new RulesException <ModelFoo>(); // This will apply to the model as whole and should be used for // scenarios where there are multiple issues against another object. rulesException.ErrorForModel("There is already a ModelFoo with this Id and Name"); Assert.Single(rulesException.Errors); // Should be used for property issues. rulesException.ErrorFor(x => x.Name, "The Name is not Unique"); rulesException.ErrorFor(x => x.ModelBar.Name, "The Name is not Unique"); rulesException.ErrorFor("Name", "Another Error"); var errorMessage = rulesException.ToString(); Assert.Equal(4, rulesException.Errors.Count()); }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="visa"></param> /// <param name="isCreate"></param> private void _validate(VisaDetail visa, bool isCreate) { var errors = new RulesException <VisaDetail>(); if (string.IsNullOrWhiteSpace(visa.StayReasonDetail)) { errors.ErrorFor(x => x.Code, "เลือกข้อมูลระยะเวลาให้ถูกต้องด้วยค่ะ"); } if (visa.Code != "") { int visaId = 0; if (!isCreate) { visaId = visa.Id; } var oldCode = (from p in entities.VisaDetails where (p.Id != visaId) && (p.Code == visa.Code) && (p.RequestDate.Year == visa.RequestDate.Year) && (!p.IsCancel) select p.Id).Count(); if (oldCode > 0) { errors.ErrorFor(x => x.Code, "Code ที่กรอก ซ้ำกับรายการอื่นในปีเดียวกัน"); } } if (errors.Errors.Any()) { throw errors; } }
private static void EnsureValidForCreation(Appointment appt) { var errors = new RulesException <Appointment>(); if (string.IsNullOrEmpty(appt.ClientName)) { errors.ErrorFor(x => x.ClientName, "Please specify a name"); } if (appt.AppointmentDate < DateTime.Now.Date) { errors.ErrorFor(x => x.AppointmentDate, "Can't book in the past"); } else if ((appt.AppointmentDate - DateTime.Now.Date).TotalDays > 7) { errors.ErrorFor(x => x.AppointmentDate, "Can't book more than a week in advance"); } if (appt.ClientName == "Steve" && appt.AppointmentDate.DayOfWeek == DayOfWeek.Saturday) { errors.ErrorForModel("Steve can't book on weekends"); } if (errors.Errors.Any()) { throw errors; } }
public override void Validate() { var errors = new RulesException <VisualEditorPlugin>(); base.Validate(errors); if (!string.IsNullOrEmpty(Url) && !UrlHelpers.IsValidWebFolderUrl(Url)) { errors.ErrorFor(n => Url, VisualEditorStrings.UrlPrefixInvalidFormat); } var duplicateCurrentNames = VeCommands.GroupBy(c => c.Name).Where(g => g.Count() > 1).Select(x => x.Key).ToArray(); var duplicateCurrentAliases = VeCommands.GroupBy(c => c.Alias).Where(g => g.Count() > 1).Select(x => x.Key).ToArray(); if (VeCommands.Count == 0) { errors.ErrorForModel(VisualEditorStrings.CommandsRequired); } else { var veCommandsArray = VeCommands.ToArray(); for (var i = 0; i < veCommandsArray.Length; i++) { ValidateVeCommand(veCommandsArray[i], errors, i + 1, duplicateCurrentNames, duplicateCurrentAliases); } } if (!errors.IsEmpty) { throw errors; } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="reentry"></param> /// <param name="isCreate"></param> private void _validate(ReEntry reentry, bool isCreate) { var errors = new RulesException <ReEntry>(); if (reentry.Code != "") { int reentryId = 0; if (!isCreate) { reentryId = reentry.Id; } var oldCode = (from p in entities.ReEntrys where (p.Id != reentryId) && (p.Code == reentry.Code) && (p.RequestDate.Year == reentry.RequestDate.Year) && (!p.IsCancel) select p.Id).Count(); if (oldCode > 0) { errors.ErrorFor(x => x.Code, "Code ที่กรอก ซ้ำกับรายการอื่นในปีเดียวกัน"); } } if (errors.Errors.Any()) { throw errors; } }
/// <summary> /// สร้าง code /// </summary> /// <returns>ถ้าแปลงเป็น Int ได้จะ return ตัวเลขให้ แต่ถ้าไม่ได้ ก็ส่ง error ไปแจ้ง</returns> private string _getNewCode(int refTypeId, string defaultCode) { if (defaultCode != null && defaultCode != "") { return(defaultCode); } string result = (from p in FindAll(refTypeId) select p.Code).DefaultIfEmpty("000").Max(); int iResult; try { iResult = Convert.ToInt32(result); } catch { iResult = -1; } if (iResult >= 0) { return((iResult + 1).ToString("D3")); } //ถ้าไม่สามารถแปลงได้ ให้ throw error แสดงให้ user เห็น var clash = new RulesException <zz_Reference>(); clash.ErrorFor(x => x.Code, "สร้างรหัสอัตโนมัติไม่ได้เนื่องจาก code เดิมไม่ใช่ตัวเลข"); throw clash; }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="stay"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(Staying90Day stay, int numRecursive = 5) { try { _checkCode(stay, stay.Id == 0); _save(); } catch (Exception ex) { //ถ้าซ้ำ ก็หา code ใหม่... if (ex.ExMessage() == "dupcode") { if (numRecursive > 0) { stay.Code = GetNewCode(); _saveCheckCode(stay, --numRecursive); //recursive } else { var clash = new RulesException <Staying90Day>(); clash.ErrorFor(x => x.Code, "Sorry, error when generating the code."); throw clash; } } else { throw; } } }
// // Persistance /// <summary> /// บันทึกข้อมูลลง database (ef) จริง ๆ /// </summary> private void _save() { try { entities.SaveChanges(); } catch (Exception ex) { //ตรวจสอบ error จาก sql server string exMessage = ex.ExMessage(); //เช็คค่า unique index ไม่ต้อง loop เช็คซ้ำอีกแล้ว if (exMessage.Contains("IX_zz_References_RefTypeId_Code")) { var clash = new RulesException <zz_Reference>(); clash.ErrorFor(x => x.Code, "รหัสซ้ำไม่ได้ค่ะ"); throw clash; } if (exMessage.Contains("IX_zz_References_RefTypeId_RefName")) { var clash = new RulesException <zz_Reference>(); clash.ErrorFor(x => x.RefName, "ความหมายซ้ำไม่ได้ค่ะ"); throw clash; } throw; // Rethrow any other exceptions to avoid interfering with them } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="endorseStamp"></param> /// <param name="isCreate"></param> private void _validate(EndorseStamp endorseStamp, bool isCreate) { var errors = new RulesException <EndorseStamp>(); if (endorseStamp.Code != "") { int endorseStampId = 0; if (!isCreate) { endorseStampId = endorseStamp.Id; } var oldCode = (from p in entities.EndorseStamps where (p.Id != endorseStampId) && (p.Code == endorseStamp.Code) && (p.StampDate.Year == endorseStamp.StampDate.Year) select p.Id).Count(); if (oldCode > 0) { errors.ErrorFor(x => x.Code, "Code ที่กรอก ซ้ำกับรายการอื่นในปีเดียวกัน"); } } if (errors.Errors.Any()) { throw errors; } }
// // Persistance /// <summary> /// บันทึกข้อมูลลง database (ef) จริง ๆ /// </summary> private void _save() { try { entities.SaveChanges(); } catch (Exception ex) { //ตรวจสอบ error จาก sql server string exMessage = ex.ExMessage(); //เช็คค่า unique index ไม่ต้อง loop เช็คซ้ำอีกแล้ว if (exMessage.Contains("IX_mst_Product_ProdCode")) { throw new ApplicationException("dupcode"); } if (exMessage.Contains("IX_mst_Product_ProdName")) { var clash = new RulesException <Alien>(); clash.ErrorFor(x => x.Name.FirstName, "Sorry, duplicate name not allowed"); throw clash; } throw; // Rethrow any other exceptions to avoid interfering with them } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="crew"></param> /// <param name="isCreate"></param> private void _validate(Crew crew, bool isCreate) { var errors = new RulesException <Crew>(); if (crew.Code != "") { int crewId = 0; if (!isCreate) { crewId = crew.Id; } var oldCode = (from p in entities.Crews where (p.Id != crewId) && (p.Code == crew.Code) && (p.IsCrew == crew.IsCrew) && !p.IsCancel && (p.ConveyanceInOutId == crew.ConveyanceInOutId) select p.Id).Count(); if (oldCode > 0) { errors.ErrorFor(x => x.Code, "Code ที่กรอก ซ้ำกับรายการอื่นในหน้านี้"); } } if (errors.Errors.Any()) { throw errors; } }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="convInOut"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(ConveyanceInOut convInOut, int numRecursive = 5) { try { _checkCode(convInOut, convInOut.Id == 0); _save(); } catch (Exception ex) { //ถ้าซ้ำ ก็หา code ใหม่... if (ex.ExMessage() == "dupcode") { if (numRecursive > 0) { convInOut.Code = GetNewCode(); _saveCheckCode(convInOut, --numRecursive); //recursive } else { var clash = new RulesException <ConveyanceInOut>(); clash.ErrorFor(x => x.Code, "Sorry, error when generating the code."); throw clash; } } else { throw; } } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="addRemoveCrew"></param> /// <param name="isCreate"></param> private void _validate(AddRemoveCrew addRemoveCrew, bool isCreate, int addRemoveType) { var errors = new RulesException <AddRemoveCrew>(); if (addRemoveCrew.Code != "") { int addRemoveCrewId = 0; if (!isCreate) { addRemoveCrewId = addRemoveCrew.Id; } //ถ้ามี subcode if (addRemoveCrew.SubCode != "") { var oldCode = (from p in entities.AddRemoveCrews where (p.Id != addRemoveCrewId) && (p.Code == addRemoveCrew.Code) && (p.SubCode == addRemoveCrew.SubCode) && (p.AddRemoveType == addRemoveType) && (!p.IsCancel) select p.Id).Count(); if (oldCode > 0) { errors.ErrorFor(x => x, "ลำดับที่และลำดับย่อยที่กรอก ซ้ำกับรายการอื่นในปีเดียวกัน"); } } //ถ้าไม่มี ให้เช็คกับ code โดยตรง else { var oldCode = (from p in entities.AddRemoveCrews where (p.Id != addRemoveCrewId) && (p.Code == addRemoveCrew.Code) && (p.AddRemoveType == addRemoveType) && (!p.IsCancel) select p.Id).Count(); if (oldCode > 0) { errors.ErrorFor(x => x, "ลำดับที่ ที่กรอก ซ้ำกับรายการอื่นในปีเดียวกัน"); } } } if (errors.Errors.Any()) { throw errors; } }
public void validate_fail_case_sensitive_keys() { var rulesException = new RulesException(); rulesException.ErrorFor("menu", "test"); rulesException.ErrorFor("Menu", "test"); var rulesExceptions = new List <RulesException> { rulesException }; Assert.Throws <ArgumentException>(() => { new RulesExceptionCollection(rulesExceptions).GetRulesExceptionDto(); }); var rulesExceptionCollection = new RulesExceptionCollection(rulesExceptions).GetRulesExceptionDto(false); }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="endorseStamp"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(EndorseStamp endorseStamp, int numRecursive = 5) { try { _save(); } catch (Exception ex) { var clash = new RulesException <EndorseStamp>(); clash.ErrorFor(x => x, "Error :" + ex.ExMessage()); throw clash; } }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="visa"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(VisaDetail visa, int numRecursive = 5) { try { _save(); } catch (Exception ex) { var clash = new RulesException <VisaDetail>(); clash.ErrorFor(x => x.Code, "Error :" + ex.ExMessage()); throw clash; } }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="reentry"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(ReEntry reentry, int numRecursive = 5) { try { _save(); } catch (Exception ex) { var clash = new RulesException <ReEntry>(); clash.ErrorFor(x => x.Code, "Error :" + ex.ExMessage()); throw clash; } }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="alien"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(Alien alien, int numRecursive = 5) { try { _save(); } catch (ApplicationException ex) { var clash = new RulesException <Alien>(); clash.ErrorFor(x => x.Name.FirstName, "Error :" + ex.ExMessage()); throw clash; } }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="conv"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(Conveyance conv, int numRecursive = 5) { try { _save(); } catch (ApplicationException ex) { var clash = new RulesException <Conveyance>(); clash.ErrorFor(x => x.Name, "Error :" + ex.ExMessage()); throw clash; } }
/// <summary> /// เรียกการบันทึกข้อมูล โดยตรวจสอบว่า Code ซ้ำหรือไม่ /// </summary> /// <param name="addRemoveCrew"></param> /// <param name="numRecursive">จำนวนที่ให้วน loop ปรกติไม่ต้องใส่มา</param> private void _saveCheckCode(AddRemoveCrew addRemoveCrew, int numRecursive = 5) { try { _save(); } catch (Exception ex) { var clash = new RulesException <AddRemoveCrew>(); clash.ErrorFor(x => x, "Error :" + ex.ExMessage()); throw clash; } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="alien"></param> /// <param name="isCreate"></param> private void _validate(Alien alien, bool isCreate) { var errors = new RulesException <Alien>(); if (alien.Name.FirstName == "a") { errors.ErrorFor(x => x.Name.FirstName, "a is not allowed..."); } if (errors.Errors.Any()) { throw errors; } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="reference"></param> /// <param name="isCreate"></param> private void _validate(zz_Reference reference, bool isCreate) { var errors = new RulesException <zz_Reference>(); if (reference.RefName == "a") { errors.ErrorFor(x => x.RefName, "a is not allowed..."); } if (errors.Errors.Any()) { throw errors; } }
public void Validate(RulesBaseParams parameters) { ValidateModelPropertiesAndBuildRulesException(this); if (string.IsNullOrEmpty(Foo)) { RulesException.ErrorFor(x => x.Foo, "Please supply a Foo"); } if (parameters.ThrowException) { RulesException.ThrowException(); } }
public void Validate() { ValidateModelPropertiesAndBuildRulesException(this); var parameters = new RulesBaseParams { ThrowException = false }; RulesException.ErrorMessages.Add("This is a fake error message"); RulesException.ErrorFor(x => x.Value, "This is a fake error"); var rulesExceptionCollection = ValidateModelPropertiesAndBuildRulesExceptionCollection <RulesBaseParams>(this, parameters); rulesExceptionCollection.ThrowException(); }
/* * This function would be implemented further down the chain as * BusinessRulesBase provides the structure, not the implementation * which would be custom per Model. * * This would occur on a InsertOrUpdate at the service layer. */ protected override void ValidateModelRules(ModelFoo modelFoo) { if (Repository.All() .Where(x => x.Id != modelFoo.Id && x.Name == modelFoo.Name).Any()) { RulesException.ErrorFor(x => x.Name, DuplicateName); } if (!String.IsNullOrEmpty(modelFoo.Name) && (modelFoo.Name.Length < 2 || modelFoo.Name.Length > 6)) { RulesException.ErrorFor(c => c.Name, RangeLengthName); } }
public void validate_rulesexceptioncollection_fails() { var arrayCanError = new ArrayCanError { Values = new List <string> { { "Item 1" } } }; var rulesException = new RulesException(); rulesException.ErrorFor("Values[0]", "Test Error"); var rulesExceptions = new List <RulesException> { rulesException }; var rulesExceptionCollection = new RulesExceptionCollection(rulesExceptions); var rulesExceptionDto = rulesExceptionCollection.GetRulesExceptionDto(); }
// // Persistance /// <summary> /// บันทึกข้อมูลลง database (ef) จริง ๆ /// </summary> private void _save() { try { entities.SaveChanges(); } catch (Exception ex) { //ตรวจสอบ error จาก sql server string exMessage = ex.ExMessage(); //ไม่มีการเช็คอะไร เพียงแค่เขียนเป็นตัวอย่างเอาไว้ if (exMessage.Contains("IX_Staying90Days_Code")) { var clash = new RulesException <Staying90Day>(); clash.ErrorFor(x => x.Code, "Sorry, problem with index on Code"); throw clash; } throw; // Rethrow any other exceptions to avoid interfering with them } }
/// <summary> /// ตรวจสอบว่าข้อมูลที่กรอกเข้ามาถูกต้องหรือไม่ /// โดยเป็นส่วนของ logic ที่ไม่สามารถระบุใน metadata ไม่ได้ และไม่ได้ระบุเอาไว้ใน constraint ของ database /// </summary> /// <param name="stay"></param> /// <param name="isCreate"></param> private void _validate(Staying90Day stay, bool isCreate) { var errors = new RulesException <Staying90Day>(); //ไม่ต้องเช็ค code ตรงนี้ ปล่อยให้ saveCheckCode จัดการ //if (stay.Code != "") //{ // //_checkCode(stay,isCreate); //} if (string.IsNullOrWhiteSpace(stay.ArrivalCard.DocNo)) { errors.ErrorFor(x => x.ArrivalCard, "กรุณากรอกเลขที่บัตรขาเข้าด้วยค่ะ"); } if (errors.Errors.Any()) { throw errors; } }