I have a following class structure:
interface IA
{
List<IB> Children { get; }
}
interface IB
{
IA Parent { get; }
List<IC> Children { get; }
}
interface IC
{
IB Parent { get; }
List<ID> Children { get; }
}
interface ID
{
IC Parent { get; }
}
Classes implementing those interfaces are closely coupled - we have sets of classes XA : A, XB : B, XC : C, XD : D. What I would like to achieve is knowing the exact type of the parent and children at compile time.
Generics are the obvious way, but the implementation looks awful to me:
interface IA<TA, TB, TC, TD>
where TA : IA
where TB : IB
where TC : IC
where TD : ID
{
List<TB> Children { get; }
}
interface IB<TA, TB, TC, TD>
where TA : IA
where TB : IB
where TC : IC
where TD : ID
{
TA Parent { get; }
List<TC> Children { get; }
}
interface IC<TA, TB, TC, TD>
where TA : IA
where TB : IB
where TC : IC
where TD : ID
{
TB Parent { get; }
List<TD> Children { get; }
}
interface ID<TA, TB, TC, TD>
where TA : IA
where TB : IB
where TC : IC
where TD : ID
{
TC Parent { get; }
}
At the moment it is implemented by casting in runtime, which I consider incorrect. It's like this:
class XA : IA
{
//...
}
class XB : IB
{
//...
public XA StronglyTypedParent { get { return (XA) this.Parent; } }
}
How should it be done? Is there a better way to achieve this? Maybe there exists a structural pattern for solving such problem? Any suggestions would be most welcome. Thank you.
Aucun commentaire:
Enregistrer un commentaire