Search Results

Search found 1 results on 1 pages for 'onisemus'.

Page 1/1 | 1 

  • Contructor parameters for dependent classes with Unity Framework

    - by Onisemus
    I just started using the Unity Application Block to try to decouple my classes and make it easier for unit testing. I ran into a problem though that I'm not sure how to get around. Looked through the documentation and did some Googling but I'm coming up dry. Here's the situation: I have a facade-type class which is a chat bot. It is a singleton class which handles all sort of secondary classes and provides a central place to launch and configure the bot. I also have a class called AccessManager which, well, manages access to bot commands and resources. Boiled down to the essence, I have the classes set up like so. public class Bot { public string Owner { get; private set; } public string WorkingDirectory { get; private set; } private IAccessManager AccessManager; private Bot() { // do some setup // LoadConfig sets the Owner & WorkingDirectory variables LoadConfig(); // init the access mmanager AccessManager = new MyAccessManager(this); } public static Bot Instance() { // singleton code } ... } 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"); // do stuff to read from config file } ... } I would like to change this design to use the Unity Application Block. I'd like to use Unity to generate the Bot singleton and to load the AccessManager interface in some sort of bootstrapping method that runs before anything else does. public static void BootStrapSystem() { IUnityContainer container = new UnityContainer(); // create new bot instance Bot newBot = Bot.Instance(); // register bot instance container.RegisterInstance<Bot>(newBot); // register access manager container.RegisterType<IAccessManager,MyAccessManager>(newBot); } And when I want to get a reference to the Access Manager inside the Bot constructor I can just do: IAcessManager accessManager = container.Resolve<IAccessManager>(); And elsewhere in the system to get a reference to the Bot singleton: // do this Bot botInstance = container.Resolve<Bot>(); // instead of this Bot botInstance = Bot.Instance(); The problem is the method BootStrapSystem() is going to blow up. When I create a bot instance it's going to try to resolve IAccessManager but won't be able to because I haven't registered the types yet (that's the next line). But I can't move the registration in front of the Bot creation because as part of the registration I need to pass the Bot as a parameter! Circular dependencies!! Gah!!! This indicates to me I have a flaw in the way I have this structured. But how do I fix it? Help!!

    Read the article

1