platform icon indicating copy to clipboard operation
platform copied to clipboard

Quick explanation of how to normalize data from backend

Open Kleinhummel opened this issue 7 years ago • 8 comments

I know that there is already an issue for normalizing state, but I am still unclear about how to normalize the state from my backend. Is this the preferred workflow?: Component ----(A)GetPosts----> GetPostsEffect (Fetches Posts from backend and normalizes them with normalizr) ----(A)GetPostsSuccess----> PostsReducer|CommentsReducer (Update their state)

So should the PostsReducer and the CommentsReducer both handle the GetPostsSuccess-Action? Or should another Effect (GetPostsSuccessEffect) handle the GetPostsSuccess-Action and dispatch the AddPosts-Action and the AddComments-Action?

Can I find a working example for this scenario anywhere? A quick explanation or a short example would help me a lot!

Kleinhummel avatar Oct 09 '19 07:10 Kleinhummel

Yes, you should go for the latter one. Why? Because it is better reuseably, better traceable and leads to a better encapsulated architecture.

So use the following:

  • Actions
    • loadPosts
    • loadPostsSuccess
    • upsertPosts
    • upsertComments
  • Reducers
    • upsertPosts
    • upsertComments
  • Effects
    • loadPosts (triggering data access service)
    • normalizePosts (triggered by loadPostsSuccess and triggering the two Actions upsertPosts and upsertComments)

That way you can reuse the elementary actions upsertPosts and upsertComments easily. The normalizePosts Effect should not implement the normalization logic directly, but use a service implementing this.

Hope this helps. For more details please post on stackoverflow - it's a better fit to discuss things there as this is not really a ngrx issue.

mikezks avatar Oct 13 '19 13:10 mikezks

...and use Selectors if you need nested data in your components.

mikezks avatar Oct 13 '19 13:10 mikezks

Yes, you should go for the latter one. Why? Because it is better reuseably, better traceable and leads to a better encapsulated architecture.

So use the following:

  • Actions

    • loadPosts
    • loadPostsSuccess
    • upsertPosts
    • upsertComments
  • Reducers

    • upsertPosts
    • upsertComments
  • Effects

    • loadPosts (triggering data access service)
    • normalizePosts (triggered by loadPostsSuccess and triggering the two Actions upsertPosts and upsertComments)

That way you can reuse the elementary actions upsertPosts and upsertComments easily. The normalizePosts Effect should not implement the normalization logic directly, but use a service implementing this.

Hope this helps. For more details please post on stackoverflow - it's a better fit to discuss things there as this is not really a ngrx issue.

But in this talk Mike Ryan highlighted that reusing actions is bad! So don't you think that the first version might be more meaningful?

Kleinhummel avatar Oct 14 '19 07:10 Kleinhummel

The first version would lead to a completely different (means more complex) payload signature. I understand @MikeRyanDev 's talk about reusing Actions, that you get benefits by using different Action types, so that you know which part of the application dispatched the Action. Nevertheless the payload stays the same and therefor also the reducer logic on how to process the state change.

mikezks avatar Oct 14 '19 07:10 mikezks

But to make this clear: using a new Action type makes is necessary to define a new Action, but nevertheless they can share the same payload type and therefor you do not need to implement similar or same Reducer logic twice.

mikezks avatar Oct 14 '19 07:10 mikezks

But to make this clear: using a new Action type makes is necessary to define a new Action, but nevertheless they can share the same payload type and therefor you do not need to implement similar or same Reducer logic twice.

Ahh, ok. Now I understand. So maybe a good approach would be to use different Actions for different events but to use the same reducer "case", right?

Kleinhummel avatar Oct 14 '19 08:10 Kleinhummel

Yes, right - I would do it that way.

Also look at the fabulous blog post by @vsavkin. Read it once, read it twice, read it several times - I did the same and it is really beneficial to get into ngrx advanced patterns.

mikezks avatar Oct 14 '19 08:10 mikezks

Yes, right - I would do it that way.

Also look at the fabulous blog post by @vsavkin. Read it once, read it twice, read it several times - I did the same and it is really beneficial to get into ngrx advanced patterns.

Thank you very much. I will read the post!

Kleinhummel avatar Oct 14 '19 08:10 Kleinhummel