question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

(question) How to inherit exposed controller http methods?

See original GitHub issue

Hi, It’s possible to inherit parent controller methods? I tried to create kind of RestController with CRUD methods, but the child seems to not get any of them.

Any idea?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (7 by maintainers)

github_iconTop GitHub Comments

20reactions
kamilmysliwieccommented, Feb 15, 2018

This feature is available since v4.6.0 🎉

17reactions
jgjcommented, Nov 28, 2017

I have this exact use case and was surprised to find it didn’t work. A more concrete example (Using TypeORM):

export abstract class EntityController<T> {
    constructor(protected readonly service: EntityService<T>) { }

    @Get()
    async findAll() {
        return this.service.findAll();
    }

    @Get(':id')
    async getById(@Param() params) {
        return this.service.getById(params.id);
    }

    @Post()
    async create(@Body() data: Partial<T>) {
        return this.service.create(data);
    }
}

Every single one of my TypeORM entities should expose basically the same restful CRUD interface and, using generics, be type safe about the underlying Entity model. I’ve already done this with the service that wraps the typeorm repository (EntityService<T>). An extending controller looks like:

// ThingService extends EntityService<Thing>

@ApiUseTags('things')
@Controller('things')
export class ThingController extends EntityController<Thing> {
    constructor(private readonly thingService: ThingService) { super(thingService) }
}

This seems like a perfectly valid pattern to me, and one which will save me tons of boilerplate/copy and pasting. None of the essential CRUD logic differs between any of my entities. And, if it did, the most appropriate place to handle it would be the Service or Middleware/Interceptors/Pipes/Guards, not the controller. The value of REST is that the interface is based on the shape of the state being transferred and a set of known verbs. The verbs don’t change between controllers and the shape of the state is determined by the generic type argument.

If there is a limitation or difficulty in supporting this pattern I understand not being willing to support it. But to say there isn’t a valid use case is unfair.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Inherit http verb attributes from abstract REST controller
The controller route attribute on the base class is inherited though (as stated in the docs).
Read more >
17. Web MVC framework - Spring
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that dispatches requests to handlers, with configurable handler ...
Read more >
40+ Web API Interview Questions and Answers (2023)
There are many HTTP methods like GET, POST, PUT, etc., which can return ... You can download a PDF version of Web Api...
Read more >
ASP.NET MVC Controller Overview (C#) - Microsoft Learn
Because a controller inherits from this base class, a controller inherits several useful methods for free (We discuss these methods in a moment) ......
Read more >
Developer Guide: Scopes - AngularJS: API
Scope is the glue between application controller and the view. ... Controllers use scopes to expose controller methods to templates (see ng-controller).
Read more >

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found