# Services

## What is service?

You can use a service to create messages and work with the db, and your code will be cleaner

## Export message to service

We recommend that you describe the message you will use in the controller in the service and retrieve it with `this.service.message`

{% code title="app.service.ts" %}

```typescript
import { Service } from 'nestgram';

@Service()
export class AppService {
  get helloMessage(): string {
    return 'Hello, world!';
  }
}
```

{% endcode %}

{% code title="app.controller.ts" %}

```typescript
import { OnCommand, Controller } from 'nestgram';
import { AppService } from './app.service';

@Controller()
export class AppController {
  constructor(private readonly appService?: AppService) {}

  @OnCommand('start')
  async start(): Promise<string> {
    return this.appService.helloMessage;
  }
}
```

{% endcode %}

## Working with db

We recommend to work with db in controllers. It will be more modular. If you'll use database work in services, we will provide you with a good work with it. You can read more about it [here](/nestgram/config/modules/mongo-module.md)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://degreetpro.gitbook.io/nestgram/nestgram-features/services.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
