I just started using the Unity application block to separate my classes and simplify unit testing. However, I had a problem with circular dependencies.
I have a facade type class that is a chat bot. This is a singleton class that handles all kinds of secondary classes and provides a central place to launch and configure the bot. I also have a class called AccessManager that, well, controls access to bot commands and resources. Restraining myself to the point, I have classes configured like this:
public class Bot
{
public string Owner { get; private set; }
public string WorkingDirectory { get; private set; }
private IAccessManager AccessManager;
private Bot()
{
LoadConfig();
AccessManager = new MyAccessManager(this);
}
public static Bot Instance()
{
}
...
}
And the AccessManager class:
public class MyAccessManager : IAccessManager
{
private Bot botReference;
public MyAccesManager(Bot botReference)
{
this.botReference = botReference;
SetOwnerAccess(botReference.Owner);
}
private void LoadConfig()
{
string configPath = Path.Combine(
botReference.WorkingDirectory,
"access.config");
}
...
}
, Unity Application Block. Unity Singleton One Bot AccessManager - , , - .
public static void BootStrapSystem()
{
IUnityContainer container = new UnityContainer();
Bot newBot = Bot.Instance();
container.RegisterInstance<Bot>(newBot);
container.RegisterType<IAccessManager,MyAccessManager>(newBot);
}
Access Manager Bot, :
IAcessManager accessManager = container.Resolve<IAccessManager>();
, Bottonton:
// do this
Bot botInstance = container.Resolve<Bot>();
// instead of this
Bot botInstance = Bot.Instance();
, BootStrapSystem() . , IAccessManager, , ( ). Bot, Bot ! !! !!!
, , . ? !!