Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

isdaniel/AwesomeProxy.Net

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
61 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AwesomeProxy.Net NuGet Downloads Build status Ask DeepWiki


Introduction to AOP (Aspect-Oriented Programming)

AOP is a programming paradigm that extends OOP (Object-Oriented Programming) (it does not replace OOP but extends it).

Introducing AOP helps to:

Separate core logic from non-core logic code, which reduces module coupling and facilitates future extensions.

Non-core logic code includes aspects like: (logging, performance statistics, security control, transaction processing, exception handling, etc.), separating them from business logic code.

For example:

Instead of embedding logging-related code within business logic methods, which violates the single responsibility principle, we can refactor and extract logging methods (as shown in the right image).

like below image.

https://ithelp.ithome.com.tw/upload/images/20180209/20096630UyP6I4l2MB.png

Classic Example: In Asp.Net MVC, Controller, Action filters (FilterAttribute)


Introduction to AwesomeProxy.Net:

AwesomeProxy.Net mainly intercepts method processing:

  • Before method execution
  • After method execution
  • On method exception

How to Use: Usage is similar to Controller, Action filters in Asp.Net MV

Before 1.5 version

  1. Write an attribute to mark the interception action
public class CacheAttribute : AopBaseAttribute
{
    public string CacheName { get; set; }

    public override void OnExecting(ExecuteingContext context)
    {
        object cacheObj = CallContext.GetData(CacheName);
        if (cacheObj != null)
        {
            context.Result = cacheObj;
        }
    }

    public override void OnExecuted(ExecutedContext context)
    {
        CallContext.SetData(CacheName, context.Result);
    }
}
  1. Inherit the class to be intercepted from MarshalByRefObject
public class CacheService : MarshalByRefObject
{
        [Cache]
	public string GetCacheDate()
	{
		return DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
	}
}
  1. Dynamically generate the proxy class using ProxyFactory.GetProxyInstance
CacheService cache = ProxyFactory.GetProxyInstance<CacheService>();
  1. Directly call the method to execute the interception action on the attribute
CacheService cache = ProxyFactory.GetProxyInstance<CacheService>();
Console.WriteLine(cache.GetCacheDate());

After 1.5 version

  1. Write an attribute to mark the interception action
public class CacheAttribute : AopBaseAttribute
{
	public string CacheName { get; set; }

	public override void OnExecuted(ExecutedContext context)
	{
	    CallContext.SetData(CacheName, context.Result);
	}

	public override void OnExecuting(ExecutingContext context)
	{
	    object cacheObj = CallContext.GetData(CacheName);
	    if (cacheObj != null)
	    {
		context.Result = cacheObj;
	    }
	}
}
  1. Create an interface and class
public class CacheService : ICacheService
{
	public string GetCacheDate()
	{
	    return DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss");
	}
}

public interface ICacheService {
	[Cache(CacheName = "GetCacheDate")]
	public string GetCacheDate();
}
  1. Dynamically generate the proxy class using ProxyFactory.GetProxyInstance
var cache = ProxyFactory.GetProxyInstance<CacheService>();
  1. Directly call the method to execute the interception action on the attribute
var cache = ProxyFactory.GetProxyInstance<CacheService>();
Console.WriteLine(cache.GetCacheDate());

Simple Code:

  • Write Log
  • Permission Verification
  • Caching

https://ithelp.ithome.com.tw/upload/images/20180209/20096630BB4lN2NYOW.png

Packages

 
 
 

Contributors

Morty Proxy This is a proxified and sanitized view of the page, visit original site.