1
\$\begingroup\$

I have a WPF project with Prism 6 and EF6. In my project I have a MainTabModule which has four ViewModels. One of these ViewModels is called ContactViewModel.

The MainTabModule will initialize and resolve my EF context and will also resolve my services. Finally I register my views to the regions in the Shell. This means that all four ViewModels are always active during the entire lifetime of the application (this is intended).

Now comes the challenging part. I've created a custom popup notification class that can pass in an entity. In that popup I can modify the entity and return it. Seems simple enough, however that entity may have foreign keys. Which means I need some way to search for that new foreign entity.

As far as I know I can't dependency inject my own services into the PopupViewModel. But I can use the EventAggregator. So I created two events.

public class ContactSearchEvent : PubSubEvent<string>
{
}

public class ContactCollectionEvent : PubSubEvent<List<Contact>>
{
}

The ContactViewModel (which is already active, see the first a second paragraph) will subscribe to the ContactSearchEvent in the constructor.

    public ContactSearchViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<ContactSearchEvent>().Subscribe(EventAggregatorSearch);
    }

Then PopupViewModel will subscribe to the ContactCollectionEvent in its constructor.

    public ContactSearchViewModel(IEventAggregator eventAggregator)
    {
        _eventAggregator = eventAggregator;
        _eventAggregator.GetEvent<ContactCollectionEvent>().Subscribe(SetCollection);
    }

The user can now type in the string payload from the Popup Window and search.

    private void Search()
    {
        _eventAggregator.GetEvent<ContactSearchEvent>().Publish(SearchPayload);
    }

The ContactSearchViewModel will now receive the payload then publish to ContactCollectionEvent. Normally it would use the service to search the datbase.

    private void EventAggregatorSearch(string payload)
    {
        _eventAggregator.GetEvent<ContactCollectionEvent>().Publish(new List<Contact>
        {
            new Contact
            {
                FirstName = "Test1"
            },
            new Contact
            {
                FirstName = "Test2"
            },
            new Contact
            {
                FirstName = "Test3"
            }
        });
    }

Back in the PopupViewModel we can now set the collection to the property.

    private void SetCollection(List<Contact> contacts)
    {
        SearchResults = contacts;
    }

This process works. However, I feel like there may be a better way in order to use my services to search the database from a Popup Window in Prism 6. Does anyone have any suggestions on how I can make it simpler or better?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.