Generic ID type for "Clean Architecture" Go program -


i trying find proper type ids in go program designed using uncle bob martin's "clean architecture".

type userid ...  type user struct {   id userid   username string   ... }  type userrepository interface {   findbyid(id userid) (*user, error)   ... } 

i following uncle bob martin's "clean architecture", code organized set of layers (from outside-in: infrastructure, interfaces, usecases, , domain). 1 of principles dependency rule: source code dependencies can point inwards.

my user type part of domain layer , id type cannot dependent on database chosen userrepository; if using mongodb, id might objectid (string), while in postgresql, might use integer. user type in domain layer cannot know implementing type be.

through dependency injection real type (e.g. mongouserrepository) implement userrepository interface , provide findbyid method. since mongouserrepository defined in interfaces or infrastructure layer, can depend on definition of userrepository in (more inward) domain layer.

i considered using

type userid interface{} 

but compiler not helpful if code in 1 of outer layer tries assign in incorrect implementation type.

i want have interfaces layer or infrastructure layer, database specified, determine , require specific type userid, cannot have domain layer code import information, because violate dependency rule.

i considered (and using)

type userid interface {     string() string } 

but assumes knowledge database use strings ids (i using mongodb objectid -- type synonym string).

how can handle problem in idiomatic fashion while allowing compiler provide maximum type safety , not violate dependency rule?

maybe can use this:

type userid interface {   getvalue() string   setvalue(string) } 

then assume passing , getting string id (it can stringified version of integer id in case of pgsql , other rdbmss), , implement userid per database type:

type pguserid struct {     value int }  func (id *pguserid) getvalue() string {     return strconv.itoa(id.value) }  func (id *pguserid) setvalue(val string){     id.value = strconv.atoi(val) }  type mongouserid struct {     value string }  func (id *mongouserid) getvalue() string {     return value }  func (id *mongouserid) setvalue(val string){     id.value = val } 

i wonder if accomplishes want achieve, maybe it's more elegant hide string conversions inside userid?


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -