Looking at my Handlers, I discovered that about half of my handlers use just 1 method of my datastore while the vast majority of the remaining handlers use 2 methods of my datastore. So I defined my Store interface with sub-interfaces where each sub-interface has only one method.
type Store interface {
EntryByIdGetter
EntryUpdater
UserByIdGetter
UserByNameGetter
UserByNameUpdater
EntriesByUserGetter
.....
}
type EntryByIdGetter interface {
EntryById(...) error
}
...
Then each implementation package can have a single exported New method like so.
func New(...) db.Store
Then my Handlers can specify exactly what they need like so.
type ViewEntryHandler struct {
Store db.EntryByIdGetter
...
}
For Handlers that use two methods, I can define an interface in that handler like so.
type Store interface {
db.EntryByIdGetter
db.EntryUpdater
...
}
type EditEntryHandler struct {
Store Store
...
}
This makes testing super easy and my documentation doesn't get cluttered with something like a UnsupportedOperationStore and its self documenting because it is easy to tell exactly what each handler does. I think this is where go really shines.
On Monday, February 25, 2013 8:33:55 AM UTC-8, Travis Keep wrote: