Reputation: 3318
What is the purpose and usings of shared services in prism ?
What things can make me think that I have to use shared services instead of EventAggegator
?
Upvotes: 3
Views: 1203
Reputation: 16018
We have used both, but generally use shared services when the functionality is more than just a simple notification - we also use EventAgregator
from within our services in some cases.
For example we have a service for scanning in documents :
public interface IDocumentScannerService
{
}
public class DocumentScannerService : IDocumentScannerService
{
}
This would be a pretty bad design to try and implement this with EventAggregator
.
Upvotes: 1
Reputation: 3539
I don't know how other using it but I used it a lot for modularity of my app. For example in Silverlight application for security reasons only OpenFileDialog
could return Stream
to file. So I just build an IOpenFileService
service and plugged it in constructor to any ViewModel that need to open a stream to file. It's also suitable for various Loggers and even Database layer.
Another useful part of services is that they could be build and tested independently from other modules. MEF/Unity will provide all the glue to insert ready objects inside constructor or properties of other objects.
And don't forget that the service class itself could use MEF/Unity magic and insert other services to itself.
And for EventAgregator
: You code could became overloaded with various Event
definitions very quickly. For example Resize
event. On Silverlight app initialization the PRISM Region
controls is slow process so the Regions
was attached to VisualTree
very late and somehow they missing the initial Resize
event . I provided the internal Resize
event for Region
(via EventAgregator
) and then another Resize
event that each Region
control will send to it's children to resize themselves to Region
control boundaries. It's 2 Event
classes just for Resize...
Upvotes: 1
Reputation: 25201
Looking at EventAggregator
from an event subscriber point of view, it's good for getting notified about certain events that occur in the application. This way you're performing an operation passively, i.e. whenever someone else publishes an event. You'd like to use EventAggregator
when you need to react to something happening in your application.
On the other hand, with shared services you can actively perform operations and request data. For example, you could have a shared service that exposes a GetData()
method, and you could resolve this service and ask for the data actively, any time you need it.
Upvotes: 4