Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Inheritance in C#

Is there a known pattern to inherit data in a hierarchical object structure? I have a hierarchical 'Item' structure which needs to inherit its 'Type' from its 'Parent' (have the same data as default). The type of sub item can be modified by its own, and when the type of parent Item changes, all sub items which their type is not changed, should get the new type of parent.

Note that I cannot fake it like

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

        return type;
    }
}

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

The only way I can think of 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.

like image 662
reticent Avatar asked Dec 12 '25 04:12

reticent


2 Answers

It's a common problem, usually related to maintenance of various hierarchical settings/configurations. So, I guess a solution to it can be considered "a pattern".

Anyways, from the internal architecture perspective you have 2 major options:

  • normalized structure
  • denormalized structure

"Normazlied" is the one implemented with recursion. A particular piece of data is always stored in one place, all the other places have references to it (e.g., to parent). The structure is easily updated, but readng from it may be a problem.

"Denormalized" means that every node will store the whole set of settings for its level and whenever you update a node it takes some time to go down the hierarchy and corect all the children nodes. But the reading operation is instant.

And so the "denormalized" version seems to be more widely used, because the common scenario with settings is that you update them rarely, while read them often, hence you need better read performance. For example, Windows ACL security model uses the "denormalized" approach to make security checks fast. You can read how they resolve conflicts between the "inherited" and explicit permissions (ACEs) by checking them in a specific order. That might be an overkill for your particular system though, you can simply have a flag that a particular value was overriden or, on the opposite, reset to "default"...

Further details depend on your system needs, you might waht to have a "hybrid" architecture, where some of the fields would be "normalized" and some others won't. But you seem to be on the right way.

like image 142
Max Galkin Avatar answered Dec 14 '25 16:12

Max Galkin


I'm not 100% sure what it is you are trying to do... but you could use generics to pass the type of a parent object into a child object... But having a setter there doesn't really make sense... The Parent object's type will be set when it's instantiated, so why would you have a setter there to change it.

Assuming you have something like this...

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

So then, when you have a Parent Object of any type, you can pass that to your Child Property...

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; }
}

Calling any of those ChildObj.Type Properties will return ParentA, ParentB & ParentC respectively.

Buit I've a funny feeling you haven't fully explained what it is you're trying to do. Can you post some more code examples showing a Parent Class & Child/Item Class

like image 33
Eoin Campbell Avatar answered Dec 14 '25 17:12

Eoin Campbell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!