ThreeDotsLabs / wild-workouts-go-ddd-example

Go DDD example application. Complete project to show how to apply DDD, Clean Architecture, and CQRS by practical refactoring.
https://threedots.tech
MIT License
5.04k stars 464 forks source link

Creating Trainings directly #35

Closed wlopez-enkoding closed 2 years ago

wlopez-enkoding commented 2 years ago

Hey guys ! I was wondering why the Training struct is public... I mean, what happen if someone creates a Trining bypassing all required validations that exist in NewTraining ?

Thank you !

https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/c7273256574c598760498743d8ffa28b1b35acee/internal/trainings/domain/training/training.go#L10

m110 commented 2 years ago

Hey @wlopez-enkoding!

The Training has to be public so other packages are able to reference it. It's not used directly, but converted into other specific types, like here: https://github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/blob/c7273256574c598760498743d8ffa28b1b35acee/internal/trainings/adapters/trainings_firestore_repository.go#L117

You can create a Training outside of the pacakge, but since all its fields are unexported, you won't be able to fill them. So in worst-case scenario, you'll create an empty training.

To make sure the passed value is valid, you can keep a check like if training == Training{} or have a dedicated training.IsZero() method.