Delete subitems / children

Sep 10, 2014 at 8:31 AM
I am trying to implement deletions of subitems/children using this framework with no luck yet.

I have an Entity called Language and several other Entities that each have details relating to the Language Entity.

For example:

public class Language Entity
{
    public int LanguageID { get; set; }
    public virtual ICollection<ApplicationSettingDetail> ApplicationSettingDetails { get; set; }
} 

public class ApplicationSetting
{
    public int ApplicationSettingId { get; set; }
    public virtual ICollection<ApplicationSettingDetail> ApplicationSettingDetails { get; set; }
}

public class ApplicationSettingDetail
{
        public int ApplicationSettingDetailId { get; set; }  
        public int ApplicationSettingId { get; set; }  
        public int LanguageId { get; set; } 
        public string Value { get; set; }  

        // Foreign keys
        public virtual ApplicationSetting ApplicationSetting { get; set; } 
        public virtual Language Language { get; set; }      
}
What I would like to do is when I delete a Language entity the other related Entities such as ApplicationSettingDetails get deleted.

I understand that this needs to be done in the LanguageService and somehow use the UnitOfWork but I do not know how I should implement this.

My implementation at the moment looks like this:
    public class LanguageController : Controller
    {

        private ILanguageService service;

        public LanguageController(ILanguageService svr)
        {
            service = svr; 
        }
    }


    public class LanguageService : Service<Language>, ILanguageService 
    {
        private readonly IRepositoryAsync<Language> _repository;

        public LanguageService(IRepositoryAsync<Language> repository)
            : base(repository)
        {
            _repository = repository;
        }

        public Language GetById(int LanguageID)
        {
            return _repository.GetByID(LanguageID);
        }

        public IEnumerable<Language> GetAll()
        {
            return _repository.GetAll();
        }

        public override void Delete(Language entity)
        {   
            base.Delete(entity.LanguageId);
        }


        public SaveResult Save()
        {
            SaveResult saveResult = new SaveResult(false);

            try
            {
                saveResult.Successful = _repository.UnitOfWork().SaveChanges() > 0;
            }
            catch (Exception e)
            {
                saveResult.Successful = false; 
                saveResult.Exception = e;
            }

            return saveResult;
        }
    }

    public static class LanguageRepository
    {
        public static Language GetByID(this IRepository<Language> repository, int applicationSettingId)
        {
            return repository.Find(applicationSettingId);
        }

        public static IEnumerable<Language> GetAll(this IRepository<Language> repository)
        {
            return repository.Queryable().AsEnumerable();
        } 

    }

    // Language
    public class Language : MajorItem
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [DisplayName("Language ID")]
        public int LanguageId { get; set; } // LanguageID (Primary key)

        . . . 

        // Reverse navigation
        public virtual ICollection<ApplicationSettingDetail> ApplicationSettingDetails { get; set; } 
        . . . 
        public Language()
        {
            //default
            SortOrder = 99;

            ApplicationSettingDetails = new List<ApplicationSettingDetail>(); 
            . . .
        }
    }
Any help will be appreciated.
Sep 10, 2014 at 12:43 PM
Is adding the following to the LanguageService delete call a good solution?
   public override void Delete(Language entity)
        {
            IUnitOfWork  unitOfWork= _repository.UnitOfWork();

            try
            {
                unitOfWork.BeginTransaction();

                //ApplicationSettingDetails
                while (entity.ApplicationSettingDetails.Count() > 0)
                {
                    unitOfWork.Repository<ApplicationSettingDetail>().Delete(entity.ApplicationSettingDetails.First());
                }

                base.Delete(entity.LanguageId);

                unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                unitOfWork.Rollback();
                throw ex;
            }
            
        }
Coordinator
Sep 11, 2014 at 12:22 AM

I don’t see anything wrong with it, however you can configure EF and/or Sql to do a cascading delete, again, not required, just another option.

Marked as answer by lelong37 on 9/12/2014 at 3:10 PM