I would create extension methods for each AddRelationship called, passing another object as this object.
The passed object will add the link, and then call the AddRelationship method of another:
static void AddRelationship(this ConfigManager configMgr, ErrorManager errMgr) { this.ErrorManager = errMgr; if (this != errMgr.ConfigManager) errMgr.AddRelationship(this); } static void AddRelationship(this ErrorManager errMgr, ConfigManager configMgr) { this.ConfigManager = configMgr; if (this != configManager.errMgr) configMgr.AddRelationship(this); }
This means that you can add relationships using any object.
ConfigManager cfg = new ConfigManager(); ErrorManager err = new ErrorManager();
You must also create RemoveRelationship extensions.
static void RemoveRelationship(this ConfigManager configMgr, ErrorManager errMgr) { if (this.errorManager == errMgr) { this.errorManager = null; if (errManager.configManager == this) errMgr.RemoveRelationship(this); } } static void RemoveRelationship(this ErrorManager errMgr, ConfigManager cfgMgr) { if (this.ConfigManager == cfgMgr) { this.configManager = null; if (cfgMgr.errorManager == this) cfgMgr.RemoveRelationship(this); } }
I donβt know that circular links are a particularly good coding practice, but this should solve the problem on a mission.
Benalabaster
source share