Data Inheritance in C #

Is there a well-known template for data inheritance in the hierarchical structure of an object? I have a hierarchical structure "Element", which must inherit its "Type" from its "Parent" (have the same data as the default). The type of the subheading can be changed on its own, and when the type of the parent element changes, all the subitems that their type does not change should receive a new type of parent.

Notice that i can't fake it like

public string Type
{
    get
    {
        if (type == null)
            return Parent != null ? Parent.Type : null;

        return type;
    }
}

'because I have to fill in the values ​​in the database, and the structure is too deep to use recursion and not worry about performance.

The only way I can think about it now is

public string Type
{
    set
    {
        type = value;
        UpdateUnchangedChildren(value);
    }
}

public int AddChild(Item item)
{
    item.Type = Type;
    return Items.Add(item);
}

Is there a better way? Thanks.

+3
4

, /. , , "".

, 2 :

"Normazlied" - , . , (, ). , .

"Denormalized" , node , , node, , . .

"" , -, , , , , . , Windows ACL "" . , "" (ACE), . , , , , reset " "...

, "" , "", - . , , .

+3

100%, ... generics ... ... , , , .

, - ...

public class Child<T>
{
   public string Type
   {
       get { return typeof(T).ToString(); }
   }
}

, Parent , ...

public class ParentA
{
   public Child<ParentA> ChildObj { get; set; }
}

public class ParentB
{
   public Child<ParentB> ChildObj { get; set; }
}

public class ParentC
{
   public Child<ParentC> ChildObj { get; set; }
}

ChildObj.Type ParentA, ParentB ParentC .

Buit , , . , Child/Item

+1

, . , ( , , O (h ^ 2) O (h)), , , .

:

class Node
{
    string _cachedParentType = null;
    string _type;
    string Type 
    {
        get { return _type ?? _cachedParentType ?? (_cachedParentType = Parent.Type); }
        set 
        { 
            _type = value; 
            foreach (var child in Children) { child._cachedParentType = null; }
        }
    }
}

, , O (1) , , , " " O (h), h - ; O (k) k ( !). , , UpdateUnchangedChildren ( , ), WAY , .

+1

"... , .

? , , ""? ?

, , . , . , .

Of course, your second solution also uses recursion, simply reducing the hierarchy, not up. If child inserts occur at a different time and can absorb a possible hit in performance, it might be more acceptable.

0
source

Source: https://habr.com/ru/post/1708715/


All Articles