So let's say I have two classes, Food and Barrel for example. Barrel contains an array of food private Food[] stores to track what's stored in it. Barrel also contains methods to access and modify stores, such as public void Insert(Food) and public void Remove(Food).
So Barrel can store Food. But I also want Food to know where it's stored in, if anywhere. For example, I want to have a method Food.Rot(), which only rots food if it's not stored.
My current solution is to have a property public Barrel Container { get; set; } in Food that tracks the instance of Barrel it's stored in, and have Barrel.Insert(Food toAdd) set toAdd.Container to itself and Barrel.Remove(Food toRemove) set toRemove.Container to null (under which condition Food.Rot() would actually rot the food).
However, any other class can also access Food.Container, and if they write to it, the original Barrel will still have this Food instance in stores, but Food.Container will no longer reflect that.
I can make the set method ensure that when Food.Container changes, it first calls Container.Remove(this), but I really don't want anything besides Barrel to be able to set Food.Container in the first place.
I could also make it so Food doesn't store its container at all and simply have Rot() call a method bool IsStored() to check every single Barrel.stores to see if the instance of the Food class that called it was stored inside, that way there's no two-way malarkey, but that seems just terrible for optimization.
It feels like somewhere along this design I've violated OOP and I expect that ideally this whole situation needs to be redesigned. I'm more than open to such feedback as well.