Stuhia 1.0.5

dotnet add package Stuhia --version 1.0.5
NuGet\Install-Package Stuhia -Version 1.0.5
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="Stuhia" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Stuhia --version 1.0.5
#r "nuget: Stuhia, 1.0.5"
#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.
// Install Stuhia as a Cake Addin
#addin nuget:?package=Stuhia&version=1.0.5

// Install Stuhia as a Cake Tool
#tool nuget:?package=Stuhia&version=1.0.5

Stuhia

Nuget Nuget

Stuhia

Stuhia is a .NET NuGet package designed to simplify event handling and decouple application concerns. With this package, developers can easily publish and subscribe to application-level events, enabling a more flexible and maintainable architecture. By abstracting event handling, Stuhia promotes cleaner code and better separation of concerns. Start using Stuhia today to enhance the modularity and extensibility of your .NET applications!

How it works

It's important to understand the purpose of application events. In enterprise level software, where code keeps getting more and more bulky, it is required to keep it clean, readable and foremost modular keeping domain concerns decoupled from each-other. Application events in one way or other help us achieve that, let's take a really basic example. Let's say whenever a domain entity is updated successfully we want to remove its cache, so here we have two different concerns: updating entity and removing its cache. In order to decouple these concerns we could easily achieve that through Stuhia in three steps listed below:

  1. We need a model which represents this event and consists the required properties for fulfilling the cache removal requirements. Let's name it EntityUpdatedEvent and make sure we have marked it with the IApplicationEvent interface.
public class EntityUpdatedEvent : IApplicationEvent 
{
	public string EntityId { get; init; }
	public string EntityName { get; init; }
}
  1. Every time we define an application event we need to define its handler. We can do this by implementing the IEventHandler<> interface, where we pass the event type we want to handle as its generic type parameter.
public class EntityUpdatedEventHandler(IMemoryCache memoryCache) : IEventHandler<EntityUpdatedEvent>
{
	private readonly IMemoryCache _memoryCache = memoryCache;

	public Task HandleAsync(EntityUpdatedEvent @event, CancellationToken cancellationToken)
	{
		_memoryCache.Remove($"{@event.EventName}_{@event.EventId}");
		
		return Task.CompletedTask;
	}
}

In the code snippet above, we can see that we are injecting the IMemoryCache in our handler constructor, this is because Stuhia supports injecting services that have been registered in DI container.

Note Stuhia doesn't register event handlers in the DI container, in order to reduce memory allocation, event handler instances are lazy loaded.

  1. Last but not least there's only one way to publish this event so all this could work out, and that's it through the IEventPublisher interface, so we can inject this in our service constructor and call its PublishAsync method whenever the domain logic flow has completed successfully.
public async Task<bool> UpdateAsync(int id, DomainEntity entity)
{
	//business logic for updaing domain entity
	
    if (isUpdated) 
    {
	    await _eventPublisher.PublishAsync(new EntityUpdatedEvent
	    {
		    EntityId = id,
		    EntityName = entity.GetType().Name
	    });
    }
}

One more thing

In order to make this thing work, you need to register Stuhia in your Program.cs. This can be done by invoking the desired extension method, like shown below.

builder.Services.AddApplicationEvents(config =>
{
    config.SilentFailures = true;
    config.RegisterHandlersFromAssembly(Assembly.GetAssembly(typeof(EntityUpdatedEventHandler));
});

And by that, you're ready to go. Happy publishing and handling!

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.5 90 3/22/2024
1.0.4 84 3/22/2024
1.0.3 80 3/21/2024
1.0.2 87 3/18/2024
1.0.1 102 3/17/2024
1.0.0 108 3/17/2024