tags:

views:

13

answers:

1

We have our own custom ORM wrapper with classes like Contact, Company, etc., which have properties such as Name, Address, and so on. You "create" an instance of a class, which implicitly downloads the current record from the database:

// fetch a contact with ID myRecordID from connection myConnection
Contact myContact = Contact.Create(myRecordID, myConnection);
myContact.Birthday = Date.Now;

Here's the thing: for performance reasons (plus other considerations such as transactions), saving doesn't happen implicitly when setting a property. Instead, you have to call save():

myContact.save(); // great! the new birthday is in the database

This works great — except when you forget to call save(). The compiler won't see an issue, but the above code doesn't do anything useful without saving.

I'm wondering whether the API can let the compiler know that, when properties have been set, the compiler should warn if save() isn't called before the Contact instance leaves scope.

A: 

You'll have to implement something to let your code know that the entity has been changed. For example by implementing INotifyPropertyChanged in your entities and listening for a change. Raise property changed on every property that needs to be persisted to the database.

When the contact has been changed you can even automatically save the contact at the end of a transaction. Of course you need to add the infrastructure code.

RonaldV
That doesn't really help me; I'd need a way for the API to detect that scope has been left.
Sören Kuklau