Note: I began tinkering with this idea several months ago, inspired by some design work I was doing on a real-world project. Initially, I thought I could pimp up this stuff into a full-fledged article. Months came by and I didn't, so (in the spirit of my old concept of Blogging as Destructuring) I thought I could just as well say something here instead.
Consider a web site offering some kind of service to users. Users have to register (yeah :-), and they can then buy a subscription to several services. A simple class diagram can model this easily:
In most cases, the underlying database schema wouldn't be much different.
In a real case, there might be different kind of services, each requiring a derived class, but we can ignore this issue right now. Also, there might be several kind of subscription (time-based, pay-per-use, and so on), but again, let's ignore that issue right now, and just concentrate on time-base subscriptions.
A subscription can be renewed at any time. In the object model, this would translate into a Renew method in class Subscription. Renew could take a parameter, like the extension of the renewal. Most likely, it would add a new record to the Subscription table (to keep track of the whole subscription history for that user), and possibly create a new Subscription object. So far, so good.
Now the marketing guys come up with a nice idea: whenever you register, you can provide the email address of a friend who has already registered. If you do, everytime you renew a subscription your friend will get some kind of gift, like a free extension or whatever. This may generate a few more leads, meaning a little more business.
From a purely OO mindset, this may lead us to perform a little maintenance on the existing model. We can add a relationship from User to User to model the "friend" relationship, or we could just add a field like friendEmail (possibly left empty). At the database level, adding a field is probably easier.
We can also modify the Renew method to check for the presence of a friend in the subscribing user, and if so, invoke some Gift logic. I won't draw a modified diagram for this scenario: I guess it's just too obvious.
Now, this approach obviously works. However, you may recognize that the whole "friend gift" concept is a cross-cutting concern: it cuts through User (requiring new data) and through Subscription (requiring new logic). More on detecting cross-cutting concerns during analysis and design (and on the difference between cross-cutting and pervasive concerns) next time.
In the AOP world, we could approach the problem differently. We could define a FriendGift aspect. The aspect may add a new data member to the User class (the friendEmail), and intercept the persistence logic to save / load that data from the database. The aspect may also intercept Renew and perform the Gift logic if required.
Actually the aspect doesn't have to modify User (class and table); indeed, it would be better not to, as the persistence logic might not be easy to intercept (more on this next time). The aspect could just use a different database table to store the friend email.
Using an UML-like notation, we could model this approach as:
Interestingly, the database is probably different from the previous scenario: Friend would map to its own table.
What if we are not using an AOP-enabled language? For instance, we might be using plain old C#. Can we still borrow some ideas from the above? I believe so. In the same sense as OO thinking can inform traditional structured programming, AOP thinking can inform traditional object oriented programming.
If we don't have pointcuts, join points and aspects, we may still have events (in C#/.NET, or callback in other languages, and so on). Sure, we have to forego obliviousness (which might not be so bad: more on this next time). But we can come up with a more modular and decoupled design by reasoning along the AOP lines:
No rocket science here :-). Just a different form, same function. Different database, a more general Subscription class, unaffected by a business rule which may change at any time. Also the User class and table are left unaffected.
More on this, and a few comments on the SOA part, in just a few days (I hope :-).
5 comments:
This is an excellent and brilliant example of what is missing in SOA. In our extension of the SOA concept in Sense, we've built the concept of Emotion that is something similar to an Aspect that is applied in a distributed service environment. You make your architecture assimilating an Emotion that is a Crosscutting Concern that goes around all the deployed Services.
At the database level you can use triggers
In general I don't like triggers very much, beacuse they tend to break business logic between db layer and application layer: but triggers can be appropriate for some low level aspects (tracing, logging, security, etc.) particulary when the application could/should be unaware of them
Frank: interesting idea.
I took a quick tour of your website, looking for some code or diagram, just to see how you actually get to define and assimilate an emotion.
I didn't find anything; won't be a bad idea to add some hands-on, developer-oriented papers and examples...
Marco: yeap, a trigger would do the trick in many cases, but as you say, it's usually in the wrong layer. Or maybe the layers themselves are 30 years old and would need some rethinking ;-) [ok, that's partially tongue-in-cheek, but not so much].
Carlo, an inspired blog has just released about Sense and Emotion (AOP) here: http://mmondora.mondora.com/2008/04/soa-and-aop-make-sense.html
Post a Comment