0

I want to inject a grandchild Service in the root Module like this:

providers:[MyAppTables]

My services structure is like this:

@Injectable()
export class TableBase {

  protected _items: BehaviorSubject<any[]> = new BehaviorSubject([]);
  public items: Observable<any[]> = this._items.asObservable();

  constructor(){
    this.init([]);
  }

  init(newItems:any[]) {
    this._items.next(newItems);
  }
}

@Injectable()
export class TableProducts extends TableBase {

  constructor(@Inject(TableBase) __base: any ){
    super();
  }

  init() {
    super.init([{id:1,name:'A'},{id:2,name:'B'}]);
  }
}

@Injectable()
export class MyAppTables{
  public products: Observable<any[]>;

  constructor(@Inject(TableProducts) prod: any ){
    this.products = prod.items;
  }
}

So far I only could make it work like this

provider:[TableBase, TableProducts, MyAppTables]

my question is: there is a better way to inject this?

Note: this example only shows 1 parent, the idea is to abstract the base code in the grandparent class, make a parent class for each table and include that service where is needed

ElBarto
  • 387
  • 2
  • 12
  • what do you mean by **grandchildren service** – Aravind Mar 10 '17 at 18:57
  • 1
    A classC (grandchild) that extends classB where classB extends classA (grandparent). Angular2 needs all in a provider (or Injected, I'm new at this) – ElBarto Mar 10 '17 at 19:00
  • 1
    you write services like this? extending one another? – Aravind Mar 10 '17 at 19:02
  • just in this case, so I don't have to copy the same exact code in all 12 services – ElBarto Mar 10 '17 at 19:03
  • are you sure you need inheritance to share code between services? Couldn't you accomplish the same by creating helper modules/functions/classes? – snorkpete Mar 10 '17 at 19:04
  • then you can **use the same service across the application injecting it locally** – Aravind Mar 10 '17 at 19:04
  • @Aravind this is an example with no methods, but the BaseService will have common methods and the middle will have especific table methods. Finally the suscriber must saw a suscribable list, not the service implementation. It's a really specific implementation – ElBarto Mar 10 '17 at 19:06
  • @Snorkpete, I was in the belief that inheritance was better. Nevertheless if a create a global Service that inherit from another I have to include both in the Root module, I want the last one there, and the others on the correspondant module – ElBarto Mar 10 '17 at 19:11
  • **specific implementation** seems to be a bad practice unfortunately. When you have a global service that has all your global methods, inject to all the services and components so that reused – Aravind Mar 10 '17 at 19:14
  • @Aravind that really depends if the method is simple or the method affects more than one type of class, if the method has everything related with the small group I thought inheritance was the appropriate way. Again, the question is about a better way to import extended services, not how to group code, becouse I would have to paste all my code so it could make sense for everyone. – ElBarto Mar 10 '17 at 19:24
  • @Aravind. anyway, thanks for your advice, I'll keep it in mind – ElBarto Mar 10 '17 at 19:26
  • its not about pasting the entire code or something. When you are extending the services, there exists a relationship between services which is not actual use of it. **Services are singleton** hope this answers your question – Aravind Mar 10 '17 at 19:31
  • @Aravind, I always take hard on remember that. Well, this is the case, each table is a Singleton, but 7-8 are exactly the same, 3 vary a little, 2 vary a lot (with diferent structures), all connect to Firebase and to a Http server depending the version of the app (it's not a single problem). Can I extend a Service from a simple Class? – ElBarto Mar 10 '17 at 19:37
  • you can extend interfaces to reuse the models. and write services separately and use various operators to collect them as required – Aravind Mar 10 '17 at 19:44
  • I think this is what you want http://stackoverflow.com/a/39653824/2564847 – Robin Dijkhof Mar 10 '17 at 20:19

0 Answers0