RonSijm.BlazorAspect 0.1.0-alpha

Prefix Reserved
This is a prerelease version of RonSijm.BlazorAspect.
There is a newer version of this package available.
See the version list below for details.
dotnet add package RonSijm.BlazorAspect --version 0.1.0-alpha
                    
NuGet\Install-Package RonSijm.BlazorAspect -Version 0.1.0-alpha
                    
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="RonSijm.BlazorAspect" Version="0.1.0-alpha" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="RonSijm.BlazorAspect" Version="0.1.0-alpha" />
                    
Directory.Packages.props
<PackageReference Include="RonSijm.BlazorAspect" />
                    
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 RonSijm.BlazorAspect --version 0.1.0-alpha
                    
#r "nuget: RonSijm.BlazorAspect, 0.1.0-alpha"
                    
#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 RonSijm.BlazorAspect@0.1.0-alpha
                    
#: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=RonSijm.BlazorAspect&version=0.1.0-alpha&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=RonSijm.BlazorAspect&version=0.1.0-alpha&prerelease
                    
Install as a Cake Tool

RonSijm.BlazorAspect

.NET NuGet codecov

A C# Blazor library To assign Aspects to a page or component through assigning an interface.

NuGet: https://www.nuget.org/packages/RonSijm.BlazorAspect/

What is this library:

This library aims to add Aspect to Blazor pages or components, and allows you to basically delegate OnAfterRender to a standalone class.

Practically this library introduces a hook after OnParametersSetAsync and Before OnAfterRender that can be used by interface based subscribers.

In usual C# you might want to implement similar behavior through PostSharp or Fody, and handle the AOP side through code-weaving during compile time, but in Blazor those approaches are not always ideal, and it's sometimes preferred to handle things run-time

Or in usual C# you would lean towards Castle DynamicProxy and interceptors - that's not doing to work very well in Blazor

Example Use-Cases

A good example of a use-case is Fluxor.Blazor.

Fluxor.Blazor allows you to "Automagically" subscribe to IState components through inheriting from a FluxorComponent
The downside of that, is that you have to inherit all your classes from the FluxorComponent. And as you (should) know - "favor composition over inheritance"

This library will allow you to replace the inheritance with composition though interfaces.

Getting started:

To enable BlazorAspects:

In your program.cs simply start adding aspects with a the line such as:
builder.Services.WhenAssignableFrom(typeof(ILogRendering)).UseAspect(component => LogRenderingAction.Log(component));

For example:

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);

        builder.Services.WhenAssignableFrom(typeof(ILogRendering)).UseAspect(component => LogRenderingAction.Log(component));
		// Other stuff
	}
}

AssignableFrom is expected to be the most common use-case, though you can also use your own type condition.

For example:

builder.Services.WhenType(x => x.IsAssignableFrom(typeof(ILogRendering))).UseAspect(component => LogRenderingAction.Log(component));

To enable logging:

To enable logging, you can add the following line to your program.cs:

It used the default ILogger, and can be configured as such.

    public static async Task Main(string[] args)
    {
        var builder = WebAssemblyHostBuilder.CreateDefault(args);
		
		// I think you have to add this before building
        builder.Logging.AddFilter("RonSijm.BlazorAspect.AspectActivation", LogLevel.Trace);
		
        var application = builder.Build();

        //Add this after building
        application.Services.EnableComponentResolveLogging();

        await application.RunAsync();
    }
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.

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.2 304 4/11/2023
1.0.1 236 4/7/2023
1.0.0.1 246 4/6/2023
1.0.0 244 4/5/2023
0.1.0-alpha 199 4/5/2023