本文介绍了两个服务如何以双向方式相互通信?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一方面是通过事件,另一方面是通过调用方法.我试图在我的应用程序中实现聚合模式.

In one way by events, in other by calling methods. I trying to realize aggregate pattern in my app.

我有 AuthService,在这里我处理身份验证结果并发出事件.

I have AuthService, here i handle auth results and emit the event.

if (auth) { this.eAuth.emit(true) } else { this.eAuth.emit(false) }

我可以在 AuthComponent 中订阅

I can subscribe in AuthComponent

_authService.eAuth.subscribe( (isAuth) => this.handleAuthResult(isAuth) )

它完美无缺.但是 AggregateService 也需要知道这一点,并将这些信息广播给 UserService、LoadDataService 等.

And it works perfect. But AggregateService also need to know about this and broadcast this information to UserService, LoadDataService and so on.

怎么做?

更新:我的 AggregateService 没有组件,我已经将 AuthService 注入其中.

upd: my AggregateService doesn't have component and I already inject AuthService into it.

推荐答案

如果 ServiceA 被注入到 ServiceB 中,ServiceB 可以调用 ServiceA 上的方法(因此 ServiceB → ServiceA 通信)并且它可以 subscribe() 到 ServiceA 可能暴露的任何 Obervable(因此 ServiceA → 到 ServiceB 通信).

If ServiceA is injected into ServiceB, ServiceB can call methods on ServiceA (hence ServiceB → ServiceA communication) and it can subscribe() to any Obervable that ServiceA might expose (hence ServiceA → to ServiceB communication).

缺少的是 ServiceA 直接调用 ServiceB 方法的能力.通常不建议这样做,因为它会造成服务之间的耦合.ServiceA 应该使用 next() 在 ServiceB 可以subscribe() 的 Observable 上发出事件,然后 ServiceB 可以调用适当的方法.

What's missing is ServiceA's ability to directly call methods on ServiceB. This is often not recommended, as it creates coupling between the services. ServiceA should emit events using next() on the Observable that ServiceB can subscribe() to, then ServiceB can call the appropriate method(s) on itself.

然而,如果你真的需要它来工作,这里有一种方法:让 ServiceB 在 ServiceA 上调用某种 registerService(this) 方法.参数的类型应该是接口而不是具体类型,以限制耦合.然后 ServiceA 将拥有对 ServiceB 的引用,并且可以调用其上的方法.

However, if you really need this to work, here is one way to do it: have ServiceB call some kind of registerService(this) method on ServiceA. The type of the argument should be an interface rather than a concrete type, to limit coupling. Then ServiceA will have a reference to ServiceB and it can call methods on it.

interface SomeInterface {
  public methodOne();
  public methodTwo();
}
import {SomeInterface} from './some-interface';
export class ServiceA {
    registerService(someService:SomeInterface) {
       someService.methodOne(this);
       // you'll probably want to store someService in this object
    }
}

ServiceB 应该实现那个接口——即实现ServiceA可以调用的方法集.

ServiceB should implement that interface -- i.e., implement the set of methods that ServiceA can call.

import {SomeInterface} from './some-interface';
export class ServiceB implements SomeInterface {
    constructor(private _serviceA: ServiceA) {
       _serviceA.registerService(this);
    }
    methodOne(who) {
       console.log('hello from ServiceB.methodOne(), called by', who);
    }
    methodTwo() { ... }
}

这篇关于两个服务如何以双向方式相互通信?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-24 11:29