Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Pub / Sub system for view models communication #28

Open
LeftTwixWand opened this issue Oct 27, 2022 · 0 comments
Open

Integrate Pub / Sub system for view models communication #28

LeftTwixWand opened this issue Oct 27, 2022 · 0 comments
Assignees
Labels
enhancement New feature or request feature request Means some additional functionality is asked layer/application Means some changes in the Application layer layer/infrastructure Means some changes in the Infrastructure layer

Comments

@LeftTwixWand
Copy link
Owner

CommunityToolkit.Mvvm.Messaging provides IMessenger interface, that allows us to set up Pub / Sub system for flexible ViewModels communication.

How it can be useful?
We have a ProductsView with ProductsViewModel, which has a list of products.
And a ProductView with ProductViewModel, where we can see some detail information, modify it, delete current product or to add a new one.

After we added, edited or deleted a product - we can just send a message, what we've modified.
And the main ProductsViewModel will handle it and will update its collection.
So, we won't need to load all the products from the database after every modification - we just will need to update im-memoty Observable collection.

// Define a message
public sealed class ProductDeletedMessage : ValueChangedMessage<int>
{
    public ProductDeletedMessage(int productId) : base(productId)
    {
    }
}
// Send a message
public sealed partial class ProductViewModel : ObservableObject
{
    [RelayCommand]
    private void DeleteItem(int productId)
    {
        WeakReferenceMessenger.Default.Send(new ProductDeletedMessage(productId));
    }
}
// Handle a message
public sealed partial class ProductsViewModel : ObservableRecipient, IRecipient<ProductDeletedMessage>
{
    public ProductsViewModel()
    {
        // Activate message listener
        IsActive = true;
    }

    public ObservableCollection<ProductModel> Products { get; } = new();

    public void Receive(ProductDeletedMessagemessage)
    {
        var productToDelete = Products.First(prodcut => prodcut.Id == message.Value);
        Products.Remove(productToDelete);
    }
}
@LeftTwixWand LeftTwixWand added enhancement New feature or request layer/application Means some changes in the Application layer layer/infrastructure Means some changes in the Infrastructure layer feature request Means some additional functionality is asked labels Oct 27, 2022
@LeftTwixWand LeftTwixWand self-assigned this Oct 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request feature request Means some additional functionality is asked layer/application Means some changes in the Application layer layer/infrastructure Means some changes in the Infrastructure layer
Projects
None yet
Development

No branches or pull requests

1 participant