samedi 2 novembre 2019

What the best design pattern for passing object?

I have some problem with designing code architecture... I'm writing web application on Golang with SQL storage. I have two entities: User, Location.

type User struct {
    id uint64
    ....
    locationID uint64
    ....
    valueA uint
    lastValueAUpdateTimestamp time.Time

}
type Location struct {
   id uint
   ....

}

In entity "User" I have dynamical parameter (valueA) that updated through time. Updating valueA depends on lastValueAUpdateTimestamp and user location parameters..

What's the best way to pass Location object to User, based on user locationID? I have several options:

  1. Pass Location registry object, that have functions to get location object from database. This option have two serious disadvantages:
    • Sometimes valueA isn't necessary, but for every User object creation we should pass this registry...
    • Sometimes in future we can add registry object to another entities, and we will get "big" constructor.
  2. Hide all methods in user object linked with ValueA, create new inherited struct that receive location registry object in constructor, and provide access to methods linked with ValueA..

Aucun commentaire:

Enregistrer un commentaire