Giter Site home page Giter Site logo

nexusaop's Introduction

Contributors Forks Stargazers Issues NuGet MIT License

NexusAop

NexusAop is a powerful and flexible library for reflection and aspect-oriented programming (AOP) in .NET 5.0. This library enables developers to easily apply cross-cutting concerns to their applications by utilizing custom attributes. With NexusAop, you can interrupt method executions, perform specific actions, and retrieve results seamlessly.
Report Bug or Request Feature · Contect Me Via Mail · Contect Me Via Linkedin

Table Of Content

  1. Features
  2. Get It Started
  3. Cache Attribute Example
  4. Contributions
  5. License

Features

  1. Aspect-Oriented Programming (AOP):
    NexusAop empowers developers to embrace the principles of AOP by providing a straightforward mechanism for applying cross-cutting concerns using custom attributes.
  2. Method Interruption:
    Leverage the NextAsync() method to interrupt the execution of a method and perform specific actions before allowing the method to continue. This allows for dynamic and context-aware behavior in your applications.
  3. Result Retrieval:
    Utilize the ExecuteAndGetResultAsync() method to retrieve the result of the related method. This feature is particularly useful when you need to capture and manipulate the output of a method in a controlled manner.
  4. Custom Attributes:
    Easily create and apply custom attributes to your methods, enabling a clean and declarative way to define aspects. Custom attributes in NexusAop serve as the building blocks for weaving cross-cutting concerns into your application.
  5. .NET 5.0 Compatibility:
    NexusAop is designed to seamlessly integrate with .NET 5.0.

Get It Started

To start using NexusAop in your .NET 5.0 project, follow these simple steps:

  1. Install the Package:

dotnet add package NexusAop

2. Service Implementation:

serviceCollection.AddSingletonWithCustomAop<ITestService, TestService>();
  1. Apply Custom Attributes:

Decorate your methods with custom attributes to define the desired cross-cutting concerns.

public class TestService : ITestService
{
  [CustomAspect]
  public async Task<int> MyMethodAsync()
  {
      // Your method implementation
  }
}
  1. Integrate Aspect-Oriented Behavior:

Use the provided methods such as NextAsync() and ExecuteAndGetResultAsync() within your custom aspects to influence the method execution flow.

public class CustomAspectAttribute : NexusAopAttribute
{
    public override async Task ExecuteAsync(NexusAopContext context)
    {
        // Perform actions before the method execution

        // Proceed with the execution of the target method
        var result = await context.NextAsync();

        // User-defined logic after the target method

        // Get the result if you needed
        var setResult= await context.ExecuteAndGetResultAsync();

        return result;
    }
}
  1. Build and Run:

Build your project, and NexusAop will seamlessly weave the specified aspects into your methods during runtime.

Cache Attribute Example

public class CacheMethodAttribute : NexusAopAttribute
    {
        public CacheMethodAttribute(
                int ttlAsSecond)
        {
            Ttl = TimeSpan.FromSeconds(ttlAsSecond);
        }

        public CacheMethodAttribute()
        {
            Ttl = null;
        }

        public TimeSpan? Ttl { get; set; }

        public override async Task ExecuteAsync(NexusAopContext context)
        {
            if (!CheckMethodCacheable(context.TargetMethod))
            {
                return;
            }
            var cacheKey = GetCacheKey(context.TargetMethod, context.TargetMethodsArgs);
            var result = GetResult(cacheKey);

            if (result != null)
            {
                context.Result= result;
                return;
            }

            result = await context.ExecuteAndGetResultAsync();
            await SetCacheAsync(context.TargetMethod, context.TargetMethodsArgs,result);
        }

        // ...
        // see CacheMethodAttribute.cs in /Samples/Cache for other logics
        // ...
  }

Contributions

Contributions are welcome! If you encounter any issues or have suggestions for improvements, please feel free to create an issue or submit a pull request.

License

This project is licensed under the BSD 3-Clause License.

nexusaop's People

Contributors

anilsnl avatar asliyigiit avatar

Stargazers

 avatar Yiğit Tanyel avatar Umit AKINCI avatar

Watchers

Bilgehan Berberoğlu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.