mercredi 1 février 2023

Exported types in Repository-Service-Controller Pattern

Skip below section, if you already know the Repository-Service-Controller pattern.


Brief explanation of Repository-Service-Controller pattern for context.

In a go web application, the following is the code organization-

main.go

server
|__clientdata.go

clientdata
|__controller.go
|__service.go
|__repository.go

main.go is the entry point. It creates a server mux object and calls the receiving methods defined on it in the server package. These methods define routes/endpoints and map them to their respective handlefuncs exposed by controller.go in their respective packages (here, package clientdata).

Then, the controller calls the service which in turn calls the repository- all of which are in the same package.


But, why are the service and repository methods exported types (ie, capitalized first letter) when in this pattern, they are called by the controller which is in the same package?

Isn't it a bad practice to export unnecessarily?

The question :-

So, why are they exported? Is it alright if I don't export them?

Aucun commentaire:

Enregistrer un commentaire