public bool SaveData() { Bus.Customer customer = ModelFromView(); if (this.HasChanged()) { if (this.ValidateData(customer)) { try { customer.Update(); CustomerSaveEventArgs args = new CustomerSaveEventArgs(); args.CustomerId = this.CustomerId; args.NewFullName = customer.Model.LastName + ", " + customer.Model.FirstName; OnCustomerSave(this, args); customer = null; return(true); } catch (Exception x) { //KF: This is only very basic exception handling, a full solution would look for inner exceptions, particular types and much more g.LogEvent("Save Error: " + x.Message, EventLogging.Events.EventLevel.Exception); } } return(false); } else { //If nothing to do return true return(true); } }
public bool ValidateData(Bus.Customer customer) { //KF There are lots of ways and places of doing these checks //Generally I put the screen logic here, that is mandatory fields are filled in and similar, anything that only needs the data on the screen //Then any business logic goes into the business layer, only it loads up other data from outside the screen //the validation can (and should) also go into the database so that any scripts also have it follow the business rules //However the front end shouldn't just handle the database exceptions it should do it's own validation and give the user a sensible //error message StringBuilder sb = new StringBuilder(); VF.RequiredFieldHasText(txtFirstName, lblFirstName, sb); VF.RequiredFieldHasText(txtLastName, lblLastName, sb); customer.Validate(sb); if (sb.ToString() != "") { if (!this.SuppressMessages) { XtraMessageBox.Show(sb.ToString(), "Validation Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(false); } else { return(true); } }
public void ValidateCustomer() { //KF: In a full solution each piece of validation could have its own test. //The validation could return a collection of specific error numbers that the //routines here would check for. So instead of using a StringBuilder the //validation functions would return a collection of errors. frmCustomer frm = new frmCustomer(); frm.CustomerId = Guid.Parse("88A96958-A302-4913-9ADC-1997B49C7571"); frm.LoadData(); //KF: Should be able to implement a way of finding controls by name or by tag frm.txtFirstName.Text = ""; frm.txtLastName.Text = ""; GenesisModels.Customer modelCustomer = new GenesisModels.Customer(); modelCustomer.Id = Guid.Parse("88A96958-A302-4913-9ADC-1997B49C7571"); modelCustomer.FirstName = "Kevin"; modelCustomer.LastName = "Finegan"; Bus.Customer customer = new Bus.Customer(modelCustomer); frm.SuppressMessages = true; Assert.IsFalse(frm.ValidateData(customer)); customer = null; }
private Bus.Customer ModelFromView() { Models.Customer modCustomer = new Models.Customer(); modCustomer.Id = this.CustomerId; modCustomer.FirstName = txtFirstName.Text; modCustomer.LastName = txtLastName.Text; Bus.Customer customer = new Bus.Customer(modCustomer); return(customer); }
public void UpdateCustomer() { GenesisModels.Customer modelCustomer = new GenesisModels.Customer(); modelCustomer.Id = Guid.Parse("88A96958-A302-4913-9ADC-1997B49C7571"); modelCustomer.FirstName = "Kevin"; modelCustomer.LastName = "Finegan"; Bus.Customer customer = new Bus.Customer(modelCustomer); customer.Update(); customer = null; }
public void LoadData() { Bus.Customer customer = new Bus.Customer(); if (this.CustomerId != null && this.CustomerId != Guid.Empty) { customer = customer.Select(this.CustomerId); txtId.Text = customer.Model.Id.ToString(); txtFirstName.Text = customer.Model.FirstName; txtLastName.Text = customer.Model.LastName; _originalFirstName = customer.Model.FirstName; _originalSurname = customer.Model.LastName; } }
public void ValidateDuplicateCustomer() { frmCustomer frm = new frmCustomer(); frm.CustomerId = Guid.Parse("A0A21498-8538-4539-9F44-B283803AFD1C"); frm.LoadData(); frm.txtFirstName.Text = "Kevin"; frm.txtLastName.Text = "Finegan"; GenesisModels.Customer modelCustomer = new GenesisModels.Customer(); modelCustomer.Id = Guid.Parse("A0A21498-8538-4539-9F44-B283803AFD1C"); modelCustomer.FirstName = "Kevin"; modelCustomer.LastName = "Finegan"; Bus.Customer customer = new Bus.Customer(modelCustomer); frm.SuppressMessages = true; Assert.IsFalse(frm.SaveData()); customer = null; }