title: Unity Container, Resolve Individual Objects
body: <p>I am beginning to learn about the Unity Container and Dependency Injection.  I'm having difficulty understanding what my object model should look like.</p>
  
  <p>For my example, I created a really simple Employee class (I've omitted the Constructor, because that's what I'm confused about):</p>
  
  <pre><code>public class Employee
  {
      private int _id;
      private string _name;
      private DateTime _birthDate;
  
      public int Id
      {
          get { return _id; }
      }
  
      public string Name
      {
          get { return _name; }
      }
  
      public DateTime BirthDate
      {
          get { return _birthDate; }
      }
  }
  </code></pre>
  
  <p>This Employee object should get its information from a database.  Here is a shim database adapter, just to develop a dependency:</p>
  
  <pre><code>public class DataAdapter
  {
      public DbParameter NewParameter(string name, object value)
      {
          return new OracleParameter(name, value);
      }
  
      public DataRow ExecuteDataRow(string command, CommandType commandType, List&lt;DbParameter&gt; parameters)
      {
          DataTable dt = new DataTable();
  
          dt.Columns.Add(new DataColumn("ID"));
          dt.Columns.Add(new DataColumn("NAME"));
          dt.Columns.Add(new DataColumn("BIRTH_DATE"));
  
          DataRow dr = dt.NewRow();
  
          dr["ID"] = new Random().Next();
          dr["NAME"] = "John Smith";
          dr["BIRTH_DATE"] = DateTime.Now;
  
          return dr;
      }
  }
  </code></pre>
  
  <p>Ideally, the employee object should take an "id" parameter in order to know which Employee to retrieve from the database.  Let's say that we use an Employee constructor that looks like this:</p>
  
  <pre><code>public Employee(int id, DataAdapter dataAdapter)
  {
      List&lt;DbParameter&gt; parameters = new List&lt;DbParameter&gt;();
  
      parameters.Add(dataAdapter.NewParameter("ID", id));
  
      DataRow dr = dataAdapter.ExecuteDataRow("GetEmployee", CommandType.StoredProcedure, parameters);
  
      if (dr == null)
          throw new EmployeeNotFoundException();
  
      _id = id;
      _name = Convert.ToString(dr["NAME"]);
      _birthDate = Convert.ToDateTime(dr["BIRTH_DATE"]);
  
      _id = employeeData.Id;
      _name = employeeData.Name;
      _birthDate = employeeData.BirthDate;
  }
  </code></pre>
  
  <p>I'm not sure how to specify the Employee's id with Unity's resolver except using ParameterOverride:</p>
  
  <pre><code>class Program
  {
      static void Main(string[] args)
      {
          UnityContainer container = new UnityContainer();
  
          container.RegisterType(typeof(EmployeeData));
  
          Employee emp = container.Resolve&lt;Employee&gt;(new ParameterOverride("id", 45));
  
          Console.WriteLine(emp.Id);
          Console.WriteLine(emp.Name);
          Console.WriteLine(emp.BirthDate);
          Console.ReadKey();
      }
  }
  </code></pre>
  
  <p>I don't like this because there is no compile-time checking to see if the parameter name is correct.  Problems like this make me think that I'm applying the pattern incorrectly.  Can anybody shed some light on what I am misunderstanding?</p>
  
  <p>Thanks!</p>