| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ViewFactory.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using MyNet.UI.Locators.Conventions; |
| | | 9 | | |
| | | 10 | | namespace MyNet.UI.Locators.Factories; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Resolves a view type from a view model type, then instantiates it via <see cref="IViewLocator"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="resolver">The type resolver used to determine the view type for a given view model type.</param> |
| | | 16 | | /// <param name="viewLocator">The view locator used to retrieve an instance of the view.</param> |
| | | 17 | | public sealed class ViewFactory(ITypeResolver resolver, IViewLocator viewLocator) : IViewFactory |
| | | 18 | | { |
| | | 19 | | /// <inheritdoc /> |
| | | 20 | | public object CreateView(Type viewModelType) |
| | | 21 | | { |
| | 18 | 22 | | ArgumentNullException.ThrowIfNull(viewModelType); |
| | | 23 | | |
| | 18 | 24 | | var viewType = resolver.Resolve(viewModelType) |
| | 18 | 25 | | ?? throw ViewResolutionException.NoMapping(viewModelType); |
| | | 26 | | |
| | 15 | 27 | | return viewLocator.Get(viewType); |
| | | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public TView CreateView<TViewModel, TView>() |
| | | 32 | | where TViewModel : class |
| | | 33 | | where TView : class |
| | | 34 | | { |
| | 6 | 35 | | var view = CreateView(typeof(TViewModel)); |
| | 6 | 36 | | return view as TView ?? throw ViewResolutionException.ResolvedViewTypeMismatch(typeof(TViewModel), view.GetType( |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |