V
V
Vladimir Grabko2016-08-04 08:53:21
go
Vladimir Grabko, 2016-08-04 08:53:21

Does the implementation really smell?

I have a package for working with sessions. Now they are stored in a bolt, but if I want to switch to a radish, for example, then I will need to edit the code. I made an API for robots with storage

type SessidFileDriver struct {
  Get func(id string) (string, bool)
  Set func(id, data string) error
  Del func(id string) error
  Up  func(id, data string) error
}

Then I made 2 functions. One looks outward and takes 1 parameter func(SessidFileDriver). Sends functions to a global variable. And the second local to get functions anywhere. Then I made a driver for Bolt and in the New method I simply set the functions for working with the database. Then, if I want, I can very simply use any of the storages.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeniy Ivakha, 2016-08-11
@ivahaev

That's what interfaces are for. The wrapper over the Bolt must implement the necessary methods. Switch to Redis - write a wrapper for it.
Depending, for example, on the config, you will use one or another implementation of the interface.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question