Thanks
"Also, what if the operation was an all-or-nothing affair? Where is the
rollback for this kind of thing? Is it in a transaction?"
In this case I define a transaction as saving a single Contact not the List
of all contacts
in such a way if some contact failed to be saved(due to validation rules)
I can still try to save other contacts
"I think that having separate exceptions for when the list is partially
saved and when it is completely saved is a bad idea, as it will fragment
your code....
I would store it in a Hashtable,
keyed on the unique identifier for each object (only you know what it is),
and then throw one exception with the hashtable attached to it."
ok,i really dont need separate exceptions
Thanks.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:u8**************@TK2MSFTNGP15.phx.gbl...
Julia,
I think that having separate exceptions for when the list is partially
saved and when it is completely saved is a bad idea, as it will fragment
your code.
Also, what if the operation was an all-or-nothing affair? Where is
the rollback for this kind of thing? Is it in a transaction?
Basically, if there is an exception that is thrown (and I hope you are
not using them for business logic errors), I would store it in a
Hashtable, keyed on the unique identifier for each object (only you know what it is),
and then throw one exception with the hashtable attached to it.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com
"Julia" <co********@012.net.il> wrote in message
news:%2******************@TK2MSFTNGP11.phx.gbl... Thanks for all you responses
assuming I have a collection of objects which I want to save in a
database
I wonder if the following is the way to deal with a situation that only
some
of the objects were saved
function Save()
{
Exception expList=new Exception() //A list to collect exceptions
foreach(Contact contact in Contacts)
{
try
{
contact.Persist //Save the current contact
}
catch(Exception e)
{
expList.Add(contact,e) //catch the exception for the current
object and add it to the list
}
}
if(expList.Count>0)
{
//Throw exception and attach the list of exceptions
throw new PartlyPersistentException("Some objects failed to be
saved",expList)
}
if(expList.Count==Contacts.Count){
//Throw exception and attach the list of exceptions
throw new PersistentFailedException("All objects failed to be
saved",expList)
}
}