Ste*_*pUp 3 c# wpf prism sharedservices
我有 5 个模块,我使用EventAggregator模式在模块之间进行通信。在我看来,我的代码变得丑陋,并且在我的项目中使用EventAggregator是糟糕的设计。
模块之间的通信方式有以下三种:
我想了解有关共享服务通信的更多信息。我发现了一篇关于Prism ToolKit 中的 StockTrader 应用程序的文章。
是否有一些在 Prism 中使用共享服务的更轻量级和更清晰的示例,可以使用共享服务在模块之间进行通信?(可下载代码将不胜感激)
你的代码在哪些方面变得丑陋?如果您愿意的话,这EventAggregator是一项共享服务。
您将一个服务接口放入共享程序集中,然后一个模块可以将数据推送到服务中,而另一个模块可以从服务中获取数据。
编辑:
共享装配
public interface IMySharedService
{
void AddData( object newData );
object GetData();
event System.Action<object> DataArrived;
}
Run Code Online (Sandbox Code Playgroud)
第一通信模块
// 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;
}
Run Code Online (Sandbox Code Playgroud)
第二通信模块
// 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...
}
}
Run Code Online (Sandbox Code Playgroud)
其他一些模块的初始化
// this provides the instance of the shared service that will be injected in SomeClass and SomeOtherClass
_unityContainer.RegisterType<IMySharedService,MySharedServiceImplementation>( new ContainerControlledLifetimeManager() );
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4080 次 |
| 最近记录: |