Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

c# - Example of using Shared Services. Prism

I have 5 modules and I am using EventAggregator pattern to communicate between modules. And it seems to me that my code becomes ugly and it is bad design to use EventAggregator in my project.

There are three ways to communicate between modules:

  • Loosely coupled events
  • Shared services
  • Shared resources

I would like to know more about communication by Shared Services. What I've found is an article about StockTrader application from Prism ToolKit.

Is there some more lightweight and clearer example of using Shared Services in Prism where it is possible to see talking between modules using Shared Services? (downloadable code would be highly appreciated)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In which way is your code getting ugly? The EventAggregator is a shared service, if you like.

You put a service interface in a shared assembly, and then one module can, say, push data into the service while another module get's the data from the service.

Edit:

Shared assembly

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

First communicating module

// 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;
}

Second communicating module

// 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...
    }
}

Some other module's initialization

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...