| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SuffixConvention.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Linq; |
| | | 9 | | |
| | | 10 | | namespace MyNet.UI.Locators.Conventions; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Maps view models to views by swapping the <c>ViewModels</c>/<c>Views</c> namespace segment and trying multiple view |
| | | 14 | | /// Registered by default via <see cref="ServiceCollectionExtensions.AddViewLocators"/>. |
| | | 15 | | /// </summary> |
| | | 16 | | public sealed class SuffixConvention : TypeNamingConventionBase |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | protected override Type? ResolveViewFromViewModel(Type source) |
| | | 20 | | { |
| | 15 | 21 | | if (source.Namespace is null) |
| | 0 | 22 | | return null; |
| | | 23 | | |
| | 15 | 24 | | var baseName = NamingConventionHelpers.GetBaseNameFromViewModel(source); |
| | 15 | 25 | | var targetNs = NamingConventionHelpers.ReplaceNamespaceSegment(source.Namespace, "ViewModels", "Views"); |
| | | 26 | | |
| | 15 | 27 | | return GetFirstMatchingType( |
| | 15 | 28 | | source, |
| | 15 | 29 | | NamingConventionHelpers.ViewSuffixes.Select(suffix => $"{targetNs}.{baseName}{suffix}")); |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | protected override Type? ResolveViewModelFromView(Type source) |
| | | 34 | | { |
| | 6 | 35 | | if (source.Namespace is null) |
| | 0 | 36 | | return null; |
| | | 37 | | |
| | 6 | 38 | | var baseName = NamingConventionHelpers.GetBaseNameFromView(source); |
| | 6 | 39 | | var targetNs = NamingConventionHelpers.ReplaceNamespaceSegment(source.Namespace, "Views", "ViewModels"); |
| | | 40 | | |
| | 6 | 41 | | return GetType(source, $"{targetNs}.{baseName}ViewModel"); |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |