Coder Social home page Coder Social logo

Non-singleton instances about easy.messagehub HOT 8 CLOSED

nimaara avatar nimaara commented on July 23, 2024 1
Non-singleton instances

from easy.messagehub.

Comments (8)

NimaAra avatar NimaAra commented on July 23, 2024 4

This is now possible as of v5.0.0

from easy.messagehub.

srowan avatar srowan commented on July 23, 2024 1

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.

NimaAra avatar NimaAra commented on July 23, 2024

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.

NimaAra avatar NimaAra commented on July 23, 2024

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.

codeConcussion avatar codeConcussion commented on July 23, 2024

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.

NimaAra avatar NimaAra commented on July 23, 2024

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.

codeConcussion avatar codeConcussion commented on July 23, 2024

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.

ryanbuening avatar ryanbuening commented on July 23, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.