How can I create a parallel inheritance structure in C #?

I have 2 sets of 2 classes, where each pair has a super / sub-class relationship, and the orthogonal pair has a dependency relationship. I am trying to determine what to do with constructors and / or property bodies in order to simplify the model as much as possible with minimal data duplication.

Here is the structure in the code:

public class Base1 {
    public List<Base2> MyBase2Things { get; set; }
    // Do things with Base2 objects
}

public class Sub1 : Base1 {
    public List<Sub2> MySub2Things { get; set; }
    // Do things with Sub2 objects and also with Base2 objects
}

public class Base2 {
    public Base1 MyBase1 { get; set; }
    // Do things with the Base1 object
}

public class Sub2 : Base2 {
    public Sub1 MySub1 { get; set; }
    // Do things with the Sub1 object
}

I looked at overriding basic properties in subclasses, but that doesn't fit very cleanly because properties in subclasses don't have the same signature, so I would have to add properties.

I also considered setting the base property in subclass constructors and methods set, but there is no way to override the sub-class property if updating the property of the base class.

, ( )?

. , . , , .

+5
2

, . - , , , 4 .

, .

1: .

public class ItemParent {  // formerly Base1
    public List<ItemChild> MyChildren {get; set;}
}

public class ItemChild {  // formerly Base2
    public ItemParent MyParent {get; set;}
}

public class Car : ItemParent {  // formerly Sub1
    public List<CarPart> MyParts {get; set;}
}

public class CarPart : ItemChild {  // formerly Sub2
    public Car ParentCar {get; set;}
}

, CarPart, ItemChild. , .

public class ItemParent<T> where T : ItemChild {
    public List<T> MyChildren {get; set;}
}

public class ItemChild<T> where T : ItemParent {
    public T MyParent {get; set;}
}

public class Car : ItemParent<CarPart> {}
public class CarPart : ItemChild<Car> {}

public class Truck : ItemParent<TruckPart> {}
public class TruckPart : ItemChild<Truck> {}

subclass.MyChildren [] MyParts, MyChildren.

, - , / . , Truck-TruckParts ( -, - ..), .

/ "" (, ), :

public class ParentChildCollection<TParent, TChild> {}

public class Car {
    private ParentChildCollection<Car, CarPart> PartHierarchy;
    public List<CarPart> MyParts {get { return PartHierarchy.GetMyChildren(this); } }
}

public class CarPart {
    private ParentChildCollection<Car, CarPart> PartHierarcy;
    public Car ParentCar {get { return PartHierarchy.GetMyParent(this); }}
}

, , Truck Car ( , ).

2. , .

public class Car {  // formerly Base1
    public List<CarPart> MyParts {get; set;}
}

public class CarPart {  // formerly Base2
    public Car MyParent {get; set;}
}

public class Truck : Car {  // formerly Sub1
    public List<TruckPart> MyParts {get; set;}
}

public class TruckPart : CarPart {  // formerly Sub2
    public Truck MyParent {get; set;}
}

Truck Car . , . (Vehicle-VehiclePart). . / stictly Car Truck.

, , . , - , ( ) .

+2

... - :

public class Base1<T>
    where T: Base2
{
    public List<T> MyThings { get; set; }

    protected Base1(List<T> listOfThings)
    {
        this.MyThings = listOfThings;
    }
}

public class Sub1 : Base1<Sub2>
{
    public Sub1(List<Sub2> listofThings):
        base(listofThings)
    {

    }
}

, , ( ), :

// Base 1 hierachy
abstract public class Base1
{
    protected abstract Base2 GetBase2(int index); //we can't return the list directly
}

public class Base1<Base2Type> :Base1
    where Base2Type : Base2
{
    public List<Base2Type> MyBase2s { get; set; }

    protected Base1(List<Base2Type> listOfThings)
    {
        this.MyBase2s = listOfThings;
    }

    protected override Base2  GetBase2(int index)
    {
        return MyBase2s[index];
    }

}

public class Sub1<MySub1Type,MySub2Type> : Base1<MySub2Type>
    where MySub1Type : Sub1<MySub1Type,MySub2Type>
    where MySub2Type : Sub2<MySub1Type, MySub2Type>
{
    public Sub1(List<MySub2Type> listOfThings):
        base(listOfThings)
    {
        this.MyBase2s = listOfThings;
    }
}

public class Sub1 : Sub1<Sub1,Sub2>
{
    public Sub1(List<Sub2> listofThings):
        base(listofThings)
    {

    }
}


// base 2 hirachy
abstract public class Base2
{
    protected abstract Base1 MyBase1 { get; }
}

public class Base2<Base1Type,Base2Type> : Base2
    where Base1Type: Base1<Base2Type>
    where Base2Type : Base2
{
    public Base1Type myBase1;

    protected override Base1 MyBase1{ get {return myBase1;} }
}

public class Sub2<Sub1Type, Sub2Type> : Base2<Sub1Type,Sub2Type>
    where Sub1Type : Sub1<Sub1Type,Sub2Type>
    where Sub2Type : Sub2<Sub1Type,Sub2Type>
{

}

public class Sub2 : Sub2<Sub1,Sub2>
{

}
0

All Articles