Skip to content

A scalable and maintainable Blazor application implementing the MVVM pattern using CommunityToolkit.Mvvm. Features include adding, toggling, and asynchronously loading todo items, with real-time statistics.

License

Notifications You must be signed in to change notification settings

phmatray/BlazorMvvmApp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

8 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

Blazor MVVM Todo App

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.

๐Ÿ“‹ Table of Contents

๐ŸŒŸ Features

  • 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 and StatsViewModel operate independently, interacting through a shared data service.
    • Observable Properties: Automatic property change notifications ensure seamless UI updates.
  • 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.

๐Ÿ›  Technologies Used

  • 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.

๐Ÿ—‚ Project Structure

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

๐Ÿ“„ File Descriptions

  • 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.
  • 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 and IsComplete.
  • 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.

๐Ÿš€ Getting Started

Follow these instructions to set up and run the project locally.

๐Ÿ“ Prerequisites

๐Ÿ“ฅ Installation

  1. Clone the Repository:

    git clone https://github.com/phmatray/BlazorMvvmApp.git
  2. Navigate to the Project Directory:

    cd BlazorMvvmApp
  3. Restore Dependencies:

    dotnet restore
  4. 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

โ–ถ๏ธ Running the Application

  1. Build the Project:

    dotnet build
  2. Run the Application:

    dotnet run
  3. Access the Application:

    Open your browser and navigate to https://localhost:5001 or the URL specified in the terminal output.

๐ŸŽฎ Usage

Todo Page (/todos or /)

  • Add a Todo:

    1. Enter a title in the input field.
    2. Click the "Add" button.
    3. 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.

Stats Page (/stats)

  • View Total Todos:
    • Displays the total count of todos, including both initial and loaded items.

๐Ÿ› Architecture Overview

The application follows the MVVM pattern, separating concerns into Models, ViewModels, and Views (Blazor components).

Models

  • TodoItem:
    • Represents the data structure for a todo item.

ViewModels

  • 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.

Services

  • 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.

Components

  • ViewModelComponentBase:

    • A base component that subscribes to PropertyChanged events from ViewModels to trigger UI updates (StateHasChanged).
  • Todos.razor:

    • UI component for displaying and interacting with the todo list.
  • Stats.razor:

    • UI component for displaying statistics related to todos.

๐Ÿค Contributing

Contributions are welcome! Follow these steps to contribute to the project:

  1. Fork the Repository:

    Click the "Fork" button at the top-right corner of the repository page.

  2. Clone Your Fork:

    git clone https://github.com/phmatray/BlazorMvvmApp.git
    cd BlazorMvvmApp
  3. Create a Feature Branch:

    git checkout -b feature/YourFeatureName
  4. Commit Your Changes:

    git commit -m "Add your detailed description of changes"
  5. Push to Your Fork:

    git push origin feature/YourFeatureName
  6. Create a Pull Request:

    Navigate to your fork on GitHub and click the "Compare & pull request" button.

๐Ÿ“Œ Guidelines

  • 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.

๐Ÿ“„ License

This project is licensed under the MIT License.
See the LICENSE file for details.


๐Ÿ“ž Contact

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.

About

A scalable and maintainable Blazor application implementing the MVVM pattern using CommunityToolkit.Mvvm. Features include adding, toggling, and asynchronously loading todo items, with real-time statistics.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published