SAP / spartacus

Spartacus is a lean, Angular-based JavaScript storefront for SAP Commerce Cloud that communicates exclusively through the Commerce REST API.
Apache License 2.0
736 stars 381 forks source link

Why doesn't use "protected" methods & properties instead of "private" in classes #9581

Closed alvteren closed 3 years ago

alvteren commented 3 years ago

Often I need extend any service or component for changing logic in one method. But I can't do this without "hack".

Last few cases:

Firstcase, our backend developer add few fields to cart items. Spartacus model is

export interface Item {
  product?: any;
  quantity?: any;
  basePrice?: any;
  totalPrice?: any;
  updateable?: boolean;
}

my model:

export interface CustomItem extends Item {
 weight: number;
 volume: number;
}

but property _items is private image

Second case, I want to extend ProductListComponentService and I want to add in method getQueryFromRouteParams my custom condition

image

if this method was protected, I would do

protected getQueryFromRouteParams({
    brandCode,
    categoryCode,
    promoCode,
    query,
  }: ProductListRouteParams) {
    if (query) {
      return query;
    }
    if (categoryCode) {
      return this.RELEVANCE_ALLCATEGORIES + categoryCode;
    }
    if (brandCode) {
      return this.RELEVANCE_ALLCATEGORIES + brandCode;
    }
    if (promoCode) {
      return this.RELEVANCE_ALLCAMPAINGS + promoCode;
    }
  }

But I have to replace whole class

Xymmer commented 3 years ago

This is mostly by design, @dunqan to provide more info.

Platonn commented 3 years ago

The extensibility of the ProductListComponentService has been improved in v2.1. See PR https://github.com/SAP/spartacus/pull/7987