I have the following methods:
SingleUserMode()
- turns the single user mode on in the databaseBeginEdit()
- starts the edit of items in the context, locks them in the databaseSaveChanges()
- saves the changes to the databaseReleaseSingleUserMode()
- turns the single user mode off in the database
Each method returns an object which has a message and if it is was executed successfully.
I would like to know if there is a "nicer" way than to write this:
using (var storeContext = StoreContext.CreateContext())
{
var result = storeContext.SingleUserMode();
if (result)
{
//load data into storecontext...
result = storeContext.BeginEdit();
if (result)
{
//change data...
result = storeContext.SaveChanges();
if (result)
{
//update UI on success
}
}
var releaseResult = storeContext.ReleaseSingleUserMode();
if (!releaseResult)
{
result.AddSubResult(releaseResult); //add the failed release of single user mode to the "parent" result
}
}
if (!result)
{
//fire messagebox on error
}
}
(The comments indicate the places where usually there is a custom code, the rest is "always" the same)
FYI, the methods SingleUserMode
and ReleaseSingleUserMode
are optional and dont have to be called, though if you call SingleUserMode
you have to call ReleaseSingleUserMode
, but without it its just this:
using (var storeContext = StoreContext.CreateContext())
{
//load data into storecontext...
result = storeContext.BeginEdit();
if (result)
{
//change data...
result = storeContext.SaveChanges();
if (result)
{
//update UI on success
}
}
if (!result)
{
//fire messagebox on error
}
}