mardi 31 janvier 2017

Is having an ActiveResource model which is persisted on a local db a good practice?

Hello I have been using Rails for quite some time and am looking for a good design pattern for structuring my code.

How I am want to do it:

I have a resource which resides on an API and I need to persist it locally because of dependencies on the API from my models (business logic).

I was thinking of creating an ActiveResource model and having a "manager" type architecture which would persist my objects and create a different object model for interacting with those internal objects (cached resource objects essentially).

The API service has web hooks to notify me of any changes to the objects which I can then handle and update locally.

The way I am currently doing it

The way I have architected it is kind of the way I have been doing it and not sure if it is a poor design:

Currently I have a mongoid persisted object which has after_create and after_save hooks to make the api calls and create persist the object.

I currently prevent the object from being persisted if an invalid response is returned from the API. This way I avoid getting into an invalid state between the API and the local persisted object.

I am curious to hear opinions of what people have done, and what they think of this approach.

Aucun commentaire:

Enregistrer un commentaire