You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

Tapestry Inversion of Control Container

Why do I need to define an interface for my services? Why can't I just use the class itself?

First of all: you can do exactly this, but you lose some of the functionality that Tapestry's IoC container provides.

The reason for the split is so that Tapestry can provide functionality for your service around the core service implementation. It does this by creating proxies: Java classes
that implement the service interface. The methods of the proxy will ultimately invoke the methods of your service implementation.

One of the primary purposes for proxies is to encapsulate the service's lifecycle: most services are singletons that are created just in time. Just in time means only as soon
as you invoke a method. What's going on is that the lifecycle proxy (the object that gets injected into pages, components or other service implementations) checks on each method invocation
to see if the actual service exists yet. If not, it instantiates and configures it (using proper locking to ensure thread safety), then delegates the method invocation to the service.

If you binding a service class (not a service interface and class), then the service is fully instantiated the first time it is injected, rather than at that first method invocation. Further, you
can't use decorations or method advices on such a service.

The final reason for the service interface / implementation split is to nudge you towards always coding to an interface, which has manifest benefits for code structure, robustness, and testability.

  • No labels