Skip to content

Commit

Permalink
Improve registration exclusions (#32)
Browse files Browse the repository at this point in the history
* Added ignore attribute and also ignore abstracts

* Cast ignored types to immutable hashset

* Added demo project test for ignore atribute
  • Loading branch information
matt-goldman authored Jul 6, 2024
1 parent ff594da commit 88b83a2
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/DemoProject/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@
Command="{Binding GoToScopeCheckCommand}"
HorizontalOptions="Center" />

<Button
Text="Click to go to ignored dependency page (expect crash)"
FontAttributes="Bold"
Grid.Row="3"
SemanticProperties.Hint="Crashes the app!"
Command="{Binding GoToIgnoredPageCommand}"
HorizontalOptions="Center" />

<Image Grid.Row="4"
Source="dotnet_bot.png"
SemanticProperties.Description="Cute dot net bot waving hi to you!"
Expand Down
18 changes: 18 additions & 0 deletions src/DemoProject/Pages/BrokenPage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace DemoProject.Pages;

/// <summary>
/// This page has a dependency on a service that should not be registered. Runtime exception expected when navigating to this page.
/// </summary>
public class BrokenPage : ContentPage
{
public BrokenPage(IIgnoredService ignoredService)
{
Content = new VerticalStackLayout
{
Children = {
new Label { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, Text = ignoredService.GetHello()
}
}
};
}
}
14 changes: 14 additions & 0 deletions src/DemoProject/Services/IIgnoredService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Maui.Plugins.PageResolver.Attributes;

namespace DemoProject.Services;

public interface IIgnoredService
{
string GetHello();
}

[Ignore]
public class IgnoredService : IIgnoredService
{
public string GetHello() => "Hello";
}
7 changes: 7 additions & 0 deletions src/DemoProject/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public class MainViewModel : BaseViewModel

public ICommand GoToScopeCheckCommand => new Command(async () => await GoToScopeCheck());

public ICommand GoToIgnoredPageCommand => new Command(async () => await GoToBrokenPage());

public string Name { get; set; }

public MainViewModel(INameService nameService)
Expand Down Expand Up @@ -52,4 +54,9 @@ async Task GoToScopeCheck()
{
await Navigation.PushAsync<ScopeCheckPage>();
}

async Task GoToBrokenPage()
{
await Navigation.PushAsync<BrokenPage>();
}
}
4 changes: 4 additions & 0 deletions src/Maui.Plugins.PageResolver.Attributes/IgnoreAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace Maui.Plugins.PageResolver.Attributes;

[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class IgnoreAttribute : Attribute { }
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ private void InitialiseDependencies(IEnumerable<ITypeSymbol> types)
"using Maui.Plugins.PageResolver;"
};

var ignoredTypes = new HashSet<ITypeSymbol>(types.Where(type =>
type.GetAttributes().Any(ad =>
ad.AttributeClass.ToDisplayString() == "Maui.Plugins.PageResolver.Attributes.IgnoreAttribute")
|| type.IsAbstract), comparer);

var singletons = types.Where(type =>
type.GetAttributes().Any(ad =>
ad.AttributeClass.ToDisplayString() == "Maui.Plugins.PageResolver.Attributes.SingletonAttribute"));
Expand All @@ -197,13 +202,13 @@ private void InitialiseDependencies(IEnumerable<ITypeSymbol> types)
ad.AttributeClass.ToDisplayString() == "Maui.Plugins.PageResolver.Attributes.TransientAttribute"));
_dependencies["ExplicitTransients"] = new HashSet<ITypeSymbol>(transients, comparer);

var pages = types.Where(t => t.TypeKind == TypeKind.Class && t.Name.EndsWith("Page"));
var pages = types.Where(t => t.TypeKind == TypeKind.Class && t.Name.EndsWith("Page") && !ignoredTypes.Contains(t, comparer));
_dependencies["Pages"] = new HashSet<ITypeSymbol>(pages, comparer);

var viewModels = types.Where(t => t.TypeKind == TypeKind.Class && t.Name.EndsWith("ViewModel"));
var viewModels = types.Where(t => t.TypeKind == TypeKind.Class && t.Name.EndsWith("ViewModel") && !ignoredTypes.Contains(t, comparer));
_dependencies["ViewModels"] = new HashSet<ITypeSymbol>(viewModels, comparer);

var services = types.Where(t => t.TypeKind == TypeKind.Class && t.Name.EndsWith("Service"));
var services = types.Where(t => t.TypeKind == TypeKind.Class && t.Name.EndsWith("Service") && !ignoredTypes.Contains(t, comparer));
_dependencies["Services"] = new HashSet<ITypeSymbol>(services, comparer);

var abstractions = types.Where(t => t.TypeKind == TypeKind.Interface && t.Name.EndsWith("Service"));
Expand Down

0 comments on commit 88b83a2

Please sign in to comment.