Help and Information about Aspect Oriented Programming

前端 未结 5 879
挽巷
挽巷 2020-12-23 12:40

I\'m a newcomer to the idea of aspect-oriented programming but I would like to explore the idea of using it on my project for handling logging, reporting, etc. To this end

5条回答
  •  不知归路
    2020-12-23 12:59

    I can't speak for the specifics of .NET, but AOP and the more general idea of being able to attach hooks to arbitrary methods, is a handy technique that can solve some otherwise hairy problems.

    One example is design by contract. Let's say you have a bunch of methods upon which you want to apply some common contracts. You can add some "advice" (the AOP term) before and after each method is called without having to cut & paste it into every method.

    While testing, it is often useful to know what is going on in some internal method. How many times it was called and maybe what it returned. You can add an aspect to do that monitoring without having to add distracting testing code to the method itself. Editing the code for that method might not even be possible.

    Just watch out for how aspects are implemented. Some implementations are elaborate pre-processors which may make debugging your code more complicated. Others hook smoothly into the language. Dynamic languages handle AOP very well. Perl has Aspect.pm for AOP and the more general Hook::LexWrap to accomplish method hooks.

提交回复
热议问题