Giter Site home page Giter Site logo

Sagas about node-cqrs-domain HOT 6 CLOSED

thenativeweb avatar thenativeweb commented on May 22, 2024
Sagas

from node-cqrs-domain.

Comments (6)

adrai avatar adrai commented on May 22, 2024

Hi @bkak!

Good input! At the moment I have not so much time to read your links completely, but I think this is really an open task... When I implemented the sagas in the cqrs-domain I decided to just implement a simple version first... and perhaps now it's time to implement a better version for sagas... ;-)

The momentary solution in cqrs-domain is to save some data in the saga and handle these compensation actions manually... (you can save some data by calling this.set()) like here: https://github.com/adrai/cqrs-sample/blob/master/domain/sagas/itemSaga.js

If you want and have more time you are free to propose something via pull request... :-) (I know the code is not really documented, but why not trying...?)

from node-cqrs-domain.

bkak avatar bkak commented on May 22, 2024

Ok gr8.

I shall give it a try. I am very new to Node, its just a month that I am playing with it.

With your help I should be able to create clemens saga handlers.

Shall buzz u once I get a prototype done.

Thanks a lot.

from node-cqrs-domain.

bkak avatar bkak commented on May 22, 2024

Hi,

The problem that I am facing in using this is I need Saga capability to issue commands to different AR of another BC in the same internal system, but Business Exception may arise in another BC. If there is any Exception I need to rollback/compensate the commands that were issued in this session.
Writing compensation for each and every command will be little difficult. In my domain if there is any Business Exception I can tell the user, that action has failed due to this "Business reason". User can make appropriate changes and issue it again.

Initially I thought to implement RoutingSlip that Clemens Vasters is talking about. Here writing compensation for every command will be tedious.
I feel this problem can be solved with Unit of work. If I encapsulate all the commands raised by the User or by the saga in the same session, and if all of them are successful then only on commit push the events to event store and publish all the events generated by them. This way no rollback/compensate will be required.

Can you think of any other solution to this problem?

Your feedback is highly valuable for me.

Thanks,
Bhoomi

from node-cqrs-domain.

adrai avatar adrai commented on May 22, 2024

Hi Bhoomi!

This smells a little like transactions, and this is not CQRS-like...
There is no session... and a saga can't react on commands, only on events.
If you want to react on commands use commandHandlers...
There you can do things like:

var commandHandlerBase = require('cqrs-domain').commandHandlerBase;

module.exports = commandHandlerBase.extend({

    commands: [
          'archive',
          'unarchive',
          'deleteArchive' 
    ],

    aggregate: 'archive',

    unarchive: function(id, cmd) {
        var self = this;

        this.loadAggregate(id, function(err, aggregate, stream) {

            var myObject = aggregate.get('myObject');

            if (myObject) {
                (new self.Command({
                    command: 'addMyObjectToOrganisation',
                    payload: {
                        id: aggregate.get('organisation'),
                        myObject: myObject
                    },
                    head: cmd.head
                })).emit(function(evt) {
                    if (evt.event !== 'commandRejected') {
                        self.finish(id, cmd);
                        (new self.Command({
                            id: cmd.id,
                            command: 'deleteArchive',
                            payload: cmd.payload,
                            head: cmd.head
                        })).emit();
                    } else {
                        self.finish(id, cmd, evt.payload.reason);
                    }
                });
            }

        });
    }
});

from node-cqrs-domain.

bkak avatar bkak commented on May 22, 2024

Oh no no I am not trying ask Saga to react to Commands.

Ok, my goal is to have Transactional Saga.

I shall give an example :

User places a Sales Order. CreateSalesOrder is the command to be fired on SalesOrder AR.
It raises an event SalesOrderCreated. This event is subscribed by Saga and it raises command PostAccountEffect on Accounting AR. Something goes wrong and Accounting AR raises Business Exception.
I want all the events to be rolled back instead of doing compensation.

Therefore I thought if I can encapsulate this in one UOW, and commit the UOW when all the commands are executed. On UOW Commit save all the events together in EventStore and then publish it. In case of any Business Exception do not commit the UOW but send Business Exception in response.

from node-cqrs-domain.

adrai avatar adrai commented on May 22, 2024

Ok, feel free to try it... At least you learned something... :-)

PS. events are published by the eventstore... (so they will be already committed)

from node-cqrs-domain.

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.