When an API's data structure does not fit well with an angular app, I am trying to determine the best-practice in my specific design and implementation for converting it to a usable structure.

I was doing this on an 'as needed' basis within the app controllers/directives, which quickly lead to spaghetti code.

Now I'm thinking this belongs in the service/factory which interacts with the API. Are there drawbacks or benefits to the Factory pattern over a more service oriented architecture when it comes to handling data structure conversion?

  • 3
    This seems like far too generic a question. The right structure for sending data to an API depends upon a whole lot of things that are specific to the data, the application, how the data is normally consumed, how the data is transmitted and the specific API function, etc... For example, a Javascript API takes arguments and objects with properties, but a REST API requires most things in string form which may mean JSON.
    – jfriend00
    Commented Aug 25, 2014 at 22:57
  • I'm not asking what specific structure I should use. I'm asking how/where it should be handled.
    – hal
    Commented Aug 25, 2014 at 22:59
  • 2
    Again, this is too generic a question. The app should structure its data in what makes the most sense for the app. There are many considerations that go into determining what makes the most sense. How the data must be organized for communicating with an API is only one of those considerations. How the data is used within the app is likely more important, but is just yet another consideration. How the data is stored persistently (if stored persistently) is yet another consideration, security might be another consideration and so on...
    – jfriend00
    Commented Aug 25, 2014 at 23:06
  • 1
    Why was my question closed as too broad? Commented Aug 26, 2014 at 1:51
  • 1
    @david.pfx: It was your "if I could" that triggered my comment. Just a comment that you disagree with the close votes/reason and an explanation why is usually enough for me to take a second look at the question before casting my vote while reviewing. Commented Aug 26, 2014 at 14:16

1 Answer 1


Yours is a common-enough problem that the solution is has a name: an anticorruption layer. This is a layer that translates between an external API with an undesirable structure and the desired internal structure of your app. The idea is to limit the "corruption" of the undesired structure to as small a part of your code as possible.

The external API doesn't have to be bad in an absolute sense, just not ideally suited for your particular application. If you can think of a better API, by all means go for it. The earlier you do, the cleaner your code will be.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.