[erlang-questions] How to code against an interface rather than an implementation?

H.C. v. Stockhausen hc@REDACTED
Fri Aug 9 09:41:12 CEST 2013


I need a DB backend for my application but I'd like to be able to swap
it out for different DBs if I choose so later on.

I would like to code against an interface and tell the application
what specific backend to use through config rather than code changes.

Is there a pattern for doing that? Hot code upgrades and multi-node
are not a real concern at this time - mostly, since I have no
experience yet with either - however if doing it right means taking
that into account too I'd also like to learn more about that.

I am thinking of defining a custom behaviour ("my_crud" perhaps), then
to implement it for various DBs and to also let a config driven
adapter implement it that I then use to throughout the code to talk to
the DB layer.

For example, using Mnesia and AWS DynamoDB:

- my_crud.erl (behaviour)
- my_db_mnesia.erl (implements behaviour)
- my_db_dynamo.erl (implements behaviour)
- my_db.erl (configurable adapter that also implements behaviour)
- my.config

my_db:insert(Key, Value).

Is that a reasonable approach that makes proper use of Erlang and
behaviours or is this just not how one should do it?

Thank you for any help & best regards,

More information about the erlang-questions mailing list