Welcome to the Blazor MVVM Todo App, a modern, scalable, and maintainable web application built using the Blazor framework and adhering to the Model-View-ViewModel (MVVM) architectural pattern. This application leverages the CommunityToolkit.Mvvm package to streamline MVVM implementations, reducing boilerplate code and enhancing developer productivity.
- Blazor MVVM Todo App
-
Todo Management:
- Add Todos: Easily add new todo items with a title.
- Toggle Completion: Mark todos as complete or incomplete.
- Load Todos: Asynchronously load additional todos.
-
Statistics:
- Total Todos: View the total count of todos, including initial and loaded items.
-
MVVM Architecture:
- Decoupled ViewModels:
TodoViewModel
andStatsViewModel
operate independently, interacting through a shared data service. - Observable Properties: Automatic property change notifications ensure seamless UI updates.
- Decoupled ViewModels:
-
CommunityToolkit.Mvvm Integration:
- Simplified Commands: Utilize
[RelayCommand]
and[ObservableProperty]
attributes to reduce boilerplate. - Shared Data Service: A centralized
TodoService
manages todo items, promoting a single source of truth.
- Simplified Commands: Utilize
-
Frameworks & Libraries:
- Blazor: A framework for building interactive client-side web UI with .NET.
- CommunityToolkit.Mvvm: Provides utilities and components to implement the MVVM pattern effectively.
-
Languages:
- C#: Primary programming language.
- Razor: Markup syntax for building Blazor components.
-
Tools:
- .NET SDK: For building and running the application.
- Visual Studio / Visual Studio Code: Recommended IDEs for development.
BlazorMvvmApp/
โโโ Components/
โ โ App.razor
โ โโโ ViewModelComponentBase.cs
โโโ Features/
โ โโโ Stats/
โ โ โโโ StatsViewModel.cs
โ โ โโโ Stats.razor
โ โโโ Todos/
โ โโโ TodoItem.cs
โ โโโ TodoViewModel.cs
โ โโโ Todos.razor
โโโ Services/
โ โโโ ITodoService.cs
โ โโโ TodoService.cs
โโโ DependencyInjections.cs
โโโ Program.cs
-
Components/App.razor:
- Root component of the Blazor application, configuring routing.
-
Components/ViewModelComponentBase.cs:
- A base Blazor component that handles
PropertyChanged
events from ViewModels to trigger UI updates.
- A base Blazor component that handles
-
Features/Stats/StatsViewModel.cs:
- ViewModel responsible for tracking and updating the total count of todos.
-
Features/Stats/Stats.razor:
- Blazor component that displays statistics, specifically the total number of todos.
-
Features/Todos/TodoItem.cs:
- Model representing a single todo item with properties like
Title
andIsComplete
.
- Model representing a single todo item with properties like
-
Features/Todos/TodoViewModel.cs:
- ViewModel managing todo items, including adding new todos and loading additional ones asynchronously.
-
Features/Todos/Todos.razor:
- Blazor component for displaying and interacting with the todo list.
-
Services/ITodoService.cs & Services/TodoService.cs:
- Shared data service managing the collection of todos, ensuring a single source of truth across ViewModels.
-
DependencyInjections.cs:
- Extension methods for registering services and ViewModels with the Dependency Injection (DI) container.
-
Program.cs:
- Entry point of the Blazor application, configuring services and building the host.
Follow these instructions to set up and run the project locally.
-
.NET SDK 9.0 or later:
Ensure you have the .NET SDK installed. -
IDE:
- Visual Studio 2022 or later with the ASP.NET and web development workload.
- Alternatively, Visual Studio Code with the C# extension.
- Or even better, JetBrains Rider.
-
Clone the Repository:
git clone https://github.com/phmatray/BlazorMvvmApp.git
-
Navigate to the Project Directory:
cd BlazorMvvmApp
-
Restore Dependencies:
dotnet restore
-
Install CommunityToolkit.Mvvm Package:
The
CommunityToolkit.Mvvm
package is already referenced in the project. If not, install it via the CLI:dotnet add package CommunityToolkit.Mvvm
-
Build the Project:
dotnet build
-
Run the Application:
dotnet run
-
Access the Application:
Open your browser and navigate to
https://localhost:5001
or the URL specified in the terminal output.
-
Add a Todo:
- Enter a title in the input field.
- Click the "Add" button.
- The new todo appears in the list.
-
Toggle Completion:
- Click the checkbox next to a todo to mark it as complete or incomplete.
-
Load More Todos:
- Click the "Load Todos" button to asynchronously load additional todos.
- View Total Todos:
- Displays the total count of todos, including both initial and loaded items.
The application follows the MVVM pattern, separating concerns into Models, ViewModels, and Views (Blazor components).
- TodoItem:
- Represents the data structure for a todo item.
-
TodoViewModel:
- Manages the collection of todos, handles adding new todos, and loading additional todos asynchronously.
-
StatsViewModel:
- Monitors the total count of todos by subscribing to changes in the
ITodoService
.
- Monitors the total count of todos by subscribing to changes in the
- ITodoService & TodoService:
- A shared service that manages the
ObservableCollection<TodoItem>
. Acts as a single source of truth for the todo items, allowing ViewModels to interact with it without direct dependencies on each other.
- A shared service that manages the
-
ViewModelComponentBase:
- A base component that subscribes to
PropertyChanged
events from ViewModels to trigger UI updates (StateHasChanged
).
- A base component that subscribes to
-
Todos.razor:
- UI component for displaying and interacting with the todo list.
-
Stats.razor:
- UI component for displaying statistics related to todos.
Contributions are welcome! Follow these steps to contribute to the project:
-
Fork the Repository:
Click the "Fork" button at the top-right corner of the repository page.
-
Clone Your Fork:
git clone https://github.com/phmatray/BlazorMvvmApp.git cd BlazorMvvmApp
-
Create a Feature Branch:
git checkout -b feature/YourFeatureName
-
Commit Your Changes:
git commit -m "Add your detailed description of changes"
-
Push to Your Fork:
git push origin feature/YourFeatureName
-
Create a Pull Request:
Navigate to your fork on GitHub and click the "Compare & pull request" button.
-
Code Quality:
Ensure your code follows best practices and is well-documented. -
Testing:
Include unit tests for new features or significant changes. -
Commit Messages:
Write clear and descriptive commit messages.
This project is licensed under the MIT License.
See the LICENSE file for details.
For any questions, suggestions, or support, please open an issue in the GitHub repository.
Thank you for checking out the Blazor MVVM Todo App! We hope it serves as a solid foundation for your Blazor projects using the MVVM pattern.