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