Tapestry Training -- From The Source

Let me help you get your team up to speed in Tapestry ... fast. Visit howardlewisship.com for details on training, mentoring and support!

Tuesday, August 12, 2003

HiveMind Deferred Service Proxies

I'm really coming to love Javasssist. I've called it a giant hammer searching for nails and I am finding a lot of nails to hit with it.

One thing that was bugging me in HiveMind was that if you create a service, and that service uses some other service (that is, you set a property using the <set-service> property setter), then the other service will be constructed. And so on, and so on. Of course, the service could look up the second service just as needed, but that's bad from both the unwanted-coding perspective and the Inversion of Control perspective (or perhaps those two are perfectly aligned)?

In other words, I want to be able to assign the service as a property (of another service) without actually constructing the service. Fortunately, services are hidden behind interfaces, so all I need is a implementation of the service that looks like:

private syncnhronized TheServiceInterface _service()
{
  if (_service == null)
    _service = _serviceExtensionPoint.constructServiceImplementation();

  return _service;
}

public Foo serviceMethod(...)
{
  return _service().serviceMethod(...);
}

And, of course, where does that implementation come from? Brewed up on the fly by Javassist. So now I can have my cake and eat it too ... all the worrying about when to instantiate classes falls back into the HiveMind framework domain, and is of no concern to the HiveMind user, who simply has an interface and is free to invoke methods on it.

No comments: