title: How do you alternate Ninject bindings based on user?
body: <p>This question requires a bit of context before it makes sense so I'll just start with a description of the project.</p>
  
  <h2>Project Background</h2>
  
  <p>I have an open source project which is a command-prompt style website (<a href="http://www.u413.com" rel="nofollow noreferrer">U413.com</a>, <a href="http://u413.googlecode.com" rel="nofollow noreferrer">U413.GoogleCode.com</a>). This project is built in ASP.NET MVC 3 and uses Entity Framework 4. Essentially the site allows you to pass in commands and arguments and then the site returns some data. The concept is fairly simple, but I didn't want to use one giant IF statement to handle the commands. Instead, I decided to do something somewhat unique and build an object that contains all the possible commands as methods on the object.</p>
  
  <p>The site uses reflection to locate methods that correspond to the sent command and execute them. This object is built dynamically based on the current user because some users have access to different commands than other users (e.g. Administrators have more than moderators, and mods have more than users, etc, etc).</p>
  
  <p>I built a custom <code>CommandModuleFactory</code> that would be created in the MVC controller and would call it's <code>BuildCommandModule</code> method to build out a command module object. I am now using Ninject for dependency injection and I want to phase out this <code>CommandModuleFactory</code>, in favor of having the <code>ICommandModule</code> injected into the controller without the controller doing any work.</p>
  
  <p><code>ICommandModule</code> has one method defined, like this:</p>
  
  <pre><code>public interface ICommandModule
  {
      object InvokeCommand(string command, List&lt;string&gt; args);
  }
  </code></pre>
  
  <p><code>InvokeCommand</code> is the method that performs the reflection over itself to find all methods that might match the passed in command.</p>
  
  <p>I then have five different objects that inherit from <code>ICommandModule</code> (some of them inherit from other modules as well so we don't repeat commands):</p>
  
  <p><code>AdministratorCommandModule</code> inherits from <code>ModeratorCommandModule</code> which inherits from <code>UserCommandModule</code> which inherits from <code>BaseCommandModule</code>.</p>
  
  <p>I then also have <code>VisitorCommandModule</code> which inherits from <code>BaseCommandModule</code> because visitors will not have access to any of the commands in the other three command modules.</p>
  
  <p>Hopefully you can start to see how this works. I'm pretty proud of the way this is all working so far.</p>
  
  <h2>The Question</h2>
  
  <p>I want Ninject to build my command module for me and bind it to <code>ICommandModule</code> so that I can just make my MVC controller dependent upon <code>ICommandModule</code> and it will receive the correct version of it. Here is what my Ninject module looks like where the binding takes place.</p>
  
  <pre><code>public class BuildCommandModule : NinjectModule
  {
      private bool _isAuthenticated;
      private User _currentUser;
  
      public BuildCommandModule(
          bool isAuthenticated,
          string username,
          IUserRepository userRepository
          )
      {
          this._isAuthenticated = isAuthenticated;
          this._currentUser = userRepository.GetUserBy_Username(username);
      }
  
      public override void Load()
      {
          if (_isAuthenticated)
              if (_currentUser.Administrator)
                  //load administrator command module
                  this.Bind&lt;ICommandModule&gt;().To&lt;AdministratorCommandModule&gt;();
              else if (_currentUser.Moderator)
                  //Load moderator command module
                  this.Bind&lt;ICommandModule&gt;().To&lt;ModeratorCommandModule&gt;();
              else
                  //Load user command module
                  this.Bind&lt;ICommandModule&gt;().To&lt;UserCommandModule&gt;();
          else
              //Load visitor command module
              this.Bind&lt;ICommandModule&gt;().To&lt;VisitorCommandModule&gt;();
      }
  }
  </code></pre>
  
  <p>A couple things are happening here. Firstly, the Ninject module depends on a few things. It depends on a boolean indicating whether or not the user is authenticated (to determine if it will be one of the logged in command modules, or the visitor command module). Next it depends on a string username and <code>IUserRepository</code>. Here is where my mappings are defined in Global.asax.</p>
  
  <pre><code>    protected override IKernel CreateKernel()
      {
          var kernel = new StandardKernel();
  
          kernel.Bind&lt;IBoardRepository&gt;().To&lt;BoardRepository&gt;();
          kernel.Bind&lt;IReplyRepository&gt;().To&lt;ReplyRepository&gt;();
          kernel.Bind&lt;ITopicRepository&gt;().To&lt;TopicRepository&gt;();
          kernel.Bind&lt;IUserRepository&gt;().To&lt;UserRepository&gt;();
  
          kernel.Load(new BuildCommandModule(User.Identity.IsAuthenticated, User.Identity.Name, kernel.Get&lt;IUserRepository&gt;()));
  
          return kernel;
      }
  </code></pre>
  
  <p>You can see that I map <code>IUserRepository</code> to its concrete type before I load the Ninject module to build my command module (try not to confuse Ninject binding modules with my command modules :S). I then use <code>kernel.Get&lt;IUserRepository&gt;()</code> to resolve my Ninject module's dependency on it.</p>
  
  <p>My problem here is that <code>HttpContext.Current.User</code> is null. I'm not sure how to tell whether or not a user is logged in during the Ninject binding phase. Any ideas?</p>
  
  <p>How might I get reference to the logged in user when I'm doing my Ninject bindings? Or can you think of a better way for me to do conditional binding for my <code>ICommandModule</code>?</p>