I would like to ask someone who can help in introduce dependency injection into sample WinForm app. We have an application with following structure (not complete graph):
MainForm <-> MainModel
| |└ -----------------------┐
| └ ------------┐ |
FA <-> MA FB <-> MB FC <-> MC
| | |└-----------------┐
| └-----------┐ └----┐ |
FAA <-> MAA FAB <-> MAB FCA <-> MCA FCB <-> MCB
|||
┌--------------┘|└----------------┐
FCAA <-> MCAA FCAB <-> MCAB FCAC <-> MCAC
||
┌--------------┘└----------------┐
FCAAA <-> MCAAA FAB <-> MAB
I can implement composition root where I can type something like this:
var container = new Container();
container.RegisterSingle<IMainForm, MainForm>();
container.Register<IMainModel, MainModel>();
container.Register<IFA, FA>();
...
I know how to get the first MainForm and show it (simple resovling). But I am still struggling with proper logic how to create other forms and models on demand. I do not want to pass reference to my container into mainform and resolve dependencies in mainform because it smells as servicelocator pattern. I would like to avoid to create obscure Factory with methods as CreateFA, CreateFB, CreateFAAA... I would like to avoid to pass complete constructed graph into mainform too.
Furthermore I would like to use in MCAA some implementation of an interface IJob which can be defined in composition root, but I would like to avoid to pass instance of IJob thru all levels of forms or passing some kind of factory...
The easiest way how to create all instances of windows and models and ect. as I need them is using ServiceLocator... But, well... What about DI?
Preferred DI: SimpleInjector, Prefered language: C#