Interface suggestion/question

Interface suggestion/question

Old forum URL: forums.lhotka.net/forums/t/1774.aspx


razorkai posted on Thursday, November 16, 2006

I realised today that I am forever writing code like this:-

//Clone the object so we keep the bound object in the current state if any validation or the save fails.
Company companyClone = CurrentCompany.Clone();

//Try saving the clone, and if it succeeds replace the CompanyObject with the clone and rebind the UI
companyClone.ApplyEdit();
CurrentCompany = companyClone.Save();
bsCompany.DataSource = CurrentCompany;

It occured to me that it would be a good candidate for a generic method such as

public static T CloneAndSave<T>(ISavable obj)

where I could make the Clone and perform the ApplyEdit and Save calls on the Clone.  The trouble is that ISavable does not include the Clone method or the ApplyEdit method.  I can cast to ICloneable to get around the Clone problem, but there is no interface that gives me access to ApplyEdit. 

Any chance of adding ApplyEdit to ISavable?  Or any other way to do this?

TIA


razorkai replied on Thursday, November 16, 2006

Don't worry, found a better way to do this.  Simply added a CloneAndSave method to my derived BusinessBase and BusinessListBase classes.  Works fine!

ajj3085 replied on Thursday, November 16, 2006

Why not just change ISavable to T, and add a restriction that T is of BusinessBase<T>?

razorkai replied on Thursday, November 16, 2006

Would that work for BusinessListbase objects too?  Need this to work for both objects and collections of objects.

ajj3085 replied on Thursday, November 16, 2006

That's true, it wouldn't work for lists. 

Copyright (c) Marimer LLC