I would create a extension methods for each called AddRelationship, passing in the other object as the parameter.
The object passed would add the relationship and then call the AddRelationship method of the other:
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 the relationship using either object.
ConfigManager cfg = new ConfigManager();
ErrorManager err = new ErrorManager();
//Assign using either:
err.AddRelationship(cfg);
//Or
cfg.AddRelationship(err);
You should 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 references are a particularly good coding practice, but this should solve the question as asked.