Reading this blog post that mentions that you can force your DI container to automatically subscribe to events if it implements IHandle<> . This is exactly what I'm trying to accomplish.
Here is what I still have.
container.Register(Component .For<MainWindowViewModel>() .ImplementedBy<MainWindowViewModel>() .LifeStyle.Transient .OnCreate((kernel, thisType) => kernel.Resolve<IEventAggregator>().Subscribe(thisType)));
As long as this code is successfully signed by MainWindowViewModel to receive published messages, the time comes to actually receive messages, nothing happens. If I manually sign all the work as expected.
Here is my MainWindowViewModel class.
public class MainWindowViewModel : IHandle<SomeMessage> { private readonly FooViewModel _fooViewModel; private readonly IEventAggregator _eventAggregator; public MainWindowViewModel(FooViewModel fooViewModel, IEventAggregator eventAggregator) { _fooViewModel = fooViewModel; _eventAggregator = eventAggregator;
How can I say that Windsor will automatically sign if any class inherits IHandle<> ?
In the end, I came to this custom object that subscribes.
public class EventAggregatorFacility : AbstractFacility { protected override void Init() { Kernel.DependencyResolving += Kernel_DependencyResolving; } private void Kernel_DependencyResolving(ComponentModel client, DependencyModel model, object dependency) { if(typeof(IHandle).IsAssignableFrom(client.Implementation)) { var aggregator = Kernel.Resolve<IEventAggregator>(); aggregator.Subscribe(client.Implementation); } } }
Looking at the EventAggregator class provided by Caliburn.Micro, I can see that the subscription was successful, however, if another class posts a message, the MainWindowViewModel class does not receive processing. Manual subscription still works, but I would like to automate this process. I have a feeling that he is not subscribing to the correct instance. I donβt know how to fix it.
I also tried using every other event opened by the Kernel property. Most of them cannot solve the IEventAggregator .
What am I missing?