Reputation: 95
This may be a software design or pattern issue.
A C# WinForms solution using LINQ to SQL . There is a Form which allows user to edit property values of a Customer object.
If the user wants to create a new Customer then the Form is passed a new Customer.
If the user wants to edit an existing Customer then the Form is passed the existing Customer.
When the user attempts to close the form a check is done to see if there are edits to property values need to be saved, and the user prompted to save the changes:
protected Boolean CanExit()
{
Boolean isSaveNeeded = false;
String message = String.Empty;
if (dataContext.GetChangeSet().Updates.Contains(lookupObject))
{
message = "Save your changes before closing?";
isSaveNeeded = true;
}
else if (dataContext.GetChangeSet().Inserts.Contains(lookupObject))
{
message = String.Format("Save new {0} before closing?", lookupSingularName);
isSaveNeeded = true;
}
if (isSaveNeeded && XtraMessageBox.Show(message, "Close Form", MessageBoxButtons.YesNo, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{ return ActionSave(); }
else
{ return true; }
}
There are 2 issues with this:
Prompts to Save for a new Customer which has had no properties set.
Prompts to Save if the user has edited, say, a property value AND THEN changed the value back to its original value
I had a pattern for resolving both issues using ADO 'Original Values.
What is the best way to resolve the issues using a DataContext?
Upvotes: 1
Views: 131
Reputation: 95
I resolved the issue - by not using Linq to SQL
Replaced with an ORM ( DevExpress XPO as it happens)
An ORM makes OO development far easier
Upvotes: 0
Reputation: 13676
How to determine if a newly created object has had any property values set by user
One possible thing to do is :
Make a temp copy of your object before any changes are made.
If save is required (user pressed save button or attempted to close the edit window) then we should validate the data that he filled our form with. If this data contains some invalid values then we need to alert user and return him to edit the data again. After the data is validated, it should be compared to the initial data and if it differs then it has to be saved.
Something on lines of :
class Customer
{
//...... your logic
public static bool operator ==(Customer x, Customer y)
{
return true; // add your logic for comparison here
}
public static bool operator !=(Customer x, Customer y)
{
return false; // add your logic for comparison here
}
}
let myCustomer will be your object to add / edit
Customer temp = null;
if(myCustomer != null) temp = new Customer() { Id = myCustomer.Id, Name = myCustomer.Name ......... }
// add some logic to return user into edit form until it has valid values
while(!ValidateForm(myCustomer)) myCustomer = UIEditCustomer();
if(temp == null || myCustomer != temp) PromptToSave();
Upvotes: 0
Reputation: 30454
Your customer object should implement IEquatable.
Before the operator starts editing an existing customer remember the original Customer. In CanExit create a Customer object with the edited data and compare with the original customer to see if something has changed, and thus has to be saved. This will solve the problem when some changes were made and changed back.
If a new customer is created, then the original Customer is null.
public class Customer : IEquatable<Customer>
{
...
}
public class FormEditCustomer : ...
{
public Customer OriginalCustomer {get; set;}
private bool CanExit()
{
Customer editedCustomer = GetEditedData();
if (this.OriginalCustomer == null)
{ // new customer
return ProcessNewCustomer(editedCustomer);
}
else
{ // existing customer
if (!this.OriginalCustomer.Equals(editedCustomer))
{ // customer changed
return ProcessChanges(editedCustomer);
}
else
{ // no changes:
return RestoreCustomer(this.originalCustomer); // only if useful
}
}
}
}
Upvotes: 0
Reputation: 471
you can solve it easily using Memento design pattern,(this way you can compare objects - after and before edit)
you can check wiki details here.
https://en.wikipedia.org/wiki/Memento_pattern
and can check article on memento.
http://www.codeproject.com/Articles/186184/Memento-Design-Pattern
Upvotes: 0