public bool Delete(Unit entity)
{
Contract.Requires(entity != null);
Contract.Requires(entity.Id >= 0);
using (IUnitOfWork uow = this.GetUnitOfWork())
{
IRepository<Unit> repo = uow.GetRepository<Unit>();
IRepository<ConversionMethod> repoCM = uow.GetRepository<ConversionMethod>();
entity = repo.Reload(entity);
// delete all conversions that are somehow connected to current unit
repoCM.Delete(entity.ConversionsIamTheSource);
repoCM.Delete(entity.ConversionsIamTheTarget);
// remove all associations between current unit and the conversions
entity.ConversionsIamTheSource.ToList().ForEach(a => a.Source = a.Target = null);
entity.ConversionsIamTheTarget.ToList().ForEach(a => a.Source = a.Target = null);
entity.ConversionsIamTheSource.Clear();
entity.ConversionsIamTheTarget.Clear();
//delete the unit
repo.Delete(entity);
// commit changes
uow.Commit();
}
// if any problem was detected during the commit, an exception will be thrown!
return (true);
}