Search code examples
angularsap-commerce-cloudspartacus-storefront

What are the usage difference between OccAdapters and Services?


I have a question about Spartacus. I use 20.05 version.

this.occCartAdapter.load('xxxxx@gmail.com', '00001002').subscribe(data => {
  console.log(data);
});

this.cartService.getActive().subscribe(data => {
  console.log(data);
});

These two function gets same result at the end. What are they differences? When i try to debug these functions from backend I can only catch(debug) occCartAdapter. Why cartService is not trigger backend?


Solution

  • There are several layers in Spartacus that are responsible for loading, storing and exposing backend data. This is for a healthy layered architecture as well as for fine-grained extensibility. You can learn more about these layers at https://sap.github.io/spartacus-docs/connecting-to-other-systems/.

    Generally speaking, the cartService is representing the facade layer of the cart domain, where as the occCartAdapter represents the default OCC adapter for the cart domain. In case you like to adapt another system, you could override/customise the adapter without worrying about storing, exposing, etc.

    The data will flow through all those layers. All domains (product, cart, user) are using the following layers:

    -> component
        -> (component service)
            -> service (AKA facade) 
                -> ngrx-action 
                    -> ngrx-effect
                        -> connector 
                            -> adapter
                                -> angular 
                                    -> http client (angular)
                                        -> API (OCC)
    
    

    It is not recommended to interact directly with lower level services directly, as the central store would no be aware of the data. You'd stop having the advantage of storing data centrally.

    That begin said, for new functionality, you definitely won't need to replicate these layers. The layers are partially in place for fine-grained extensibility, which is not need for your customisations. The minimum layers i'd recommend is a (singleton) service that holds the data for your component.