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

Chris Cook <>
Fri Aug 9 10:30:19 CEST 2013


Morning Hans,

I abstracted the interface out to a separate layer when I had to do this
and set the chosen db as state in startup, so you'd have something like:

my_interface:get_content/2 -> (State#state.db):get_content/2  ->
my_db_mnesia:get_content/2

              -> my_db_dynamo:get_content/2

              -> my_db:get_content/2

hope this kind of help and along the right lines.

Regards

Chris




On 9 August 2013 08:41, H.C. v. Stockhausen <> 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
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130809/e6853f75/attachment.html>


More information about the erlang-questions mailing list