MVCS Best Practice - Resource Layer
Table of Contents
- Introduction
- All resources should use CircleResource to standardize your resource
- UUID field should use string as field type and cuuid as the validator for the field
- Its only dependency should be other services
- It should contain as little biz logic as possible
- Use
response.CustomErrorin most cases
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
CircleResourceincludes a version for every resource and integrates error handling and success body marshaling through theCircleResource.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
cuuidis a custom validator that checks if the UUID is valid or notstringtype 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.
Resourcelayer should only depend onservice. For example, It should not directly depend on anyprovideror anyrepository- If you find yourself needing to depend on
providerorrepository, 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.Successorresponse.OK
Last updated on