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

Example of using Shared Services. Prism

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

模块间通信的三种方式:

我想了解有关 Shared Services 通信的更多信息。我发现的是一篇关于 StockTrader application from Prism ToolKit.

的文章

是否有在 Prism 中使用 Shared Services 的更轻量级和更清晰的示例,其中可以看到使用 Shared Services[=28= 的模块之间的对话]? (可下载的代码将不胜感激)

GitHub 上的 Prism Library 存储库具有最新版本的 Stock Trader 示例应用程序,其中包括供您查看和下载的服务示例和源代码。

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

您的代码在哪些方面变得丑陋?如果您愿意,EventAggregator 是一项共享服务。

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

编辑:

共享程序集

public interface IMySharedService
{
    void AddData( object newData );
    object GetData();
    event System.Action<object> 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<IMySharedService,MySharedServiceImplementation>( new ContainerControlledLifetimeManager() );