问题
In traditional MDI applications some objects (Forms) will be created when a command occurs (Ex. pressing a ribbon button), so it maybe a composition point. I'm confiused about composition root in such applications. I read somewhere that we can use a ViewModelLocator which looks like Service Locator pattern. As you know the service locator pattern is denounced by some people.
Now please advice me about this issue.
Thanks in advance.
回答1:
Whether or not a ViewModelLocator is a Service Locator or not depends partly on its signature, and partly on its role.
In any case, the Composition Root of a WPF application is the OnStartup method - whether or not an application is an MDI application doesn't change that.
Since, in an MDI application, you can have an arbitrary number of child windows, you'll most likely need an Abstract Factory to create each ViewModel.
来源:https://stackoverflow.com/questions/9079530/where-is-the-composition-root-in-a-wpf-mdi-application