Git Product home page Git Product logo

simpleproxy's Introduction

SimpleProxy

What is SimpleProxy?

SimpleProxy solves the problem of Aspect Orientated Programming (AoP) in Net Core. It builds on the current Net Core Dependency Injection functions to extend it, providing Proxy services with a very small learning curve.

What is Aspect Orientated Programming?

Wikipedia describes AOP as the following:

"In computing, aspect-oriented programming (AOP) is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns. It does so by adding additional behavior to existing code (an advice) without modifying the code itself..."

Source: https://en.wikipedia.org/wiki/Aspect-oriented_programming

SimpleProxy vs PostSharp

PostSharp is a long established and mature solution for doing what SimpleProxy does. It allows you to add additional functionality to your classes without spoiling them with repetitive boilerplater code. The fundamental difference is that PostSharp injects its code AFTER compilation, changing the the binaries that are output with additional code. SimpleProxy will wrap your code at runtime, so debugging is easy, and the changes more transparent to a developer. In short, SimpleProxy makes things more obvious.

Getting Started

Option 1 => Source Code
  • Download the code from GitHub, either by cloning the repository or downloading the files directly.
  • Open the [SimpleProxy.sln] file in Visual Studio
  • Build the solution
  • Start the [SampleApp] project to see it in action
Option 2 => Binary

Visit https://www.nuget.org/packages/SimpleProxy/1.0.1 and download the latest binary

Option 3 => Nuget

Install directly into your project using: Install-Package SimpleProxy -Version 1.0.1 in your Nuget Package Manager

What do I need to run the code?

How does it work?

Creating proxies for objects is not straightforward. One of the most common frameworks for doing so is (and has been for a long time) Castle Core. (https://github.com/castleproject/Core). Infact, Castle Core is used as a fundamental building block for this project. Documentation can be difficult to find for Castle Core and its not straightforward to work with on its own.

SimpleProxy is designed to simplify the whole process. Interception is done in just a few steps:

  • Create a custom attribute that derives from the SimpleProxy base attribute
public class MyCustomAttribute : MethodInterceptionAttribute
{
    public MyCustomAttribute()
    {
    }
}
  • Create an interceptor that implements the SimpleProxy IMethodInterceptor
public class MyCustomInterceptor : IMethodInterceptor
{
    public void BeforeInvoke(InvocationContext invocationContext)
    {
    }

    public void AfterInvoke(InvocationContext invocationContext, object methodResult)
    {
    }
}
  • Register a mapping for the Attribute & Interceptors in the ServiceCollection
    // Configure the Service Provider
    var services = new ServiceCollection();

    // Enable SimpleProxy
    services.EnableSimpleProxy(p => p.AddInterceptor<MyCustomAttribute, MyCustomInterceptor>());
  • Add your class to the ServiceCollection using one of the SimpleProxy IServiceCollection overloads
    services.AddTransientWithProxy<ITestClass, TestClass>();

SimpleProxy uses the default Microsoft.Extensions.DependencyInjection library built into ASP Net Core to register interceptors and then intercept method calls based on the attributes applied to your methods. Methods are intercepted with YOUR own code either before and/or after a method call.

How do I register interceptors with the DI Framework?

Interceptors are registered in the Microsoft DI framework using the EnableSimpleProxy extension method on IServiceCollection. The AddInterceptor<T,T2>() method uses the fluent interface so it can be chained for easier configuration.

    // Configure the Service Provider
    var services = new ServiceCollection();

    // Enable SimpleProxy
    services.EnableSimpleProxy(p => p
            .AddInterceptor<MyCustomAttribute, MyCustomInterceptor>()
            .AddInterceptor<MyCustomAttribute2, MyCustomInterceptor2>());

What is InvocationContext?

...

How do I intercept my method calls?

...

Can I change the method values?

...

How do I get the return value from the method that was invoked?

...

Are there any example interceptors to get started with?

...

How can I extend SimpleProxy?

...

Is SimpleProxy actively developed?

...

simpleproxy's People

Contributors

f135ta avatar rafaporci avatar stricq 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.