| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ParentNamespaceConvention.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 under <c>{ParentNamespace}.Views</c> and <c>{ParentNamespace}.ViewModels</c>. |
| | | 13 | | /// Opt-in via <see cref="ServiceCollectionExtensions.AddParentNamespaceConvention"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | public sealed class ParentNamespaceConvention : TypeNamingConventionBase |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | protected override Type? ResolveViewFromViewModel(Type source) |
| | | 19 | | { |
| | 3 | 20 | | var parentNs = NamingConventionHelpers.GetParentNamespace(source.Namespace); |
| | 3 | 21 | | if (parentNs is null) |
| | 0 | 22 | | return null; |
| | | 23 | | |
| | 3 | 24 | | var baseName = NamingConventionHelpers.GetBaseNameFromViewModel(source); |
| | 3 | 25 | | return GetType(source, $"{parentNs}.Views.{baseName}View"); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | protected override Type? ResolveViewModelFromView(Type source) |
| | | 30 | | { |
| | 3 | 31 | | var parentNs = NamingConventionHelpers.GetParentNamespace(source.Namespace); |
| | 3 | 32 | | if (parentNs is null) |
| | 0 | 33 | | return null; |
| | | 34 | | |
| | 3 | 35 | | var baseName = NamingConventionHelpers.GetBaseNameFromView(source); |
| | 3 | 36 | | return GetType(source, $"{parentNs}.ViewModels.{baseName}ViewModel"); |
| | | 37 | | } |
| | | 38 | | } |
| | | 39 | | |