| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="AssemblyRootConvention.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>{AssemblyName}.{subNamespace}.{Name}View</c>. |
| | | 13 | | /// Opt-in via <see cref="ServiceCollectionExtensions.AddAssemblyRootConvention"/>. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="subNamespace">The sub-namespace segment for views (default: <c>UI.Views</c>).</param> |
| | 6 | 16 | | public sealed class AssemblyRootConvention(string subNamespace = "UI.Views") : TypeNamingConventionBase |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | | 19 | | protected override Type? ResolveViewFromViewModel(Type source) |
| | | 20 | | { |
| | 3 | 21 | | var assemblyName = source.Assembly.GetName().Name; |
| | 3 | 22 | | if (assemblyName is null) |
| | 0 | 23 | | return null; |
| | | 24 | | |
| | 3 | 25 | | var baseName = NamingConventionHelpers.GetBaseNameFromViewModel(source); |
| | 3 | 26 | | return GetType(source, $"{assemblyName}.{subNamespace}.{baseName}View"); |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | protected override Type? ResolveViewModelFromView(Type source) |
| | | 31 | | { |
| | 3 | 32 | | var assemblyName = source.Assembly.GetName().Name; |
| | 3 | 33 | | if (assemblyName is null) |
| | 0 | 34 | | return null; |
| | | 35 | | |
| | 3 | 36 | | var baseName = NamingConventionHelpers.GetBaseNameFromView(source); |
| | 3 | 37 | | return GetType(source, $"{assemblyName}.ViewModels.{baseName}ViewModel"); |
| | | 38 | | } |
| | | 39 | | } |
| | | 40 | | |