views:

61

answers:

2

Hello, It's a weird question I know :)

I really like to do the things in the right way and I have a doubt.

I know about making a interface, using DI...

My question is:

Is better to have a method like "SaveChanges" that you have to call manually everytime you add / delete / whatever an object?:

_repo.Add(blah);
_repo.SaveChanges();

Or is better to save changes inside every method that modify the data?

On the other hand, should I have the connection always opened or have I to close it?

Im learning DB4O and I have a Close method that I call when I have to use the repo on another place (Like in another windows, I close before I open the window).

Thank you.

+1  A: 

I personally like the SaveChanges method to be separated. I think it allows for more flexibility in the consuming applications. Which means it can have more reuse.

For example, having it separate allows a 'transaction' approach where the repository can be continually modified and then if everything is acceptable the save method is called.

On the other side, if you want to save immediately without a separate call, you can create another version of the repository which calls the save method during CRUD operations.

Jerod Houghtelling
Fair enough, I understand now when to put the method or not. Thank you.
Jesus Rodriguez
A: 

I think in .Net DataSet they also use AcceptChanges() function to "commit" latest data modifications. Also in Oracle database there is a COMMIT command for similar task.

AareP