{"id":13466653,"url":"https://github.com/mikoskinen/Blazor.EventAggregator","last_synced_at":"2025-03-26T00:31:17.181Z","repository":{"id":34077866,"uuid":"169103867","full_name":"mikoskinen/Blazor.EventAggregator","owner":"mikoskinen","description":"Lightweight Event Aggregator for Blazor (Razor Components). ","archived":false,"fork":false,"pushed_at":"2021-10-01T10:47:23.000Z","size":339,"stargazers_count":121,"open_issues_count":6,"forks_count":23,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-03-11T16:16:16.015Z","etag":null,"topics":["blazor","razor-components"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mikoskinen.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-02-04T15:54:56.000Z","updated_at":"2025-03-07T06:25:02.000Z","dependencies_parsed_at":"2022-08-08T00:00:42.496Z","dependency_job_id":null,"html_url":"https://github.com/mikoskinen/Blazor.EventAggregator","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikoskinen%2FBlazor.EventAggregator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikoskinen%2FBlazor.EventAggregator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikoskinen%2FBlazor.EventAggregator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mikoskinen%2FBlazor.EventAggregator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mikoskinen","download_url":"https://codeload.github.com/mikoskinen/Blazor.EventAggregator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245566095,"owners_count":20636390,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor","razor-components"],"created_at":"2024-07-31T15:00:47.760Z","updated_at":"2025-03-26T00:31:16.858Z","avatar_url":"https://github.com/mikoskinen.png","language":"C#","funding_links":[],"categories":["Libraries \u0026 Extensions"],"sub_categories":["Tools \u0026 Utilities"],"readme":"# Blazor.EventAggregator\r\n\r\nBlazor.EventAggregator is a lightweight Event Aggregator for Blazor. \r\n\r\n* 3/2020: Updated to work with .NET Core v3.1.0\r\n\r\nEvent aggregator is used for indirect component to component communication. In event aggregator pattern you have message/event publishers and subscribers. In the case of Blazor, component can publish its events and other component(s) can react to those events. \r\n\r\n[![NuGet](https://img.shields.io/nuget/v/EventAggregator.Blazor.svg)](https://www.nuget.org/packages/EventAggregator.Blazor/)\r\n\r\n### Note:\r\n\r\nBlazor.EventAggregator is completely based on the work done in Caliburn.Micro. The source code was copied from it and then altered to work with Blazor.\r\n\r\nAlso note that the library has only been tested with the server-side version of Blazor. There's no known issues with the WebAssembly-version of Blazor.\r\n\r\n## Getting Started\r\n\r\nFirst register EventAggregator in app’s ConfigureServices:\r\n```\r\npublic void ConfigureServices(IServiceCollection services)\r\n{\r\n    services.AddEventAggregator();\r\n}\r\n```\r\n\r\nThe rest depends on if you’re using components with code-behinds (inheritance) or without inheritance. The following guidance is for code-behind scenarios.\r\n\r\n### Creating the publisher\r\n\r\nTo create an event publishing component, first inject IEventAggregator:\r\n\r\n```\r\n[Inject]\r\nprivate IEventAggregator _eventAggregator { get; set; }\r\n```\r\n\t\t\r\nThen publish the message when something interesting happens:\r\n\r\n```\r\nawait _eventAggregator.PublishAsync(new CounterIncreasedMessage());\r\n```\r\n\r\nHere’s a full example of a publisher:\r\n```\r\npublic class CounterComponent : ComponentBase\r\n{\r\n    [Inject]\r\n    private IEventAggregator _eventAggregator { get; set; }\r\n\r\n    public int currentCount = 0;\r\n\r\n    public async Task IncrementCountAsync()\r\n    {\r\n        currentCount++;\r\n        await _eventAggregator.PublishAsync(new CounterIncreasedMessage());\r\n    }\r\n}\r\n\r\npublic class CounterIncreasedMessage\r\n{\r\n}\r\n```\r\n\t\r\n### Creating the subscriber\r\n\r\nTo create an event subscriber, also start by injecting IEventAggregator:\r\n\r\n```\r\n[Inject]\r\nprivate IEventAggregator _eventAggregator { get; set; }\r\n```\r\nThen make sure to add and implement the IHandle\u003cTMessageType\u003e interface for all the event’s your component is interested in:\r\n\r\n```\r\npublic class CounterListenerComponent : ComponentBase, IHandle\u003cCounterIncreasedMessage\u003e\r\n...\r\npublic Task HandleAsync(CounterIncreasedMessage message)\r\n{\r\n    currentCount += 1;\r\n    return Task.CompletedTask;\r\n}\r\n```\r\n\r\nHere’s full example of a subscriber:\r\n\r\n```\r\npublic class CounterListenerComponent : ComponentBase, IHandle\u003cCounterIncreasedMessage\u003e\r\n{\r\n    [Inject]\r\n    private IEventAggregator _eventAggregator { get; set; }\r\n\r\n    public int currentCount = 0;\r\n\r\n    protected override void OnInit()\r\n    {\r\n        _eventAggregator.Subscribe(this);\r\n    }\r\n\r\n    public Task HandleAsync(CounterIncreasedMessage message)\r\n    {\r\n        currentCount += 1;\r\n        return Task.CompletedTask;\r\n    }\r\n}\r\n```\r\n\r\n## Note about auto refresh\r\n\r\nThe library can try to automatically call subscriber component's StateHasChanged after it has handled the event. By default this functionality is disabled. You can enable it through options:\r\n\r\n```\r\nservices.AddEventAggregator(options =\u003e options.AutoRefresh = true);\r\n```\r\n\r\nAuto refresh is based on reflection and it assumes that the subscriber inherits from ComponentBase.\r\n\t\r\n## Samples\r\n\r\nThe project site contains a full working sample of the code-behind model in the samples-folder.\r\n\r\n## Requirements\r\nThe library has been developed and tested using the following tools:\r\n\r\n* .NET Core 3.1\r\n* Visual Studio 2019\r\n\r\n## Acknowledgements\r\nWork is based on the code available in Caliburn.Micro.\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikoskinen%2FBlazor.EventAggregator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmikoskinen%2FBlazor.EventAggregator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmikoskinen%2FBlazor.EventAggregator/lists"}