Chapter 3. Adding Services to APIs

In the previous chapter, we took a look at Spring Python's Inversion of Control container, and used it to exchange production objects with test doubles. One of the most prolific paradigms used today is Object Oriented Programming (OOP) which uses classes. Classes serve as representations of the domain and business problems that we solve. But this isn't enough!

We also need robust features such as caching, storage and retrieval, transactions, security, and logging. These services are orthogonal to the business problems that we must solve.

Mixing classes with these orthogonal services can quickly become costly to code correctly as well as maintain. Aspect Oriented Programming (AOP) solves these orthongal problems through a new type of modularization called an aspect.

In this chapter, we will learn:

  • AOP from 10,000 feet
  • Adding caching to Spring Python objects
  • Extending the example by adding performance and security advisors
  • How AOP is a paradigm and not just a library
  • The distinct features of Spring Python's AOP, compared to other libraries as well as Python language features
  • The risks of AOP and how to mitigate them by showing how to test our aspects
  • Some advice about applying Advice

AOP from 10,000 feet

Throughout this chapter, we will explore examples of AOP and how to use them. To do that, it's important that we learn some basic definitions.

Note

Crosscutting problems impact more than one hierarchy of classes. AOP efficiently solves these problems.

Commonly cited examples of crosscutting problems include security, transactions, tracing, auditing, and logging. What do you think makes these concerns crosscutting?

Crosscutting versus hierarchical

A common cycle of work often starts with a single issue. How would you solve a single, concrete, well defined issue? By coding a single, concrete solution! We usually don't think of reuse at this time. We are more likely to think of reuse when we need to solve a newer problem using an already-written block of code.

When we are solving another problem and trying to reuse a block of code, we start to see how we can make adjustments and abstractions. We don't just abstract things to reduce duplication of code. We are also adjusting our definition of the business solution with a more refined, higher level concept. As new requirements come in, our business solution hierarchy evolves.

As we continue solving business issues, it is not unusual to run into infrastructure issues. For example, the need to wrap operations with transactions may arise even though the customer may not have asked for it.

Infrastructure issues can easily crosscut hierarchies of classes. SQL transactions are the perfect example. Manually coding the transactional pattern in every method that needs it would lead to a lot of code duplication. And then what do we do when the transactional requirements change? Rewrite all impacted methods? Unforeseen changes in requirements could easily accrue a of work as we maintain the code. And to top it off, what are the odds that we will code the transaction pattern correctly in every method?

AOP provides another means to address this issue without using copy-and-paste tactics. We will explore this is in more detail throughout this chapter.

Crosscutting elements

The following terms describe the crosscutting elements that make up aspect oriented programming:

Join point

A Join point is an identifiable point in the execution of a program. This is the place where crosscutting actions are woven in.

Pointcut

A Pointcut is a program construct that selects join points and collects context at that point.

Advice

Advice is code that is to be executed at a join point that has been selected by a pointcut.

Aspect

An Aspect is to AOP what a class is to OOP. It is the central programming construct where a set of pointcuts and advice are defined.

Advisor/interceptor

An Advisor or interceptor is Spring Python's mechanism for implementing an aspect.

With these terms defined, we will dig in through the rest of this chapter to explore crosscutting problems and their solutions.

Weaving crosscutting behavior

As stated earlier, aspects contain pointcuts and their associated advice. Applying aspects to our code is known as weaving.

Different systems provide various ways to weave. AspectJ is a Java-based AOP solution that has both static and dynamic weaving.

Tip

AspectJ is the trail blazer of AOP and targets the Java platform. Spring Python doesn't necessarily integrate with AspectJ, nor attempt to mimic everything AspectJ does. Instead, Spring Python looks at AspectJ as a mature example of implementing AOP.

For more information about AspectJ, you could read AspectJ in Action by Ramnivas Laddad. While that book focuses on AspectJ and Java, it also has much more detail about the concepts of AOP.

Static weaving is applied at same time that code compilation occurs. Dynamic weaving is applied to already compiled code.

Spring Python provides dynamic weaving through the use of proxies. Proxies are configured using the IoC container. Calls that are normally targeted at the object are instead routed through the proxy. The proxy evaluates possible pointcuts and applies Advice. This is also known as intercepting.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.15.144.56