title: structuremap: Setting all properties of an object using setter injection
body: <p>I am using <a href="http://structuremap.github.io" rel="nofollow noreferrer">structuremap.net</a> as my container. I want to pass all dependencies of a class as a dependency object instead of one parameter per dependency.</p>
  
  <pre><code>// Dependencies of Controller
  class Dependencies {
      public IUserDatabase UserDatabase { get; set; }
      public IProductDatabase ProductDatabase { get; set; }
      // more dependencies ...
  }
  
  class Controller {
      Dependencies _d;
  
      Controller(Dependencies dependencies){
          this._d = dependencies;
      }
  
      public ActionResult Action() {
          var user = _this._d.GetUserById(10);
          var product = _this._d.GetProductById(20);
          // ...
      }
  }
  </code></pre>
  
  <p>How can I set all dependencies using setter injection (<code>IUserDatabase</code>, <code>IProductDatabase</code> etc.) automatically when building the instance of the class <code>Dependencies</code> via structuremap?</p>
  
  <pre><code>var container = new Container();
  // How to configure this in a generic way?
  var dependencies = container.GetInstance&lt;Dependencies&gt;();
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <p>My first example doesn't seem to illustrate why I want setter injection for a complete object and why this is not a bad design choice.</p>
  
  <p>We have a custom Web Framework which takes a lot of principles from ASP.NET MVC but also differs in a lot of ways. Like ASP.NET MVC we also have actions and controllers and group actions within controllers. Typical code looks like this:</p>
  
  <pre><code>class MyController {
      public IUserDatabase _userDatabase; 
      public IProductDatabase _productDatabase;
      public IRatingDatabase _ratingDatabase;
      public ICommentsDatabase _commentsDatabase; 
  
      MyController(IUserDatabase userDatabase, IUserDatabase productDatabase, IRatingDatabase ratingDatabase, ICommentsDatabase commentsDatabase){
          _userDatabase = userDatabase;
          _productDatabase = productDatabase;
          _ratingDatabase = ratingDatabase;
          _commentsDatabase = commentsDatabase;
      }
  
      public ActionResult Action1(){
          var user = _userDatabase.GetById(10);
          var product = _productDatabase.GetById(20);
      }
  
      public ActionResult Action2() {
          var ratings = _ratingDatabase.GetById(20);
          var comments = _commentsDatabase.GetById(20);
      }
  }
  </code></pre>
  
  <p>This contoller has two actions. None of these actions is using all dependencies of the controller. Furthermore there is a lot of noise for passing the dependencies to the controller via constructor injection.</p>
  
  <p>Instead, I would like to pass the dependencies of an action to that action directly as a parameter. We have our own routing that would easily support this.</p>
  
  <pre><code>class Action1Dependencies {
      public IUserDatabase UserDatabase {get; set;}
      public IProductDatabase ProductDatabase {get; set;}
  }
  
  class Action2Dependencies {
      public IRatingDatabase RatingDatabase  {get; set;}
      public ICommentsDatabase CommentsDatabase  {get; set;}
  }
  
  class MyController {
      public static ActionResult Action1(Action1Dependencies d){
          var user = d.UserDatabase.GetById(10);
          var product = d.ProductDatabase.GetById(20);
      }   
  
      public static ActionResult Action2(Action2Dependencies d) {
          var ratings = d.RatingDatabase.GetById(20);
          var comments = d.CommentsDatabase.GetById(20);
      }
  }
  </code></pre>
  
  <p>Therefore I would like to make a call like <code>var dependencies = container.GetInstance&lt;Action2Dependencies&gt;();</code> and have StructureMap set all properties of <code>Action2Dependencies</code> via setter injection.</p>
  
  <p>With C# 7 and support for <a href="https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/" rel="nofollow noreferrer">tuples</a> the code above could also possibly become</p>
  
  <pre><code>class MyController {
      public static ActionResult Action1((IUserDatabase UserDatabase, IProductDatabase ProductDatabase) d){
          var user = d.UserDatabase.GetById(10);
          var product = d.ProductDatabase.GetById(20);
      }   
  
      public static ActionResult Action2((IRatingDatabase RatingDatabase, ICommentsDatabase CommentsDatabase) d) {
          var ratings = d.RatingDatabase.GetById(20);
          var comments = d.CommentsDatabase.GetById(20);
      }
  }
  </code></pre>