title: Dependency Injection in WindowsForm Application C#
body: <p>i am currently creating the structure of a windows form application. I encountered the NULL REFERENCE Error when implementing dependency injection. I was using this kind of approach in my MVC applications for easy unit testing but cant figure it out how to implement it here in windows form application.</p>
  
  <p>This is the partial class of the Main form:</p>
  
  <pre><code>public partial class pws : Form
  {
      private IInterfaceTasks _tasks { get; set; }
      public pws(IInterfaceTasks tasks)
      {
          InitializeComponent();
          _tasks = tasks;
      }
  
      private void cmdSave_Click(object sender, EventArgs e)
      {
  
          IEntities.AssignmentEntityRequest request = new IEntities.AssignmentEntityRequest();
  
          &lt;code mapping here&gt;
  
          _tasks.CreateAssignments(request); --&gt; _tasks here is null
      }
  }
  </code></pre>
  
  <p>Here is the class that is inheriting the IServiceTasks interface</p>
  
  <pre><code>public class CreateAssignmentsInterface : IInterfaceTasks
  {
      private Service.IServiceTasks _task;
  
      public CreateAssignmentsInterface(Service.IServiceTasks task)
      {
          _task = task;
      }
  
      public bool CreateAssignments(IEntities.AssignmentEntityRequest request)
      {
          Helpers.Helpers mapper = new Helpers.Helpers();
          var assignmentRequest = new SEntities.AssignmentEntityRequest();
          assignmentRequest = mapper.MapInterfacetoServiceRequest(request);
  
          _task.CreateAssignment(); --&gt; _task here is null
          return true;
      }
  }
  </code></pre>
  
  <p>Here is my IServiceTasks Interface</p>
  
  <pre><code>public interface IServiceTasks
  {
      bool CreateAssignment();
  }
  </code></pre>
  
  <p>And for the IInterfaceTasks Interface</p>
  
  <pre><code>public interface IInterfaceTasks
  {
      bool CreateAssignments(IEntities.AssignmentEntityRequest request);
  }
  </code></pre>
  
  <p>Can somebody help me how can I implement this right?</p>
  
  <p>update:
  pws is being called here</p>
  
  <pre><code>[STAThread]
      static void Main()
      {
          Application.EnableVisualStyles();
          Application.SetCompatibleTextRenderingDefault(false);
          IInterfaceTasks tasks = new pws();
          Application.Run(new pws(tasks));
      }
  </code></pre>