I have an Object class that allows objects to be linked together.
A -> B -> C (one way linking)
When a link is formed between two objects by calling Link(IObject other), I want to set IsReferenced on the Next object.
So in the above example, A references B, but B does not reference A, B just has it's IsReferenced property set to true.
However, I only want the IsReferenced setter to be available to classes that implement the IObject interface. i.e the IsReferenced should only be able to be set inside the Object class, outside code should not be able to set it.
However, the code fails to compile as IsReferenced when accessed via the interface is read only.
public interface IObject
{
bool IsReferenced { get; }
IObject? Next { get; }
void Link(IObject next);
}
public class Object : IObject
{
public bool IsReferenced { get; }
public IObject? Next { get; private set; }
public void Link(IObject next)
{
Next = next;
next.IsReferenced = true;
}
}
I can resolve the issue by doing the following :
public void Link(IObject next)
{
Next = next;
((Object)next).IsReferenced = true;
}
But this won't work for my scenario, as I need to be able to Mock<IObject>
Is there a better solution other than making the IsReferenced a public setter?
other.Link(this)?IsReferencedproperty off of that link reference existing or not.Link()if!IsReferenced