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

ノートン ジョーセフ ウェイ ン norton@REDACTED
Fri Aug 9 18:21:39 CEST 2013


Hans -

Hi.  I wrote an application that provides an ETS-like interface with a callback interface for a backend implementation.  This application (https://github.com/norton/gen-ets) won't fit your needs but it may provide you with an example.

As others have mentioned, I'd try to first implement one DB API and implementation that fits your needs to gain experience.  You might even find out later that you don't really need a generic layer.

Cheers,

Joe N

On 2013/08/09, at 16:41, H.C. v. Stockhausen <hc@REDACTED> wrote:

> Hello,
> 
> 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,
> Hans
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions




More information about the erlang-questions mailing list