Last active
July 31, 2017 04:59
-
-
Save sakapon/32e1a0d760558a951117c66c2aab7ecb to your computer and use it in GitHub Desktop.
ProxySample/ProxyableConsole/IProxyable_TResult.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ProxyableConsole | |
{ | |
public interface IProxyable<TResult> | |
{ | |
TResult Execute(); | |
} | |
class BodyProxyable<TResult> : IProxyable<TResult> | |
{ | |
Func<TResult> execute; | |
public BodyProxyable(Func<TResult> execute) | |
{ | |
this.execute = execute ?? throw new ArgumentNullException(nameof(execute)); | |
} | |
public TResult Execute() => execute(); | |
} | |
class AspectProxyable<TResult> : IProxyable<TResult> | |
{ | |
IProxyable<TResult> source; | |
Func<Func<TResult>, TResult> execute; | |
public AspectProxyable(IProxyable<TResult> source, Func<Func<TResult>, TResult> execute) | |
{ | |
this.source = source ?? throw new ArgumentNullException(nameof(source)); | |
this.execute = execute ?? throw new ArgumentNullException(nameof(execute)); | |
} | |
public TResult Execute() => execute(source.Execute); | |
} | |
public static class Proxyable | |
{ | |
public static IProxyable<TResult> Body<TResult>(Func<TResult> execute) => new BodyProxyable<TResult>(execute); | |
public static IProxyable<TResult> Aspect<TResult>(this IProxyable<TResult> source, Func<Func<TResult>, TResult> execute) => new AspectProxyable<TResult>(source, execute); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
namespace ProxyableConsole | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var result = Proxyable.Body(() => | |
{ | |
Console.WriteLine("Body"); | |
return 123; | |
}) | |
.Aspect(f => | |
{ | |
Console.WriteLine("Before"); | |
var r = f(); | |
Console.WriteLine("After"); | |
return r; | |
}) | |
.Execute(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment