我只是看着这个演讲由Greg Young的警告人们KISS:保持简单愚蠢。
其中一个建议,他的事情是做面向方面编程,一个也不会需要一个框架。
他首先提出了一个严格的约束条件:所有方法只能使用一个参数,并且只能使用一个参数(尽管他稍后通过使用部分应用程序来放松此参数)。
他给出的示例是定义一个接口:
public interface IConsumes<T>
{
    void Consume(T message);
}
如果我们要发出命令:
public class Command
{
    public string SomeInformation;
    public int ID;
    public override string ToString()
    {
       return ID + " : " + SomeInformation + Environment.NewLine;
    }
}
该命令实现为:
public class CommandService : IConsumes<Command>
{
    private IConsumes<Command> _next;
    public CommandService(IConsumes<Command> cmd = null)
    {
        _next = cmd;
    }
    public void Consume(Command message)
    {
       Console.WriteLine("Command complete!");
        if (_next != null)
            _next.Consume(message);
    }
}
要将日志记录到控制台,只需实施以下一项:
public class Logger<T> : IConsumes<T>
{
    private readonly IConsumes<T> _next;
    public Logger(IConsumes<T> next)
    {
        _next = next;
    }
    public void Consume(T message)
    {
        Log(message);
        if (_next != null)
            _next.Consume(message);
    }
    private void Log(T message)
    {
        Console.WriteLine(message);
    }
}
然后,命令前日志记录,命令服务和命令后日志记录就是:
var log1 = new Logger<Command>(null);
var svr  = new CommandService(log);
var startOfChain = new Logger<Command>(svr);
该命令由以下命令执行:
var cmd = new Command();
startOfChain.Consume(cmd);
为此,例如在PostSharp中,可以这样注释CommandService:
public class CommandService : IConsumes<Command>
{
    [Trace]
    public void Consume(Command message)
    {
       Console.WriteLine("Command complete!");
    }
}
[Serializable]
public class TraceAttribute : OnMethodBoundaryAspect
{
    public override void OnEntry( MethodExecutionArgs args )
    {
        Console.WriteLine(args.Method.Name + " : Entered!" );   
    }
    public override void OnSuccess( MethodExecutionArgs args )
    {
        Console.WriteLine(args.Method.Name + " : Exited!" );
    }
    public override void OnException( MethodExecutionArgs args )
    {
        Console.WriteLine(args.Method.Name + " : EX : " + args.Exception.Message );
    }
}
Greg使用的参数是,从属性到属性的实现之间的联系“太神奇了”,无法解释初级开发人员的情况。最初的示例全部是“正义代码”,并且易于解释。
因此,在经历了漫长的积累之后,问题是:您何时从Greg的非框架方法转向使用PostSharp这样的工具进行AOP?
IConsumes部分。不必使用外部XML或某些Fluent接口---还有另一件事需要学习。有人可能会认为这种方法论也是“另一件事”。