{"id":231,"date":"2010-02-26T15:34:38","date_gmt":"2010-02-26T20:34:38","guid":{"rendered":"http:\/\/hoolihan.net\/blog-tim\/?p=231"},"modified":"2010-02-27T11:00:12","modified_gmt":"2010-02-27T16:00:12","slug":"passing-in-this-in-structuremap","status":"publish","type":"post","link":"http:\/\/hoolihan.net\/blog-tim\/2010\/02\/26\/passing-in-this-in-structuremap\/","title":{"rendered":"Passing in &#8220;this&#8221; in StructureMap"},"content":{"rendered":"<p>I&#8217;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&#8230;<\/p>\n<pre>\r\n<code>namespace StateMachine\r\n{\r\n  public class StateRegistry : StructureMap.Configuration.DSL.Registry\r\n  {\r\n    public StateRegistry ()\r\n      {\r\n        For&lt;IState&gt;.Use&lt;StateA&gt;.Named(\"FirstState\");\r\n        For&lt;IState&gt;.Use&lt;StateB&gt;.Named(\"SecondState\");\r\n      }\r\n  }\r\n\r\n  public interface IStateMachine \r\n  {\r\n    void ChangeState(IState state);\r\n  }\r\n\r\n  public interface IState\r\n  {\r\n    void DoWork();\r\n  }\r\n\r\n  public class StateA : IState\r\n  {\r\n    IStateMachine _machine;\r\n    public StateA(IStateMachine machine)\r\n      {\r\n        _machine = machine;\r\n      }\r\n    public void DoWork() { \/\/ do stuff }\r\n  }\r\n\r\n  public class Machine : IStateMachine\r\n  {\r\n    public IState FirstState {get;set;}\r\n    public IState SecondState {get;set;}\r\n    public IState CurrentState {get; private set;}\r\n    public Machine()\r\n      {\r\n        FirstState = ObjectFactory.With&lt;IStateMachine&gt;(this).GetInstance&lt;IState&gt;(\"FirstState\");  \r\n        SecondState = ObjectFactory.With&lt;IStateMachine&gt;(this).GetInstance&lt;IState&gt;(\"SecondState\");  \r\n        ChangeState(FirstState);\r\n      }\r\n    public void ChangeState(IState state) { CurrentState = state; }\r\n  }\r\n}<\/code>\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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&#8230; namespace StateMachine { public class StateRegistry : StructureMap.Configuration.DSL.Registry { public StateRegistry () { For&lt;IState&gt;.Use&lt;StateA&gt;.Named(&#8220;FirstState&#8221;); For&lt;IState&gt;.Use&lt;StateB&gt;.Named(&#8220;SecondState&#8221;); } } public interface IStateMachine { void ChangeState(IState state); } public interface IState { void [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,18],"tags":[41,99,69,272],"class_list":["post-231","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-programming","tag-net","tag-ioc","tag-linkedin","tag-programming"],"_links":{"self":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/231","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/comments?post=231"}],"version-history":[{"count":0,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/posts\/231\/revisions"}],"wp:attachment":[{"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/media?parent=231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/categories?post=231"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/hoolihan.net\/blog-tim\/wp-json\/wp\/v2\/tags?post=231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}