vonndutch
vonndutch

Reputation: 43

Dependency Injection in WindowsForm Application C#

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.

This is the partial class of the Main form:

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();

        <code mapping here>

        _tasks.CreateAssignments(request); --> _tasks here is null
    }
}

Here is the class that is inheriting the IServiceTasks interface

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(); --> _task here is null
        return true;
    }
}

Here is my IServiceTasks Interface

public interface IServiceTasks
{
    bool CreateAssignment();
}

And for the IInterfaceTasks Interface

public interface IInterfaceTasks
{
    bool CreateAssignments(IEntities.AssignmentEntityRequest request);
}

Can somebody help me how can I implement this right?

update: pws is being called here

[STAThread]
    static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        IInterfaceTasks tasks = new pws();
        Application.Run(new pws(tasks));
    }

Upvotes: 3

Views: 15942

Answers (1)

Cameron
Cameron

Reputation: 2594

Your issue is that you're creating a windows form and not calling InitializeComponent() in the default constructor.

So your pws class needs a single constructor like this:

    public pws(IInterfaceTasks tasks)
    {
        InitializeComponent();
        _tasks = tasks;
    }

Or your multiple constructors like this:

    public pws()
    {
        InitializeComponent();
    }
    public pws(IInterfaceTasks tasks) : this()
    {
        _tasks = tasks;
    }

I highly recommend the first option, since you're not using a service locator pattern (from what I infer/assume). That's a whole different topic that's not worth diving into.

This is a classic example of constructor overloading. In your Main() method, you'll need to get/instanciate an instance of the IInterfaceTasks implementation and pass it into the pws constructor.

So your code will look like this:

[STAThread]
static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    IInterfaceTasks interfaceTasks = new **SOME CLASS THAT IMPLEMENTS IInterfaceTasks**
    Application.Run(new pws(interfaceTasks));
}

Upvotes: 8

Related Questions