github / VisualStudio

GitHub Extension for Visual Studio
https://visualstudio.github.com
MIT License
2.36k stars 1.2k forks source link

Simplify the way ViewViewModelFactory uses MEF #1368

Open jcansdale opened 6 years ago

jcansdale commented 6 years ago

The ViewViewModelFactory class currently has dependencies on ICompositionService and IGitHubServiceProvider, which I'm not sure are necessary. It also uses AllowRecomposition = true, which I don't think is supported in Visual Studio's flavor of MEF.

I'd like to see if this can be simplified, so we don't confuse people looking at this class in future.

jcansdale commented 6 years ago

To remove the dependency on IGitHubServiceProvider, we would need to replace exports like this:

[Export(typeof(IMyViewModel))]

with something like:

[ExportViewModel(typeof(IMyViewModel))]

there would be a kind of symmetry with:

[ExportViewFor(typeof(IMyViewModel))]

The ExportViewModelAttribute would export IViewModel and allow a ExportFactory to be located using the defined type (IMyViewModel in the example above). We could then import the VM factories into ViewViewModelFactory like:

[ImportMany]
IEnumerable<ExportFactory<IViewModel, IViewModelMetadata>> Views { get; set; }

The CreateViewModel method would be similar to CreateView and not depend on IGitHubServiceProvider.

@grokys what are your thoughts on this?