Thanks to visit codestin.com
Credit goes to github.com

Skip to content

mvpete/aspekt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Aspekt - Aspect-Oriented Programming for .NET

Build status .NET License Version

Aspekt is a lightweight, powerful Aspect-Oriented Programming (AOP) foundation library for .NET that allows you to implement cross-cutting concerns using attributes. It supports modern .NET versions including .NET 6.0, 8.0, and 9.0, with async/await patterns and comprehensive Design by Contract capabilities.

πŸš€ Key Features

  • Attribute-Based AOP: Apply aspects declaratively using C# attributes
  • Async Support: Full support for async/await patterns with ValueTask methods
  • Design by Contract: Comprehensive contract system with preconditions, postconditions, and invariants
  • Return Value Interception: Modify return values using IAspectExitHandler<T>
  • Modern .NET Support: Compatible with .NET 6.0, 8.0, 9.0, and .NET Standard 2.1
  • Post-Compilation Weaving: IL manipulation using Mono.Cecil for zero-overhead aspect application
  • Built-in Logging: Ready-to-use logging aspects with customizable formatters
  • Thread-Safe: Designed for multi-threaded applications

πŸ“¦ Installation

# Core AOP functionality
Install-Package Aspekt -Version 3.0.0

# Design by Contract support
Install-Package Aspekt.Contracts -Version 3.0.0

# Logging aspects
Install-Package Aspekt.Logging -Version 3.0.0

πŸƒβ€β™‚οΈ Quick Start

Basic Logging Aspect

using Aspekt;

public class LoggingAspect : Aspect
{
    public override void OnEntry(MethodArguments args)
    {
        Console.WriteLine($"Entering: {args.FullName}");
    }

    public override void OnExit(MethodArguments args)
    {
        Console.WriteLine($"Exiting: {args.FullName}");
    }

    public override void OnException(MethodArguments args, Exception ex)
    {
        Console.WriteLine($"Exception in {args.FullName}: {ex.Message}");
    }
}

Usage

public class Calculator
{
    [Logging]
    public int Add(int x, int y)
    {
        return x + y;
    }

    [Logging]
    public async Task<string> GetDataAsync()
    {
        await Task.Delay(100);
        return "Hello, World!";
    }
}

Advanced: Return Value Modification

public class ResultModifierAspect : Aspect, IAspectExitHandler<string>
{
    public string OnExit(MethodArguments args, string result)
    {
        return $"Modified: {result}";
    }
}

public class Service
{
    [ResultModifier]
    public string GetMessage() => "Original";
    // Returns: "Modified: Original"
}

Design by Contract

using Aspekt.Contracts;

public class BankAccount
{
    private decimal _balance;
    
    [Invariant(nameof(_balance), Contract.Comparison.GreaterThanEqualTo, 0)]
    public decimal Balance => _balance;
    
    [Require(nameof(amount), Contract.Comparison.GreaterThan, 0)]
    [Ensure(Contract.Comparison.GreaterThanEqualTo, 0)]
    public decimal Deposit(decimal amount)
    {
        _balance += amount;
        return _balance;
    }
}

πŸ“š Documentation

πŸ”§ How It Works

Aspekt uses post-compilation IL weaving via Mono.Cecil. When you apply an aspect attribute to a method:

  1. Build Time: Your code compiles normally
  2. Post-Build: Aspekt.Bootstrap.Host processes your assembly
  3. IL Weaving: Aspect calls are injected into your methods
  4. Runtime: Aspects execute seamlessly with your code
// Your code:
[LoggingAspect]
public void DoWork() { /* your logic */ }

// Becomes (conceptually):
public void DoWork()
{
    var aspect = new LoggingAspect();
    var args = new MethodArguments(/* method info */);
    
    aspect.OnEntry(args);
    try
    {
        /* your original logic */
        aspect.OnExit(args);
    }
    catch (Exception ex)
    {
        aspect.OnException(args, ex);
        throw;
    }
}

πŸ—οΈ Project Structure

  • Aspekt: Core AOP functionality and base Aspect class
  • Aspekt.Contracts: Design by Contract implementation
  • Aspekt.Logging: Built-in logging aspects with multiple formatters
  • Aspekt.Bootstrap: IL weaving engine using Mono.Cecil
  • Aspekt.Test: Comprehensive test suite with 100+ tests

πŸ› οΈ Build Requirements

  • .NET SDK 6.0 or later
  • Visual Studio 2022 or VS Code
  • MSBuild 17.0+
# Clone and build
git clone https://github.com/mvpete/aspekt.git
cd aspekt
dotnet build
dotnet test

πŸš€ CI/CD Pipeline

This project uses GitHub Actions for continuous integration and deployment:

  • Automated Testing: Multi-version testing across .NET 6.0, 8.0, and 9.0
  • Code Quality: Automated formatting, analysis, and security scanning
  • Package Publishing: Automatic NuGet releases on tagged versions
  • Documentation: Link validation and spell checking

See Pipeline Documentation for detailed information about the build and release process.

⚑ Performance

Aspekt is designed for minimal runtime overhead:

  • Zero reflection at runtime
  • Compile-time weaving means no performance impact from AOP infrastructure
  • Selective application - only methods with aspects are modified
  • Async-aware - proper support for async/await patterns

🀝 Contributing

We welcome contributions! Please see CONTRIBUTING.md for guidelines.

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for your changes
  4. Ensure all tests pass
  5. Submit a pull request

πŸ“„ License

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

πŸ™ Acknowledgments

  • Built with Mono.Cecil for IL manipulation
  • Inspired by PostSharp and other AOP frameworks
  • Thanks to all contributors and users

Get started today: Check out the Getting Started Guide to begin using Aspekt in your projects!

About

A lightweight (Aspect Oriented Programming) AOP foundation

Topics

Resources

License

Code of conduct

Contributing

Stars

Watchers

Forks

Packages

 
 
 

Contributors 6