Comments (8)
This is now possible as of v5.0.0
from easy.messagehub.
I think a non singleton implementation is critical in many scenarios including unit testing (tests running in parallel), background job processing, and most architectures that support any kind of "scoped" dependency trees. And I think all you'd have to do is make the constructor public? :)
And of course, agree with codeConcussion - great library otherwise!
from easy.messagehub.
The singleton instance is by design.
You should be able to achieve what you want by defining a base class UserMessage
or IUserMessage
from which you inherit other messages. Then those concerned with user scoped messages can publish/subscribe using the base class and receive all user related messages.
from easy.messagehub.
I have not heard from you so I am going to close this for now. Let me know how you get on.
from easy.messagehub.
Not the original author, but I just ran into this need as well. Maybe you'll have a suggestion.
We're using Hangfire in our application to handle background processing on the server. It executes tasks concurrently on a configurable amount of threads. We'd like to implement pub/sub in this scenario using Hangfire and Easy.MessageHub.
To do that, a task gets queued in Hangfire that contains the message that needs published. When it executes, it looks at the message, instantiates all the interested subscriber classes (who subscribe in their constructor), then publishes the message. I'm running into a problem if multiple messages of the same type come in at the same time. Subscribers from other Hangfire threads are receiving messages they "shouldn't".
I think I could easily solve the issue if I could register an IMessageHub as Scoped instead of as a Singleton. Any thought or suggestions?
And on another note, this is a great little Event Aggregator library. So minimal and clean, I love it!
from easy.messagehub.
Can you not define your scoping logic as part of the event you are publishing? In other words, at the time of publishing and setting up the subscribers, define your scoping logic (however you see fit) and make it part of the message. The first thing your subscribers do would be checking that logic as part of the event they are receiving and ignore them if out of their scope.
Once again the design decision to make this library a singleton was a conscious one and I have no plans to change that.
from easy.messagehub.
Thanks for the response. Yes, that's one way I could solve it. It just seems like a code smell for all messages and all subscribers to have to deal with the publisher's scope. Ideally, a subscriber should just handle a message it receives and not have to worry about where it came from. And to a lesser extent, a message should contain message-related data and not have to concern itself with who is sending it.
From my viewpoint, I don't see how letting consumers of the library decide how many MessageHubs to instantiate compromises the design of the library. True, most users would probably use the Singleton instance, but I don't see how allowing other use cases is a bad thing. No offense intended, just my opinion.
Once again, thanks for this great library!
from easy.messagehub.
@srowan do you have an example of how you used this in a Blazor Server app? Do you need to handle disposing to prevent memory leaks?
from easy.messagehub.
Related Issues (20)
- Registering IMessageHub with MessageHub HOT 1
- Subscribing a runtime type HOT 5
- Subscriber is receiving 3 messages when publisher is only sending 1 HOT 7
- Publish & Subscribe from different Project? HOT 2
- Sometimes Unsubscribe method hangs HOT 6
- How to setup one Server with Many cleints between ASP Core web and .net Console service HOT 1
- System.ObjectDisposedException: ThreadLocal object has been deleted. HOT 1
- NuGet package?
- Async handlers HOT 2
- Get MessageHub to play nicely with Autofac in a web application HOT 1
- Conditional subscription HOT 6
- Unflexible detection of the given type HOT 4
- Support for async actions HOT 2
- Add an async publisher HOT 4
- Available for dotnet core? HOT 2
- Add async version HOT 6
- MessageHub is holding objects in Memory HOT 18
- UnSubscribe method causes ArgumentNullException HOT 1
- Publish and subscribe from different threads HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from easy.messagehub.