Reputation: 960
I would like an advice concerning architectural decision of c#, windows forms and unity IoC framework. Is it a good idea to keep forms within unity context? Or should just service layer and below layers be kept within it? And is here something like "depends on" in unity(something similar to spring depends on when there are beans depending on each other). Thanks in advance.
Upvotes: 0
Views: 808
Reputation: 172646
In general, you should try to let your DI framework build up the complete object graph for you.
With Windows Forms however, Form classes should have a default constructor because the platform creates those types for you. This means that you will have to resort to other mechanisms to build those classes up.
This same holds for Windows forms. Compared to ASP.NET Web Forms, Windows Forms does not require forms to have a default constructor. It is therefore good practice to let the container build up the complete object graph including the forms for you, because this allows the whole object graph to be verified and prevents your presentation layer from taking a dependency on the DI framework. This means you should use constructor injection in your forms:
public MainForm : Form
{
private readonly ILogger logger;
public MainForm(ILogger Logger)
{
this.logger = logger;
}
}
Upvotes: 2