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.

[discussion]: improve the ``Controller`` annotation to becomme truly object-oriented

See original GitHub issue

Hello and a suggestion about Controller.

Controller is really useless in the real programming world, especially to handle RESTful routes. For example, a blog application has roughly the following requests:

Request Method Url
list blogs GET /blogs
create a blog POST /blogs
delete a blog DELETE /blogs/:id
show a blog GET /blogs/:id
update the title of a blog PUT /blogs/:id/title
update the content of a blog PUT /blogs/:id/content

Current solution

Currently, you have to write 4 classes:

@Controller('blogs')
export class BlogsController {
  @Get()
  list(): string {}

  @Post()
  create(): void {}
}

@Controller('blogs/:id')
export class BlogController {
  @Delete()
  delete(): void {}

  @Get()
  show(): string {}
}

@Controller('/blogs/:id/title')
export class BlogTitleController {
  @Put()
  updateTitle(): void {}
}

@Controller('/blogs/:id/content')
export class BlogContentController {
  @Put()
  updateContent(): void {}
}

This makes the code weird. And this has no object-oriented taste. Because the methods of the Controller class are isolated from each other, there is no side effect between them.

Improved solution

So, can we make the Controller class looks like more object oriented? A Controller a route.

Just like:

@Controller('Get', 'blogs')
export class ListBlogsController {
  onResponse() {}
}

@Controller('Post', 'blogs')
export class CreateBlogController {
  onResponse() {}
}

// ...

Then we can extend these classes to append more hook methods to becomme truly object-oriented:

@Controller('Post', 'blogs')
export class MyCreateBlogController extends CreateBlogController {
  private title: string = ""
  private content: string = ""

  onSessionCheck(): boolean {}

  onValidate(): boolean {
    // validate req.body.title and req.body.content 
    // return false if validate failure
    ...

    this.title = req.body.title;
    this.content = req.body.content ;
    return true;
  }

  onSave() {
    this.mysql.save(this.title, this.content);
  }

  onResponse() {}
}

This has several advantages:

  1. Each Controller class represents a route (request - to - response)

  2. The session check, data validation, storage, and response for each request can be written in separate methods.

  3. Each method can be tested separately.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jtmthfcommented, Mar 21, 2019

@tulayang They still don’t have to be in the same controller. This is how I’d organize the two routes.

// inside user module

@Controller('users')
export class UserController {
	constructor(private readonly userService: UserService) {}

	@Get(':id')
	getUser(@Param('id') id) {
		return this.userService.findById(id);
	}
}
// inside blog module

@Controller('users/:id/blogs')
export class UserBlogController {
	constructor(private readonly blogService: BlogService) {}

	@Get()
	getUserBlogs(@Param('id') id) {
		return this.blogService.findBlogsByUserId(id);
	}
}
0reactions
lock[bot]commented, Sep 23, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring @Controller Annotation with Example - GeeksforGeeks
Spring Controller annotation is typically used in combination with annotated handler methods based on the @RequestMapping annotation.
Read more >
16.11 Annotation-based controller configuration - Spring
In the following discussion, we'll focus on controllers that are based on annotated handler methods. The following is an example of a form...
Read more >
The Spring @Controller and @RestController Annotations
In this brief tutorial, we'll discuss the difference between @Controller and @RestController annotations in Spring MVC.
Read more >
controllers - 1.3.x - Play Framework
As the Model layer, controllers are written in pure Java, making it easy to access or modify ... The web's principles are not...
Read more >
Spring - MVC Framework - Tutorialspoint
The Spring Web MVC framework provides Model-View-Controller (MVC) ... request and calls the appropriate service methods based on used GET or POST method....
Read more >

github_iconTop Related Medium Post

No results found

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