< Summary

Information
Class: MyNet.UI.Locators.ViewLocator
Assembly: MyNet.UI
File(s): https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Locators/ViewLocator.cs
Tag: 323_28699572109
Line coverage
63%
Covered lines: 7
Uncovered lines: 4
Coverable lines: 11
Total lines: 52
Line coverage: 63.6%
Branch coverage
75%
Covered branches: 3
Total branches: 4
Branch coverage: 75%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
Get(...)75%5460%
Get()100%11100%

File(s)

https://raw.githubusercontent.com/sandre58/MyNet/85372080fe102cd9ee155ceab49ae000e7f66103/src/MyNet.UI/Locators/ViewLocator.cs

#LineLine coverage
 1// -----------------------------------------------------------------------
 2// <copyright file="ViewLocator.cs" company="Stéphane ANDRE">
 3// Copyright (c) Stéphane ANDRE. All rights reserved.
 4// </copyright>
 5// -----------------------------------------------------------------------
 6
 7using System;
 8
 9namespace MyNet.UI.Locators;
 10
 11/// <summary>
 12/// Implements the IViewLocator interface to provide a mechanism for locating and instantiating views based on their typ
 13/// </summary>
 14/// <param name="provider">The service provider used to resolve view instances.</param>
 15public sealed class ViewLocator(IServiceProvider provider) : IViewLocator
 16{
 17    /// <summary>
 18    /// Retrieves a view instance based on the specified view type. The locator first attempts to resolve the view type 
 19    /// </summary>
 20    /// <param name="viewType">The type of the view to retrieve.</param>
 21    /// <returns>The view instance.</returns>
 22    public object Get(Type viewType)
 23    {
 2724        ArgumentNullException.ThrowIfNull(viewType);
 25
 2426        if (provider.GetService(viewType) is { } instance)
 327            return instance;
 28
 29        try
 30        {
 2131            return Activator.CreateInstance(viewType)
 2132                   ?? throw ViewResolutionException.CannotInstantiateView(viewType);
 33        }
 034        catch (ViewResolutionException)
 35        {
 036            throw;
 37        }
 038        catch (Exception ex)
 39        {
 040            throw ViewResolutionException.CannotInstantiateView(viewType, ex);
 41        }
 2142    }
 43
 44    /// <summary>
 45    /// Retrieves a view instance of the specified generic type. This method provides a type-safe way to retrieve views 
 46    /// </summary>
 47    /// <typeparam name="T">The type of the view to retrieve.</typeparam>
 48    /// <returns>The view instance of type T.</returns>
 49    public T Get<T>()
 950        where T : class => (T)Get(typeof(T));
 51}
 52

Methods/Properties

Get(System.Type)
Get()