3

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)

StepUp
  • 36,391
  • 15
  • 88
  • 148

2 Answers2

5

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() );
Haukinger
  • 10,420
  • 2
  • 15
  • 28
  • could you please show some examples of code how to implement shared services? – StepUp Jan 12 '16 at 21:01
  • Thanks! It is interesting. However, how can be `Second communicating module` notified when something interesting happened in `First communicating module`? – StepUp Jan 14 '16 at 08:24
  • You have multiple options - regular event, registering a callback, whatever you prefer... I added an event to the example, because with a `RegisterOnNewDataCallback` it looked too much like the `EventAggregator`... – Haukinger Jan 14 '16 at 17:51
  • is it normal that an event is not fired when data is added `_sharedService.AddData( item );`? I cannot figure out how second module can be notified. – StepUp Jan 15 '16 at 08:05
  • `DataArrived` is not fired in my Second Communicating Module. please, help me). – StepUp Jan 15 '16 at 16:37
  • The implementation of `IMySharedService` has to do this, probably in `AddData`. – Haukinger Jan 15 '16 at 17:46
  • please be very kind to see my problem. I've uploaded my code to OneDrive. I've spent the eve and a day to figure out about how to solve it. I've made a test project. please be very kind to see my problem. I've uploaded my code to OneDrive. https://onedrive.live.com/redir?resid=D6BDF30773C16E01!2053&authkey=!ANIbeCmwSvQRvf4&ithint=file%2crar – StepUp Jan 15 '16 at 17:55
  • I see one problem in `ViewModelControlB` - that one has its own `UnityContainer`, so it doesn't share anything with any other module. I'd try to get the service as dependency injected directly... I'll update the example. – Haukinger Jan 16 '16 at 16:17
  • Have a look at the unity book (https://www.microsoft.com/en-us/download/details.aspx?id=39944) if you're unsure how to use the unity container. – Haukinger Jan 16 '16 at 16:26
1

The Prism Library repo on GitHub has an up to date version of the Stock Trader sample application, which includes service examples and source code for you to look at, and download.

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

R. Richards
  • 24,603
  • 10
  • 64
  • 64
  • thanks for reply. But like I said in my question I would like to see more clearer example where just **SharedServices** and Prism modules – StepUp Jan 12 '16 at 12:32
  • Perhaps I need to know your definition of Shared Services. The example shows how to make a class available to the entire application via the IOC container (MEF in this case) as a singleton instance. As long as the modules can resolve the container entry, then they can share the service. Am I missing something? – R. Richards Jan 12 '16 at 13:05
  • I just would like to create Shared Services, not to edit. And I would like to see simple example of communication between modules by **Shared Services** – StepUp Jan 12 '16 at 14:08