Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7444997
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T11:42:49+00:00 2026-05-29T11:42:49+00:00

I’m getting familiar with RX and as my experimental project I’m trying to create

  • 0

I’m getting familiar with RX and as my experimental project I’m trying to create a simple command bus conceptually similar to this:

class Bus
{
    Subject<Command> commands;
    IObservable<Invocation> invocations;

    public Bus()
    {
        this.commands = new Subject<Command>();
        this.invocations = commands.Select(x => new Invocation { Command = x }).Publish();
    }

    public IObserver<Command> Commands
    {
        get { return this.commands; }
    }

    public IObservable<Invocation> Invocations
    {
        get { return this.invocations; }
    }
}

class Invocation
{
    public Command Command { get; set; }
    public bool Handled { get; set; }
}

The idea is that modules can install command handlers at startup using the Invocations property and can apply any filtering they wish to their subscription. Clients on the other hand can trigger command execution by calling Commands.OnNext(command).

However, I would like the Bus to provide a guarantee that each command submitted will be handled by exactly one handler. That is, OnNext processing should ideally terminate as soon as the first handler sets Invocation.Handled to true AND should throw an exception if, at the conclusion of OnNext(), Invocation.Handled is still false.

I played around creating my own ISubject, IObservable and IObserver implementations but this feels “dirty and cheap” 😉

I’m endeavouring to get my head around the compositional power that RX provides. In a compositional way, how can I provide the “exactly once” guarantee?

Thanks for any insights you can provide.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T11:42:51+00:00Added an answer on May 29, 2026 at 11:42 am

    You’ve generally got the right idea here, actually. You just need to do the actual dispatch. For this, SelectMany will help:

    class Bus
    {
        Subject<Command> commands;
        Subject<Invocation> invocations;
    
        // TODO: Instantiate me
        List<Func<Command, bool>> handlerList; 
    
        public Bus()
        {
            this.commands = new Subject<Command>();
            this.invocations = new Subject<Invocation>();
    
            commands.SelectMany(x => {
                // This FirstOrDefault() is just good ol' LINQ
                var passedHandler = 
                    handlerList.FirstOrDefault(handler => handler(x) == true);
    
                return passedHandler != null ?
                    Observable.Return(new Invocation() { Command = x, Handled = true}) :
                    Observable.Throw<Invocation>(new Exception("Unhandled!"));
            }).Multicast(invocations).Connect();
        }
    
        /* ... snip ... */
    }
    

    But, to be honest, this doesn’t really demonstrate the power of Rx, because it’s executing the handler list synchronously. Let’s make this more compelling by making this completely non-blocking.

    First, we’ll change our Func prototype to be Func<Command, IObservable<Invocation>>. This means, a method that takes a command and produces a Future Invocation result (a-la Task<T>). Then, we can get identical behavior yet have our handlers be async via this selector (coding via TextArea ahead):

    commands.SelectMany(x =>
        handlerList.ToObservable()
            .Select(h => Observable.Defer(() => h(x)))
            .Concat()
            .SkipWhile(x => x.Handled == false)
            .TakeLast(1))
        .Multicast(invocations).Connect();
    

    This is a pretty graduate-level use of Rx, but the idea is, for each Command, we’re going to initially create a Stream of handlers and run them in order (that’s what Defer + Concat does), until we find one whose Handled is true, then take the last one.

    The outer SelectMany selects a stream of Commands into a Stream of Future Results (i.e. the type is IO<IO<Invocation>> then flattens it, so it becomes a stream of results.

    No blocking ever, very concise, 100% testable, type-safe code that just expressed a pretty complicated idea that would’ve been really ugly to write imperatively. This is why Rx is cool.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.