thenativeweb / node-cqrs-domain

Node-cqrs-domain is a node.js module based on nodeEventStore that. It can be very useful as domain component if you work with (d)ddd, cqrs, eventdenormalizer, host, etc.
http://cqrs.js.org/pages/domain.html
MIT License
269 stars 57 forks source link

How to check existence of another aggregate id in preconditions ? #31

Closed ludydoo closed 9 years ago

ludydoo commented 9 years ago

Hi,

Your framework looks nice. I tried it a little and it seems quite what I need. Though, how is it possible to achieve this : Given the aggregates :

Person which has an array of names the names are a hash of (typeId : number, value : string) Like

person  = {
    id : 1,
    names : [
        {typeId : 1, value : 'John Cage Inc.'}
        {typeId : 2, value : 'John'}
        {typeId : 3, value : 'Cage'}
    ]
}

PersonNameType which is a simple string like Firstname or Lastname or PostalName

like :

postalNameType = {
    id : 1,
    value : 'Postal Name' 
}

I defined a command and event for Person "enterNewName"/"enteredNewName"

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

module.exports = domain.defineEvent({

    name: 'enteredNewName', 

    payload: 'payload' // if not defined it will pass the whole event...

}, function (data, aggregate) {

    aggregate.attributes.names.push(data);

});

and I want to add a precondition "mustBeExistingNameType"

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

module.exports = domain.definePreCondition({

    description: 'Must specify an existing name type id',

}, function (command, agg, callback) {

    // Check that the given command.payload.typeId exists 
    // as an Id of the PersonNameType aggregate
});

How is it possible to verify that the given typeId exists without having to write a custom command handler? Otherwise, could you indicate how to write this custom command handler?

Thanks

adrai commented 9 years ago

Every pre-condition and business rule has only what's defined in the aggregate. Please do not think in data! Perhaps you need to enlarge your aggregate...

ludydoo commented 9 years ago

Okay I see. I'm used to RDBMS so... I see "by table". Though, how would you see that kind of aggregate? Let's say you're enabling your client to store information about 1000's of people. Each of them has multiple contact information (first name, last name, phone, email, ...), but you have to be sure that each has a first name and last name set.

Also, the client could define custom contact information numbers that he wants to define for each of his registered person. Let's say the client wants to be able to add "Postal Name" as a contact information type, and mark it as required for all new clients?

And also, each person may or may not have multiple mandates associated. Each of those mandates have activities did on them, of a certain type (which the client defines for his business)... You get the idea? Maybe RDBMS is more tailored for this kind of job. What do you think ?

I understand the concept of aggregate as an "independent logical unit", but what happens when you have to normalize the data, and moreover, when the client wants to be able to normalize its data for his business?

adrai commented 9 years ago

All this could fit with cqrs... but I think you don't want to design the intentions of the system, but only manage your data. So a better fit is to use a CRM/ERP.