| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="TypeNamingConventionBase.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Linq; |
| | | 10 | | |
| | | 11 | | namespace MyNet.UI.Locators.Conventions; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Base class for view model ↔ view naming conventions. Subclass and override the two resolve methods. |
| | | 15 | | /// </summary> |
| | | 16 | | public abstract class TypeNamingConventionBase : ITypeNamingConvention |
| | | 17 | | { |
| | | 18 | | /// <inheritdoc /> |
| | 48 | 19 | | public Type? Resolve(Type source) => NamingConventionHelpers.IsViewModelType(source) |
| | 48 | 20 | | ? ResolveViewFromViewModel(source) |
| | 48 | 21 | | : NamingConventionHelpers.IsViewType(source) ? ResolveViewModelFromView(source) : null; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Loads a type from the same assembly as <paramref name="source"/>. |
| | | 25 | | /// </summary> |
| | | 26 | | protected static Type? GetType(Type source, string fullTypeName) => |
| | 42 | 27 | | NamingConventionHelpers.GetAssemblyType(source, fullTypeName); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Returns the first type that exists for the given fully qualified names. |
| | | 31 | | /// </summary> |
| | | 32 | | protected static Type? GetFirstMatchingType(Type source, IEnumerable<string> fullTypeNames) => |
| | 15 | 33 | | fullTypeNames |
| | 15 | 34 | | .Select(fullName => GetType(source, fullName)) |
| | 15 | 35 | | .OfType<Type>() |
| | 15 | 36 | | .FirstOrDefault(); |
| | | 37 | | |
| | | 38 | | /// <summary> |
| | | 39 | | /// Resolves a view type from a view model type. |
| | | 40 | | /// </summary> |
| | | 41 | | protected abstract Type? ResolveViewFromViewModel(Type source); |
| | | 42 | | |
| | | 43 | | /// <summary> |
| | | 44 | | /// Resolves a view model type from a view type. |
| | | 45 | | /// </summary> |
| | | 46 | | protected abstract Type? ResolveViewModelFromView(Type source); |
| | | 47 | | } |
| | | 48 | | |