diff --git a/.editorconfig b/.editorconfig
new file mode 100644
index 0000000..58f1f4f
--- /dev/null
+++ b/.editorconfig
@@ -0,0 +1,137 @@
+ο»Ώ[*.cs]
+
+# CA2254: Template should be a static expression
+dotnet_diagnostic.CA2254.severity = none
+csharp_indent_labels = one_less_than_current
+csharp_using_directive_placement = outside_namespace:error
+csharp_prefer_simple_using_statement = true:error
+csharp_prefer_braces = true:silent
+csharp_style_namespace_declarations = file_scoped:error
+csharp_style_prefer_method_group_conversion = true:suggestion
+csharp_style_expression_bodied_methods = false:silent
+csharp_style_expression_bodied_constructors = false:silent
+csharp_style_expression_bodied_operators = false:silent
+csharp_style_expression_bodied_properties = true:silent
+csharp_style_expression_bodied_indexers = true:silent
+csharp_style_expression_bodied_accessors = true:silent
+csharp_style_expression_bodied_lambdas = true:silent
+csharp_style_expression_bodied_local_functions = false:silent
+csharp_indent_braces = false
+insert_final_newline = true
+csharp_space_around_binary_operators = before_and_after
+csharp_style_throw_expression = true:suggestion
+csharp_style_prefer_null_check_over_type_check = true:suggestion
+csharp_prefer_simple_default_expression = true:suggestion
+csharp_style_prefer_local_over_anonymous_function = true:error
+csharp_style_prefer_index_operator = true:suggestion
+csharp_style_prefer_range_operator = true:suggestion
+csharp_style_implicit_object_creation_when_type_is_apparent = true:suggestion
+csharp_style_prefer_tuple_swap = true:suggestion
+csharp_style_inlined_variable_declaration = true:suggestion
+csharp_style_deconstructed_variable_declaration = true:error
+csharp_style_unused_value_assignment_preference = discard_variable:error
+csharp_style_unused_value_expression_statement_preference = discard_variable:silent
+csharp_prefer_static_local_function = true:suggestion
+csharp_style_allow_embedded_statements_on_same_line_experimental = true:silent
+csharp_style_allow_blank_line_after_colon_in_constructor_initializer_experimental = true:silent
+csharp_style_allow_blank_lines_between_consecutive_braces_experimental = false:suggestion
+csharp_style_conditional_delegate_call = true:suggestion
+csharp_style_prefer_parameter_null_checking = true:suggestion
+csharp_style_prefer_switch_expression = true:suggestion
+csharp_style_prefer_pattern_matching = true:silent
+csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion
+csharp_style_prefer_not_pattern = true:suggestion
+csharp_style_pattern_matching_over_as_with_null_check = true:suggestion
+csharp_style_prefer_extended_property_pattern = true:suggestion
+csharp_style_var_for_built_in_types = false:silent
+csharp_style_var_when_type_is_apparent = false:silent
+csharp_style_var_elsewhere = false:silent
+csharp_style_prefer_top_level_statements = true:suggestion
+csharp_style_prefer_utf8_string_literals = true:suggestion
+csharp_style_prefer_readonly_struct = true:suggestion
+
+[*.{cs,vb}]
+#### Naming styles ####
+
+# Naming rules
+
+dotnet_naming_rule.interface_should_be_begins_with_i.severity = error
+dotnet_naming_rule.interface_should_be_begins_with_i.symbols = interface
+dotnet_naming_rule.interface_should_be_begins_with_i.style = begins_with_i
+
+dotnet_naming_rule.types_should_be_pascal_case.severity = error
+dotnet_naming_rule.types_should_be_pascal_case.symbols = types
+dotnet_naming_rule.types_should_be_pascal_case.style = pascal_case
+
+dotnet_naming_rule.non_field_members_should_be_pascal_case.severity = error
+dotnet_naming_rule.non_field_members_should_be_pascal_case.symbols = non_field_members
+dotnet_naming_rule.non_field_members_should_be_pascal_case.style = pascal_case
+
+# Symbol specifications
+
+dotnet_naming_symbols.interface.applicable_kinds = interface
+dotnet_naming_symbols.interface.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
+dotnet_naming_symbols.interface.required_modifiers =
+
+dotnet_naming_symbols.types.applicable_kinds = class, struct, interface, enum
+dotnet_naming_symbols.types.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
+dotnet_naming_symbols.types.required_modifiers =
+
+dotnet_naming_symbols.non_field_members.applicable_kinds = property, event, method
+dotnet_naming_symbols.non_field_members.applicable_accessibilities = public, internal, private, protected, protected_internal, private_protected
+dotnet_naming_symbols.non_field_members.required_modifiers =
+
+# Naming styles
+
+dotnet_naming_style.begins_with_i.required_prefix = I
+dotnet_naming_style.begins_with_i.required_suffix =
+dotnet_naming_style.begins_with_i.word_separator =
+dotnet_naming_style.begins_with_i.capitalization = pascal_case
+
+dotnet_naming_style.pascal_case.required_prefix =
+dotnet_naming_style.pascal_case.required_suffix =
+dotnet_naming_style.pascal_case.word_separator =
+dotnet_naming_style.pascal_case.capitalization = pascal_case
+
+dotnet_naming_style.pascal_case.required_prefix =
+dotnet_naming_style.pascal_case.required_suffix =
+dotnet_naming_style.pascal_case.word_separator =
+dotnet_naming_style.pascal_case.capitalization = pascal_case
+dotnet_style_operator_placement_when_wrapping = beginning_of_line
+tab_width = 4
+indent_size = 4
+end_of_line = crlf
+dotnet_style_coalesce_expression = true:suggestion
+dotnet_style_null_propagation = true:suggestion
+dotnet_style_prefer_is_null_check_over_reference_equality_method = true:error
+dotnet_style_prefer_auto_properties = true:error
+dotnet_style_object_initializer = true:suggestion
+dotnet_style_collection_initializer = true:suggestion
+dotnet_style_prefer_simplified_boolean_expressions = true:suggestion
+dotnet_style_prefer_conditional_expression_over_assignment = true:silent
+dotnet_style_prefer_conditional_expression_over_return = true:silent
+dotnet_style_explicit_tuple_names = true:suggestion
+dotnet_style_prefer_inferred_tuple_names = true:suggestion
+dotnet_style_prefer_inferred_anonymous_type_member_names = true:suggestion
+dotnet_style_prefer_compound_assignment = true:suggestion
+dotnet_style_prefer_simplified_interpolation = true:suggestion
+dotnet_style_namespace_match_folder = true:suggestion
+dotnet_style_readonly_field = true:suggestion
+dotnet_style_predefined_type_for_locals_parameters_members = true:silent
+dotnet_style_predefined_type_for_member_access = true:silent
+dotnet_style_require_accessibility_modifiers = for_non_interface_members:silent
+dotnet_style_allow_multiple_blank_lines_experimental = true:silent
+dotnet_style_allow_statement_immediately_after_block_experimental = false:suggestion
+dotnet_code_quality_unused_parameters = all:error
+dotnet_style_parentheses_in_arithmetic_binary_operators = always_for_clarity:suggestion
+dotnet_style_parentheses_in_other_binary_operators = always_for_clarity:suggestion
+dotnet_style_parentheses_in_relational_binary_operators = always_for_clarity:suggestion
+dotnet_style_parentheses_in_other_operators = never_if_unnecessary:suggestion
+dotnet_style_qualification_for_field = true:error
+dotnet_style_qualification_for_property = true:error
+dotnet_style_qualification_for_method = true:error
+dotnet_style_qualification_for_event = true:error
+dotnet_diagnostic.CA1000.severity = error
+
+[*.vb]
+dotnet_diagnostic.CA1047.severity = error
\ No newline at end of file
diff --git a/.github/dependabot.yml b/.github/dependabot.yml
new file mode 100644
index 0000000..32f21e9
--- /dev/null
+++ b/.github/dependabot.yml
@@ -0,0 +1,16 @@
+# To get started with Dependabot version updates, you'll need to specify which
+# package ecosystems to update and where the package manifests are located.
+# Please see the documentation for all configuration options:
+# https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
+
+version: 2
+updates:
+ - package-ecosystem: "github-actions" # See documentation for possible values
+ directory: "/" # Location of package manifests
+ schedule:
+ interval: "weekly"
+
+ - package-ecosystem: "nuget"
+ directory: "/"
+ schedule:
+ interval: "daily"
diff --git a/.github/workflows/cd.yaml b/.github/workflows/cd.yaml
new file mode 100644
index 0000000..c8b1d07
--- /dev/null
+++ b/.github/workflows/cd.yaml
@@ -0,0 +1,43 @@
+name: TrailBlazr CD Pipeline
+
+on:
+ push:
+ branches:
+ - master
+
+jobs:
+
+ build:
+ environment: Default
+ strategy:
+ matrix:
+ targetplatform: [x64]
+
+ runs-on: windows-latest
+
+ env:
+ Configuration: Release
+ Actions_Allow_Unsecure_Commands: true
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v4
+ with:
+ fetch-depth: 0
+
+ - name: Install .NET Core
+ uses: actions/setup-dotnet@v4
+ with:
+ dotnet-version: '9.x'
+
+ - name: Setup MSBuild.exe
+ uses: microsoft/setup-msbuild@v2.0.0
+
+ - name: Build TrailBlazr project
+ run: dotnet build TrailBlazr -c $env:Configuration
+
+ - name: Package TrailBlazr
+ run: dotnet pack -c Release -o . TrailBlazr\TrailBlazr.csproj
+
+ - name: Publish
+ run: dotnet nuget push *.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate
diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml
new file mode 100644
index 0000000..641f96d
--- /dev/null
+++ b/.github/workflows/ci.yaml
@@ -0,0 +1,46 @@
+name: TrailBlazr CI Pipeline
+
+on:
+ push:
+ branches:
+ - master
+ pull_request:
+ branches:
+ - master
+ - release/*
+
+jobs:
+
+ build:
+ strategy:
+ matrix:
+ targetplatform: [x64]
+
+ runs-on: windows-latest
+
+ env:
+ Configuration: Debug
+ Actions_Allow_Unsecure_Commands: true
+
+ steps:
+ - name: Checkout
+ uses: actions/checkout@v4
+ with:
+ fetch-depth: 0
+
+ - name: Install .NET Core
+ uses: actions/setup-dotnet@v4
+ with:
+ dotnet-version: '9.x'
+
+ - name: Setup MSBuild.exe
+ uses: microsoft/setup-msbuild@v2.0.0
+
+ - name: Build TrailBlazr project
+ run: dotnet build TrailBlazr -c $env:Configuration
+
+ - name: Build TrailBlazr.Tests project
+ run: dotnet build TrailBlazr.Tests -c $env:Configuration
+
+ - name: Run tests
+ run: dotnet test TrailBlazr.Tests -c $env:Configuration --verbosity normal
\ No newline at end of file
diff --git a/Directory.Build.props b/Directory.Build.props
new file mode 100644
index 0000000..5a6708d
--- /dev/null
+++ b/Directory.Build.props
@@ -0,0 +1,9 @@
+ο»Ώ
+
+ 0.1.0
+
+ enable
+ enable
+ true
+
+
diff --git a/Directory.Packages.props b/Directory.Packages.props
new file mode 100644
index 0000000..aa1cfbe
--- /dev/null
+++ b/Directory.Packages.props
@@ -0,0 +1,11 @@
+ο»Ώ
+
+ true
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..23de219
--- /dev/null
+++ b/README.md
@@ -0,0 +1,165 @@
+ο»Ώ# TrailBlazr
+
+[](https://github.com/your-username/TrailBlazr/actions/workflows/ci.yaml)
+[](https://www.nuget.org/packages/TrailBlazr/)
+[](https://opensource.org/licenses/MIT)
+
+TrailBlazr is a powerful library for Blazor that enables compile-correct view management with linked ViewModels through the MVVM pattern. It provides a seamless navigation system using a view container component and view manager, making it easy to build maintainable Blazor applications with strong typing and separation of concerns.
+
+## Features
+
+- π **Strongly-typed View-ViewModel binding** - Compile-time safety for view-viewmodel relationships
+- π§ **Navigation management** - Centralized view navigation through `IViewManager`
+- ποΈ **MVVM pattern support** - Clean separation of concerns with ViewModels
+- π¦ **Dependency injection integration** - Seamless integration with .NET DI container
+- π― **Dynamic view rendering** - Efficient view switching with `ViewContainer` component
+- β‘ **Minimal setup** - Easy configuration with extension methods
+
+## Installation
+
+Install TrailBlazr via NuGet Package Manager:
+
+```bash
+dotnet add package TrailBlazr
+```
+
+Or via Package Manager Console:
+
+```powershell
+Install-Package TrailBlazr
+```
+
+## Quick Start
+
+### 1. Register TrailBlazr services
+
+In your `Program.cs` or startup configuration:
+
+```csharp
+using TrailBlazr.Extensions;
+
+// Add TrailBlazr services
+builder.Services.AddTrailBlazr();
+
+// Register your views and viewmodels
+builder.Services.RegisterView();
+builder.Services.RegisterView();
+
+var app = builder.Build();
+```
+
+### 2. Create a ViewModel
+
+```csharp
+using TrailBlazr.ViewModels;
+
+public class HomeViewModel : ViewModelBase
+{
+ public string Title { get; set; } = "Welcome to TrailBlazr!";
+ public string Message { get; set; } = "Navigate with confidence.";
+}
+```
+
+### 3. Create a View
+
+```razor
+@using TrailBlazr.Views
+@inherits ViewBase
+
+
+
@ViewModel.Title
+
@ViewModel.Message
+
+
+
+
+@code {
+ [Inject]
+ private IViewManager ViewManager { get; set; }
+
+ private void NavigateToProfile()
+ {
+ this.ViewManager.ShowView();
+ }
+}
+```
+
+### 4. Add ViewContainer to your layout
+
+```razor
+@using TrailBlazr.Components
+
+
+
+
+```
+
+### 5. Navigate between views
+
+Inject `IViewManager` anywhere in your application to navigate:
+
+```csharp
+[Inject]
+private IViewManager ViewManager { get; set; }
+
+// Navigate to a view with optional data context
+ViewManager.ShowView(userData);
+
+// Or navigate by type
+ViewManager.ShowView(typeof(HomeView), someData);
+```
+
+## Core Concepts
+
+### ViewBase
+
+Base class for all views that provides:
+- Strong typing between view and viewmodel
+- Automatic viewmodel injection
+- Access to `ViewManager` for navigation
+
+### ViewModelBase
+
+Base class for all viewmodels that provides:
+- Strong typing between viewmodel and view
+- Foundation for implementing business logic
+- Inherits from `INotifyPropertyChanged` for data binding
+
+### IViewManager
+
+Central service for managing view navigation:
+- `ShowView(object? dataContext)` - Navigate with strong typing
+- `ShowView(Type viewType, object? dataContext)` - Navigate by type
+- `ShowViewRequested` event - Subscribe to navigation events
+
+### ViewContainer
+
+Blazor component that hosts and renders the current view:
+- Automatically updates when navigation occurs
+- Supports dynamic component rendering
+- Passes data context to views
+
+## Architecture
+
+TrailBlazr follows the MVVM (Model-View-ViewModel) pattern:
+
+```
+βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
+β View βββββΊβ ViewModel βββββΊβ Model β
+β (Razor) β β (C# Class) β β (Data) β
+βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
+ β² β²
+ β β
+ βΌ βΌ
+βββββββββββββββββββ ββββββββββββββββββββ
+β ViewContainer βββββΊβ ViewManager β
+β (Component) β β (Service) β
+βββββββββββββββββββ ββββββββββββββββββββ
+```
+
+## Requirements
+
+- .NET 9.0 or later
+- Blazor Hybrid or Blazor Server/WebAssembly
\ No newline at end of file
diff --git a/TrailBlazr.Tests/MSTestSettings.cs b/TrailBlazr.Tests/MSTestSettings.cs
new file mode 100644
index 0000000..aaf278c
--- /dev/null
+++ b/TrailBlazr.Tests/MSTestSettings.cs
@@ -0,0 +1 @@
+ο»Ώ[assembly: Parallelize(Scope = ExecutionScope.MethodLevel)]
diff --git a/TrailBlazr.Tests/TrailBlazr.Tests.csproj b/TrailBlazr.Tests/TrailBlazr.Tests.csproj
new file mode 100644
index 0000000..ffd460f
--- /dev/null
+++ b/TrailBlazr.Tests/TrailBlazr.Tests.csproj
@@ -0,0 +1,11 @@
+
+
+
+ net9.0
+ latest
+ enable
+ enable
+ true
+
+
+
diff --git a/TrailBlazr.slnx b/TrailBlazr.slnx
new file mode 100644
index 0000000..49436c2
--- /dev/null
+++ b/TrailBlazr.slnx
@@ -0,0 +1,17 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/TrailBlazr/Components/EmptyComponent.razor b/TrailBlazr/Components/EmptyComponent.razor
new file mode 100644
index 0000000..e0a06ed
--- /dev/null
+++ b/TrailBlazr/Components/EmptyComponent.razor
@@ -0,0 +1,6 @@
+ο»Ώ
+Loading...
+
+@code {
+
+}
diff --git a/TrailBlazr/Components/ViewContainer.razor b/TrailBlazr/Components/ViewContainer.razor
new file mode 100644
index 0000000..d275898
--- /dev/null
+++ b/TrailBlazr/Components/ViewContainer.razor
@@ -0,0 +1,29 @@
+ο»Ώ
+
+@using TrailBlazr.Models
+@using TrailBlazr.Services
+@code {
+ [Inject]
+ protected IViewManager? ViewManager { get; set; }
+
+ protected Type CurrentViewType { get; set; } = typeof(EmptyComponent);
+
+ protected Dictionary CurrentParameters { get; set; } = [];
+
+ protected override void OnInitialized()
+ {
+ if (this.ViewManager is null)
+ {
+ throw new InvalidOperationException("ViewManager is not initialized.");
+ }
+
+ this.ViewManager.ShowViewRequested += this.UpdateCurrentView;
+ }
+
+ private void UpdateCurrentView(object? _, ViewRequest request)
+ {
+ this.CurrentParameters = new Dictionary() { { "DataContext", request.DataContext ?? new object() } };
+ this.CurrentViewType = request.ViewType;
+ this.StateHasChanged();
+ }
+}
diff --git a/TrailBlazr/Extensions/ServiceCollectionExtensions.cs b/TrailBlazr/Extensions/ServiceCollectionExtensions.cs
new file mode 100644
index 0000000..c58a83d
--- /dev/null
+++ b/TrailBlazr/Extensions/ServiceCollectionExtensions.cs
@@ -0,0 +1,37 @@
+ο»Ώusing Microsoft.Extensions.DependencyInjection;
+using TrailBlazr.Models;
+using TrailBlazr.Services;
+using TrailBlazr.ViewModels;
+using TrailBlazr.Views;
+
+namespace TrailBlazr.Extensions;
+public static class ServiceCollectionExtensions
+{
+ public static IServiceCollection AddTrailBlazr(this IServiceCollection services)
+ {
+ _ = services ?? throw new ArgumentNullException(nameof(services));
+
+ services.AddSingleton();
+ return services;
+ }
+
+ public static IServiceCollection RegisterView(this IServiceCollection services, bool isSingleton = false)
+ where TView : ViewBase
+ where TViewModel : ViewModelBase
+ {
+ _ = services ?? throw new ArgumentNullException(nameof(services));
+ if (isSingleton)
+ {
+ services.AddSingleton();
+ services.AddSingleton();
+ }
+ else
+ {
+ services.AddScoped();
+ services.AddScoped();
+ }
+
+ services.AddSingleton(new ViewRegistration());
+ return services;
+ }
+}
diff --git a/TrailBlazr/Models/ViewRegistration.cs b/TrailBlazr/Models/ViewRegistration.cs
new file mode 100644
index 0000000..dee8a4e
--- /dev/null
+++ b/TrailBlazr/Models/ViewRegistration.cs
@@ -0,0 +1,14 @@
+ο»Ώnamespace TrailBlazr.Models;
+
+public abstract class ViewRegistration(Type viewType, Type viewModelType)
+{
+ public Type ViewType { get; } = viewType ?? throw new ArgumentNullException(nameof(viewType));
+ public Type ViewModelType { get; } = viewModelType ?? throw new ArgumentNullException(nameof(viewModelType));
+}
+
+public sealed class ViewRegistration() : ViewRegistration(typeof(TView), typeof(TViewModel))
+ where TView : Views.ViewBase
+ where TViewModel : ViewModels.ViewModelBase
+{
+ public override string ToString() => $"({this.ViewType.Name} - {this.ViewModelType.Name})";
+}
diff --git a/TrailBlazr/Models/ViewRequest.cs b/TrailBlazr/Models/ViewRequest.cs
new file mode 100644
index 0000000..194b6ca
--- /dev/null
+++ b/TrailBlazr/Models/ViewRequest.cs
@@ -0,0 +1,4 @@
+ο»Ώnamespace TrailBlazr.Models;
+public sealed record ViewRequest(Type ViewType, Type ViewModelType, object? DataContext)
+{
+}
diff --git a/TrailBlazr/Services/IViewManager.cs b/TrailBlazr/Services/IViewManager.cs
new file mode 100644
index 0000000..5f59d34
--- /dev/null
+++ b/TrailBlazr/Services/IViewManager.cs
@@ -0,0 +1,15 @@
+ο»Ώusing TrailBlazr.Models;
+using TrailBlazr.ViewModels;
+using TrailBlazr.Views;
+
+namespace TrailBlazr.Services;
+public interface IViewManager
+{
+ event EventHandler? ShowViewRequested;
+
+ void ShowView(object? dataContext = null)
+ where TView : ViewBase
+ where TViewModel : ViewModelBase;
+
+ void ShowView(Type viewType, object? dataContext = null);
+}
diff --git a/TrailBlazr/Services/ViewManager.cs b/TrailBlazr/Services/ViewManager.cs
new file mode 100644
index 0000000..ad5c44d
--- /dev/null
+++ b/TrailBlazr/Services/ViewManager.cs
@@ -0,0 +1,36 @@
+ο»Ώusing TrailBlazr.Models;
+using TrailBlazr.ViewModels;
+using TrailBlazr.Views;
+
+namespace TrailBlazr.Services;
+public sealed class ViewManager(
+ IEnumerable viewRegistrations) : IViewManager
+{
+ private readonly List viewRegistrations = [.. viewRegistrations];
+
+ public event EventHandler? ShowViewRequested;
+
+ public void ShowView(object? dataContext = null)
+ where TView : ViewBase
+ where TViewModel : ViewModelBase
+ {
+ var viewType = typeof(TView);
+ this.ShowViewInner(viewType, dataContext);
+ }
+
+ public void ShowView(Type viewType, object? dataContext = null)
+ {
+ this.ShowViewInner(viewType, dataContext);
+ }
+
+ private void ShowViewInner(Type viewType, object? dataContext = null)
+ {
+ if (this.viewRegistrations.FirstOrDefault(r => r.ViewType == viewType) is not ViewRegistration registration)
+ {
+ throw new InvalidOperationException("View type not registered: " + viewType.FullName);
+ }
+
+ var viewRequest = new ViewRequest(registration.ViewType, registration.ViewModelType, dataContext);
+ this.ShowViewRequested?.Invoke(this, viewRequest);
+ }
+}
diff --git a/TrailBlazr/TrailBlazr.csproj b/TrailBlazr/TrailBlazr.csproj
new file mode 100644
index 0000000..9d06db9
--- /dev/null
+++ b/TrailBlazr/TrailBlazr.csproj
@@ -0,0 +1,18 @@
+ο»Ώ
+
+
+ net9.0
+ preview
+ enable
+ enable
+ Alexandru Macocian
+ Framework for hosting MVVM views in Blazor.
+ true
+
+
+
+
+
+
+
+
diff --git a/TrailBlazr/ViewModels/ViewModelBase.cs b/TrailBlazr/ViewModels/ViewModelBase.cs
new file mode 100644
index 0000000..3d36a8d
--- /dev/null
+++ b/TrailBlazr/ViewModels/ViewModelBase.cs
@@ -0,0 +1,43 @@
+ο»Ώusing System.ComponentModel;
+using TrailBlazr.Views;
+
+namespace TrailBlazr.ViewModels;
+
+public abstract class ViewModelBase : INotifyPropertyChanged
+ where TViewModel : ViewModelBase
+ where TView : ViewBase
+{
+ public event PropertyChangedEventHandler? PropertyChanged;
+
+ public object? DataContext
+ {
+ get => field;
+ set
+ {
+ field = value;
+ this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.DataContext)));
+ }
+ }
+
+ protected TView? View
+ {
+ get => field;
+ private set
+ {
+ field = value;
+ this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.View)));
+ }
+ }
+
+ internal void SetView(TView view)
+ {
+ this.View = view ?? throw new ArgumentNullException(nameof(view));
+ }
+
+ public void RefreshView()
+ {
+ this.View?.RefreshView();
+ }
+
+ public virtual ValueTask Initialize(CancellationToken cancellationToken) => ValueTask.CompletedTask;
+}
diff --git a/TrailBlazr/Views/ViewBase.cs b/TrailBlazr/Views/ViewBase.cs
new file mode 100644
index 0000000..91cf843
--- /dev/null
+++ b/TrailBlazr/Views/ViewBase.cs
@@ -0,0 +1,61 @@
+ο»Ώusing Microsoft.AspNetCore.Components;
+using Microsoft.Extensions.Logging;
+using System.ComponentModel;
+using TrailBlazr.ViewModels;
+
+namespace TrailBlazr.Views;
+public abstract class ViewBase : ComponentBase, INotifyPropertyChanged
+ where TView : ViewBase
+ where TViewModel : ViewModelBase
+{
+ public readonly static TimeSpan InitializationTimeout = TimeSpan.FromSeconds(5);
+
+ public event PropertyChangedEventHandler? PropertyChanged;
+
+ [Inject]
+ public TViewModel ViewModel { get; set; } = default!;
+ [Inject]
+ protected ILogger Logger { get; set; } = default!;
+ [Parameter]
+ public object? DataContext
+ {
+ get => field;
+ set
+ {
+ field = value;
+ this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(this.DataContext)));
+ }
+ }
+
+ protected override async Task OnAfterRenderAsync(bool firstRender)
+ {
+ await base.OnAfterRenderAsync(firstRender);
+ if (!firstRender)
+ {
+ return;
+ }
+
+ if (this is not TView view)
+ {
+ throw new InvalidOperationException($"View is not of type {typeof(TView).Name}. Actual type: {this.GetType().Name}");
+ }
+
+ this.ViewModel.SetView(view);
+ using var initializationCts = new CancellationTokenSource(InitializationTimeout);
+ _ = this.ViewModel ?? throw new InvalidOperationException("Cannot initialize ViewModel. ViewModel is null");
+ try
+ {
+ await this.ViewModel.Initialize(initializationCts.Token);
+ }
+ catch (Exception ex)
+ {
+ this.Logger?.LogError(ex, "Failed to initialize ViewModel {ViewModelType}", typeof(TViewModel).Name);
+ }
+ }
+
+ internal void RefreshView()
+ {
+ this.StateHasChanged();
+ }
+}
+