title: Passing parameters/properties with MEF
body: <p>Consider the following sample:<br>
  I have a <code>Car</code> class and an <code>ILogger</code> interface. I'd like the <code>ILogger</code> implementations to log (console, file, db, etc) a list of cars' specs. And of course, I want to use MEF for ILogger. But for logging, my logger class should have access to a car's specification. And I'm not sure what would be the best way to pass objects to my logger classes. To be more clear, here's some code:  </p>
  
  <pre><code>class Program
  {
      public class Car
      {
          public string Brand { get; set; }
          public string Model { get; set; }
      }
  
      private CompositionContainer _container;
  
      [Import(typeof(ILogger))]
      public ILogger logger;
  
      private Program()
      {
          var catalog = new AggregateCatalog();
          catalog.Catalogs.Add(new AssemblyCatalog(typeof(Program).Assembly));
          this._container = new CompositionContainer(catalog);
          this._container.ComposeParts(this);
      }  
  </code></pre>
  
  <p>The <code>ILogger</code> interface should get a <code>Car</code> object for logging. And I'm not sure whether I should pass it in logger's constructor or as a Property, or just a method parameter. </p>
  
  <ul>
  <li><strong>Using property</strong>: If I use a property for passing a car, the <code>ILogger</code> interface looks like this:
  <pre>
  public interface ILogger
  {
      Car Car { get; set; }
      void Log();
  }
  </pre><br>
  And I can iterate through my list items as below:<br>
  <pre>
  static void Main(string[] args)
  {
      Program p = new Program(); // Composition is performed in the constructor
      var cars = new List()
          {
              new Car() { Brand = "Nissan", Model = "SkyLine" },
              new Car() { Brand = "Porche", Model = "Carrera"},
              new Car() { Brand = "Ferrari", Model = "Enzo"}
          };
      foreach (var car in cars)
      {
          p.logger.Car = car;
          p.logger.Log();
      }
      Console.ReadKey();
  }
  </pre>  </li>
  <li><p><strong>Using parameter</strong>:  The interface:</p>
  
  <pre>
  public interface ILogger
  {
      void Log(Car car);
  }
  </pre>  
  
  <p>and the program:  </p>
  
  <pre>
  foreach (var car in cars)
  {
      p.logger.Log(car);
  }
  </pre></li>
  <li><p><strong>Using constructor</strong>: This is where I seem to be lost. I can change the <code>ILogger</code> interface to :</p>
  
  <pre>
  public interface ILogger
  {
      Car Car {get; set;}
      void Log();
  }
  </pre>  
  
  <p>And in the implemented class:  </p>
  
  <pre>
  [Export(typeof(ICarLogger))]
  public class ConsoleLogger : ICarLogger
  {
      public Car Car { get; set; }
      [ImportingConstructor]
      public ConsoleLogger(Car car)
      { 
          this.Car = car;
      }
      public void Log()
      {
          Console.WriteLine("Brand: {0}\tModel: {1}", Car.Brand, Car.Model);
      }
  }
  </pre>  </li>
  </ul>
  
  <p><strong>But</strong> this leads to creating the <code>CompositionContainer</code> everytime I want to pass a new value to the constructor (By using <code>ComposeExportedValue</code> method). Put it simply, How can I pass various variables to the constructor without having to rebuild the whole <code>CompositionContainer</code> again?   </p>
  
  <p>So, which of these 3 approaches are better in your opinion. And how can I implement the <em>constructor</em> approach as mentioned? Hope I described my problem clearly.<br>
  Thanks and apologies for the lengthy question.<br>
  P.S. I'm using the MEF version which comes with .Net 4.</p>