Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add abstract class to easy DI for Routers defined with RoutingDsl #9067

Open
octonato opened this issue Feb 28, 2019 · 0 comments
Open

Add abstract class to easy DI for Routers defined with RoutingDsl #9067

octonato opened this issue Feb 28, 2019 · 0 comments

Comments

@octonato
Copy link
Member

This came to the surface while documenting Additional Routers in Lagom.

It's not easy to encode a Router using the RoutingDsl and have a specific type that we can later use in Lagom.

For instance, Lagom has the following API to add Additional Routers:

bindService(
  HelloService.class, HelloServiceImpl.class,
  additionalRouter(FileUploadRouter.class)
);

Where additionalRouter expects a play.api.routing.Router.

If we want to encode a router for it, we need to write:

class FooRouter implements SimpleRouter {

  private final Router delegate;
  
  @Inject
  public FooRouter(RoutingDsl routingDsl) {
    this.delegate = 
      routingDsl.GET("/foo")
                .routingTo( __ -> ok("foo"))
                .build().asScala();
  }

  @Override
  public PartialFunction<RequestHeader, Handler> routes() {
    return delegate.routes();
  }
}

Of course, there are other means of doing that, but none are less verbose than this.

One possible solution is to add an abstract class that removes most of the boiler-plate.

abstract class FromRoutinDls implements SimpleRouter {

  private final Router delegate;
  
  public FromRoutinDls(RoutingDsl routingDsl) {
    this.delegate = buildRouter(routingDls).asScala();
  }

  public abstract play.routing.Router buildRouter(RoutingDsl routingDsl)

  @Override
  public PartialFunction<RequestHeader, Handler> routes() {
    return delegate.routes();
  }
}

The user implementation will look like:

class FooRouter extends FromRoutinDls {
  
  @Inject
  public FooRouter(RoutingDsl routingDsl) {
    super(routingDsl);
  }

  public play.routing.Router buildRouter(RoutingDsl routingDsl) {
    routingDsl.GET("/foo")
              .routingTo( __ -> ok("foo"))
              .build();
  }
}

The user is then exposed to fewer concepts. There is no concept of delegates nor is the Scala API leaked to the Java code. The user only needs to define the router on the buildRouter method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant