Mehedi.Patterns.Observer 1.0.4

dotnet add package Mehedi.Patterns.Observer --version 1.0.4
                    
NuGet\Install-Package Mehedi.Patterns.Observer -Version 1.0.4
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Mehedi.Patterns.Observer" Version="1.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Mehedi.Patterns.Observer" Version="1.0.4" />
                    
Directory.Packages.props
<PackageReference Include="Mehedi.Patterns.Observer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Mehedi.Patterns.Observer --version 1.0.4
                    
#r "nuget: Mehedi.Patterns.Observer, 1.0.4"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Mehedi.Patterns.Observer@1.0.4
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Mehedi.Patterns.Observer&version=1.0.4
                    
Install as a Cake Addin
#tool nuget:?package=Mehedi.Patterns.Observer&version=1.0.4
                    
Install as a Cake Tool

Observer Pattern in C# — Synchronous & Asynchronous Implementations

This repository demonstrates how to implement the Observer Design Pattern in C# with both synchronous and asynchronous support, along with centralized management using ObserverFactory and AsyncObserverFactory.

📌 Features

  • Synchronous Observer Pattern
  • Asynchronous Observer Pattern using Task-based notifications
  • Thread-safe factories for centralized observer management
  • Strongly-typed subjects per key
  • Easy-to-use API for registration and broadcasting

🚀 Getting Started

1️⃣ Clone the repo

git clone https://github.com/your-username/observer-pattern-csharp.git
cd observer-pattern-csharp

2️⃣ Build the project

dotnet build

3️⃣ Run examples

Synchronous Example

dotnet run --project examples/ProgramSync.csproj

Asynchronous Example

dotnet run --project examples/ProgramAsync.csproj

📖 Usage Examples

Synchronous with ObserverFactory

var factory = ObserverFactory.Instance;

factory.RegisterHandler<int>(
    key: "number-changed",
    sender: "MainProgram",
    action: value => Console.WriteLine($"[SYNC] Received value: {value}")
);

factory.Notify("number-changed", 42);
factory.Notify("number-changed", 99);

Output:

[SYNC] Received value: 42
[SYNC] Received value: 99

Asynchronous with AsyncObserverFactory

var asyncFactory = AsyncObserverFactory.Instance;

asyncFactory.RegisterHandler<int>(
    key: "number-changed-async",
    sender: "MainProgram",
    async value =>
    {
        await Task.Delay(500);
        Console.WriteLine($"[ASYNC] Received value: {value}");
    }
);

await asyncFactory.NotifyAsync("number-changed-async", 42);
await asyncFactory.NotifyAsync("number-changed-async", 99);

Output:

[ASYNC] Received value: 42
[ASYNC] Received value: 99

📦 Installation

You can copy the ObserverFactory and AsyncObserverFactory code into your project directly or package them into a shared library.


📚 References

  1. Microsoft Docs: Observer Design Pattern
  2. Gang of Four — Design Patterns
  3. Reactive Extensions (Rx.NET)

📝 License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.4 256 8/8/2025