Recursive generics with one-to-many external reference, dynamic types

Viewed 42

I have a database structure (which unfortunately I cannot modify) according to the attached scheme. In practice I have an entity (TBL-A) which has a parent-child hierarchical structure and has an external reference (one to many with TBL-B) with the other entity which in turn has the same structure as A.

All tables have been mapped with Entity Framework Code Firts.

My goal is that if "I disable" an entity in "A" must:

  1. disable (recursively) all children, grandchildren, etc ...;
  2. disable all references (from A to B) and disable in B (recursively all children, grandchildren, etc ...)
  3. disable all references (from B to C) and disable in C (recursively all children, grandchildren, etc ...)

So if I disable an element in A it must disable all those connected in A + B + C; If I disable an element in B it must disable all those connected in B + C;

What did I think !!! ????

An interface where "TEntity" and "TEntityRef" have the same "where" condition, but something is wrong, because I can't compile due to an error in the TblA class, RIGHT gives me this message

There is no implicit reference conversion from 'TblB' to 'IEntity <TblB>'

Could you help me understand how to solve the problem? This is my code

enter image description here

   public interface IEntity<TEntity, TEntityRef>
        where TEntity : IEntity<TEntity, TEntityRef>
        where TEntityRef : IEntity<TEntity, TEntityRef>
    {
        int Id { get; set; }
        int? IdParent { get; set; }
        bool Disabled { get; set; }
        ICollection<TEntity> Childs { get; set; }
        TEntity Parent { get; set; }
        ICollection<TEntityRef> References { get; set; }
    }

    public abstract class EntityBase<TEntity, TEntityRef> : IEntity<TEntity, TEntityRef>
        where TEntity : IEntity<TEntity, TEntityRef>
        where TEntityRef : IEntity<TEntity, TEntityRef>
    {
        public int Id { get; set; }
        public int? IdParent { get; set; }
        public bool Disabled { get; set; }
        public ICollection<TEntity> Childs { get; set; }
        public TEntity Parent { get; set; }
        public ICollection<TEntityRef> References { get; set; }

        public void Disable()
        {
            Disabled = true;

            Disable(Childs);

            Disable(References);
        }

        private void Disable(ICollection<TEntity> items)
        {
            if (items == null || items.Count() == 0) return;

            foreach (var item in items.Where(c => !c.Disabled))//only active entities
            {
                item.Disabled = true;
                Disable(item.Childs);
                Disable(item.References);
            }
        }

        private void Disable(ICollection<TEntityRef> items)
        {
            if (items == null || items.Count() == 0) return;

            foreach (var item in items.Where(c => !c.Disabled))//only active entities
            {
                item.Disabled = true;
                Disable(item.Childs);
                Disable(item.References);
            }
        }
    }

    public partial class TblA : EntityBase<TblA, TblB>
    {
        public TblA()
        {
            Childs = new HashSet<TblA>();
            References = new HashSet<TblB>();
        }
    }


    public partial class TblB : EntityBase<TblB, TblC>
    {
        public TblB()
        {
            Childs = new HashSet<TblB>();
            References = new HashSet<TblC>();
        }
    }
0 Answers
Related