使用共享服务的示例。 棱镜

我有5个模块,我使用EventAggregator模式在模块之间进行通信。 在我看来,我的代码变得丑陋,在我的项目中使用EventAggregator是不好的设计。

模块之间有三种通信方式:

  • 松散耦合的事件
  • 共享服务
  • 共享资源

我想更多地了解共享服务的通信。 我发现的是一篇关于Prism ToolKit的StockTrader应用程序的文章。

是否有一些更轻量级和更清晰的使用Prism中的共享服务的例子,在这里可以看到使用共享服务在模块之间进行交谈? (可下载的代码将受到高度赞赏)

您的代码以哪种方式变得丑陋? 如果您愿意, EventAggregator是一个共享服务。

您将服务接口放在共享程序集中,然后一个模块可以将数据推入服务,而另一个模块从服务获取数据。

编辑:

共享程序集

 public interface IMySharedService { void AddData( object newData ); object GetData(); event System.Action DataArrived; } 

第一个沟通模块

 // this class has to be resolved from the unity container, perhaps via AutoWireViewModel internal class SomeClass { public SomeClass( IMySharedService sharedService ) { _sharedService = sharedService; } public void PerformImport( IEnumerable data ) { foreach (var item in data) _sharedService.AddData( item ); } private readonly IMySharedService _sharedService; } 

第二通讯模块

 // this class has to be resolved from the same unity container as SomeClass (see above) internal class SomeOtherClass { public SomeOtherClass( IMySharedService sharedService ) { _sharedService = sharedService; _sharedService.DataArrived += OnNewData; } public void ProcessData() { var item = _sharedService.GetData(); if (item == null) return; // Do something with the item... } private readonly IMySharedService _sharedService; private void OnNewData( object item ) { // Do something with the item... } } 

一些其他模块的初始化

 // this provides the instance of the shared service that will be injected in SomeClass and SomeOtherClass _unityContainer.RegisterType( new ContainerControlledLifetimeManager() ); 

GitHub上的Prism Library回购包含Stock Trader示例应用程序的最新版本,其中包含服务示例和源代码供您查看和下载。

https://github.com/PrismLibrary/Prism-Samples-Wpf/tree/master/StockTraderRI