Skip to Content

MVCS Best Practice - Resource Layer

Table of Contents

Introduction

Resources are the entry point for your application. It does several things:

  • It takes in the request received from transport protocol (http, grpc, etc).
  • Maps protocol level request struct to service level request struct. The mapping helps to decouple API level model from the internal model, so if API level has any change the impact will be limited.
  • Invoke dependent services to perform business logic using the request and return the internal response struct
  • Map internal response to external response struct and return the response.

All resources should use CircleResource to standardize your resource

  • CircleResource includes a version for every resource and integrates error handling and success body marshaling through the CircleResource.Handle()
  • Example of CircleResource can be found here
example.go
type Resource struct { circleResource response.CircleResource }

UUID field should use string as field type and cuuid as the validator for the field

  • cuuid is a custom validator that checks if the UUID is valid or not
  • string type allows the validator to handle both format of uuid with/without dash and leverage validator.ValidationErrors. See thread for more detail

Its only dependency should be other services.

  • Resource layer should only depend on service. For example, It should not directly depend on any provider or any repository
  • If you find yourself needing to depend on provider or repository, it is a sign that you are not following the MVCS pattern correctly.

It should contain as little biz logic as possible

  • It should not contain complex validation logic or business logic, such as query other service for eligibility or check database to verify resource existence.
  • It should only contain the logic to map the request to service request and service response to the response.

Use response.CustomError in most cases

  • For handling for successful resource creation, you can use response.Success or response.OK
Last updated on