aesteve / nubes

Annotation layer on top of Vert.x 3
Apache License 2.0
121 stars 35 forks source link

Provide Mixins #38

Open aesteve opened 9 years ago

aesteve commented 9 years ago

Allow users to compose their controllers with mixins so that they can reuse logic.

For example, they could declare a LoggingMixin this way :

@Mixin("log")
public class LogMixin {
    @Before
    public void timeBefore(RoutingContext context) {
        context.put("timeBefore", System.currentTimeMillis());
        context.next();
    }

    @After
    public void logAfter(RoutingContext context) {
        long timeAfter = System.currentTimeMillis();
        long timeBefore = Long.valueOf(context.get("timeBefore"));
        logger.info("time for request #0", timeAfter - timeBefore);
        context.next();
    }
}

Then in MyApiController :

@Controller("my/api")
@Includes("log")
public class MyApiController {
    @GET
    public void myApi(RoutingContext context, Payload<String> payload) {
         payload.set("Hello");
         context.next();
    }
}