Passing in “this” in StructureMap

I’ve been working with StructureMap lately, and struggled when I need to pass this in. The following code is an example of resolving that issue…

namespace StateMachine
{
  public class StateRegistry : StructureMap.Configuration.DSL.Registry
  {
    public StateRegistry ()
      {
        For<IState>.Use<StateA>.Named("FirstState");
        For<IState>.Use<StateB>.Named("SecondState");
      }
  }

  public interface IStateMachine 
  {
    void ChangeState(IState state);
  }

  public interface IState
  {
    void DoWork();
  }

  public class StateA : IState
  {
    IStateMachine _machine;
    public StateA(IStateMachine machine)
      {
        _machine = machine;
      }
    public void DoWork() { // do stuff }
  }

  public class Machine : IStateMachine
  {
    public IState FirstState {get;set;}
    public IState SecondState {get;set;}
    public IState CurrentState {get; private set;}
    public Machine()
      {
        FirstState = ObjectFactory.With<IStateMachine>(this).GetInstance<IState>("FirstState");  
        SecondState = ObjectFactory.With<IStateMachine>(this).GetInstance<IState>("SecondState");  
        ChangeState(FirstState);
      }
    public void ChangeState(IState state) { CurrentState = state; }
  }
}