DDD model of a purchase in NodeJS











up vote
1
down vote

favorite












I already have some experience with Java and I'm used to modeling with a DDD approach. Therefore, now I'm beginning with JS and NodeJS and I'd like to know which is the best practice to design with a similar approach in Javascript (without ES6).



Here I have an example of what I'm trying to do, but I don't know if these is the better way. I appreciate some opinions.



function Purchase(pCustomer, pOrders, pPayment, pTotal) {
let idPurchase = PurchaseId.purchaseId();
let customer = pCustomer;
let payment = pPayment;
let orders = pOrders;
let deliveryAddress;
let billingAddress;
let total = pTotal;

// Return an object exposed to the public
return {
setOrders: function (o) {
orders = o
},
setDeliveryAddress: function (address1, address2, zipCode, city) {
deliveryAddress = new Address(address1, address2, zipCode, city);
},
setBillingAddress: function (address1, address2, zipCode, city) {
billingAddress = new Address(address1, address2, zipCode, city);
},
getId: function () {
return idPurchase;
},
getOrders: function () {
return orders;
},
getCustomer: function () {
return customer;
},
toJSON: function () {
return {
idPurchase: idPurchase,
customerOnPurchase: customer.toJSON(),
deliveryAddress: deliveryAddress.toJSON(),
billingAddress: billingAddress.toJSON(),
total: total,
}
}
}
}

module.exports = Purchase;









share|improve this question
















bumped to the homepage by Community 2 days ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.



















    up vote
    1
    down vote

    favorite












    I already have some experience with Java and I'm used to modeling with a DDD approach. Therefore, now I'm beginning with JS and NodeJS and I'd like to know which is the best practice to design with a similar approach in Javascript (without ES6).



    Here I have an example of what I'm trying to do, but I don't know if these is the better way. I appreciate some opinions.



    function Purchase(pCustomer, pOrders, pPayment, pTotal) {
    let idPurchase = PurchaseId.purchaseId();
    let customer = pCustomer;
    let payment = pPayment;
    let orders = pOrders;
    let deliveryAddress;
    let billingAddress;
    let total = pTotal;

    // Return an object exposed to the public
    return {
    setOrders: function (o) {
    orders = o
    },
    setDeliveryAddress: function (address1, address2, zipCode, city) {
    deliveryAddress = new Address(address1, address2, zipCode, city);
    },
    setBillingAddress: function (address1, address2, zipCode, city) {
    billingAddress = new Address(address1, address2, zipCode, city);
    },
    getId: function () {
    return idPurchase;
    },
    getOrders: function () {
    return orders;
    },
    getCustomer: function () {
    return customer;
    },
    toJSON: function () {
    return {
    idPurchase: idPurchase,
    customerOnPurchase: customer.toJSON(),
    deliveryAddress: deliveryAddress.toJSON(),
    billingAddress: billingAddress.toJSON(),
    total: total,
    }
    }
    }
    }

    module.exports = Purchase;









    share|improve this question
















    bumped to the homepage by Community 2 days ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I already have some experience with Java and I'm used to modeling with a DDD approach. Therefore, now I'm beginning with JS and NodeJS and I'd like to know which is the best practice to design with a similar approach in Javascript (without ES6).



      Here I have an example of what I'm trying to do, but I don't know if these is the better way. I appreciate some opinions.



      function Purchase(pCustomer, pOrders, pPayment, pTotal) {
      let idPurchase = PurchaseId.purchaseId();
      let customer = pCustomer;
      let payment = pPayment;
      let orders = pOrders;
      let deliveryAddress;
      let billingAddress;
      let total = pTotal;

      // Return an object exposed to the public
      return {
      setOrders: function (o) {
      orders = o
      },
      setDeliveryAddress: function (address1, address2, zipCode, city) {
      deliveryAddress = new Address(address1, address2, zipCode, city);
      },
      setBillingAddress: function (address1, address2, zipCode, city) {
      billingAddress = new Address(address1, address2, zipCode, city);
      },
      getId: function () {
      return idPurchase;
      },
      getOrders: function () {
      return orders;
      },
      getCustomer: function () {
      return customer;
      },
      toJSON: function () {
      return {
      idPurchase: idPurchase,
      customerOnPurchase: customer.toJSON(),
      deliveryAddress: deliveryAddress.toJSON(),
      billingAddress: billingAddress.toJSON(),
      total: total,
      }
      }
      }
      }

      module.exports = Purchase;









      share|improve this question















      I already have some experience with Java and I'm used to modeling with a DDD approach. Therefore, now I'm beginning with JS and NodeJS and I'd like to know which is the best practice to design with a similar approach in Javascript (without ES6).



      Here I have an example of what I'm trying to do, but I don't know if these is the better way. I appreciate some opinions.



      function Purchase(pCustomer, pOrders, pPayment, pTotal) {
      let idPurchase = PurchaseId.purchaseId();
      let customer = pCustomer;
      let payment = pPayment;
      let orders = pOrders;
      let deliveryAddress;
      let billingAddress;
      let total = pTotal;

      // Return an object exposed to the public
      return {
      setOrders: function (o) {
      orders = o
      },
      setDeliveryAddress: function (address1, address2, zipCode, city) {
      deliveryAddress = new Address(address1, address2, zipCode, city);
      },
      setBillingAddress: function (address1, address2, zipCode, city) {
      billingAddress = new Address(address1, address2, zipCode, city);
      },
      getId: function () {
      return idPurchase;
      },
      getOrders: function () {
      return orders;
      },
      getCustomer: function () {
      return customer;
      },
      toJSON: function () {
      return {
      idPurchase: idPurchase,
      customerOnPurchase: customer.toJSON(),
      deliveryAddress: deliveryAddress.toJSON(),
      billingAddress: billingAddress.toJSON(),
      total: total,
      }
      }
      }
      }

      module.exports = Purchase;






      javascript node.js ddd






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 16 at 17:14









      200_success

      127k15148411




      127k15148411










      asked Jul 16 at 17:05









      Bruno Alves

      62




      62





      bumped to the homepage by Community 2 days ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 2 days ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote













          Based on functions like getId and toJSON, it seems like you would naturally want to have a base class for your models. As such, it would make sense to use write your functions in a way that support prototypical inheritance e.g.



          Base Model



          function Entity(id) {
          this._id = id;
          }

          Entity.prototype = {
          function getId() {
          return this._id;
          }
          ...
          }

          module.exports = Entity;


          Derived Model



          var Entity = require('./entity');
          var util = require('util');

          function Purchase(customer, orders, ...) {
          Entity.call(this, PurchaseId.purchaseId());
          this._customer = customer;
          this._orders = orders;
          ...
          }

          util.inherits(Purchase, Entity);

          Purchase.prototype.getCustomer = function() {
          return this._customer;
          }
          Purchase.prototype.getOrders = function() {
          return this._orders;
          }
          ...
          module.exports = Purchase;


          Usage



          var purchase = new Purchase(...);
          console.log(purchase.getId()); // Purchase will inherit everything from Entity
          console.log(purchase.getCustomer()); // and have it's own functions too


          Note - notice in base we set the prototype directly but in the derived we modify it? This is deliberate because in the base class the prototype is currently empty so it's fine to completely overwrite. If we did the same thing on the derived class then we'd lose the base prototype therefore we simply append to the existing prototype.






          share|improve this answer





















            Your Answer





            StackExchange.ifUsing("editor", function () {
            return StackExchange.using("mathjaxEditing", function () {
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            });
            });
            }, "mathjax-editing");

            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "196"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199606%2fddd-model-of-a-purchase-in-nodejs%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            Based on functions like getId and toJSON, it seems like you would naturally want to have a base class for your models. As such, it would make sense to use write your functions in a way that support prototypical inheritance e.g.



            Base Model



            function Entity(id) {
            this._id = id;
            }

            Entity.prototype = {
            function getId() {
            return this._id;
            }
            ...
            }

            module.exports = Entity;


            Derived Model



            var Entity = require('./entity');
            var util = require('util');

            function Purchase(customer, orders, ...) {
            Entity.call(this, PurchaseId.purchaseId());
            this._customer = customer;
            this._orders = orders;
            ...
            }

            util.inherits(Purchase, Entity);

            Purchase.prototype.getCustomer = function() {
            return this._customer;
            }
            Purchase.prototype.getOrders = function() {
            return this._orders;
            }
            ...
            module.exports = Purchase;


            Usage



            var purchase = new Purchase(...);
            console.log(purchase.getId()); // Purchase will inherit everything from Entity
            console.log(purchase.getCustomer()); // and have it's own functions too


            Note - notice in base we set the prototype directly but in the derived we modify it? This is deliberate because in the base class the prototype is currently empty so it's fine to completely overwrite. If we did the same thing on the derived class then we'd lose the base prototype therefore we simply append to the existing prototype.






            share|improve this answer

























              up vote
              0
              down vote













              Based on functions like getId and toJSON, it seems like you would naturally want to have a base class for your models. As such, it would make sense to use write your functions in a way that support prototypical inheritance e.g.



              Base Model



              function Entity(id) {
              this._id = id;
              }

              Entity.prototype = {
              function getId() {
              return this._id;
              }
              ...
              }

              module.exports = Entity;


              Derived Model



              var Entity = require('./entity');
              var util = require('util');

              function Purchase(customer, orders, ...) {
              Entity.call(this, PurchaseId.purchaseId());
              this._customer = customer;
              this._orders = orders;
              ...
              }

              util.inherits(Purchase, Entity);

              Purchase.prototype.getCustomer = function() {
              return this._customer;
              }
              Purchase.prototype.getOrders = function() {
              return this._orders;
              }
              ...
              module.exports = Purchase;


              Usage



              var purchase = new Purchase(...);
              console.log(purchase.getId()); // Purchase will inherit everything from Entity
              console.log(purchase.getCustomer()); // and have it's own functions too


              Note - notice in base we set the prototype directly but in the derived we modify it? This is deliberate because in the base class the prototype is currently empty so it's fine to completely overwrite. If we did the same thing on the derived class then we'd lose the base prototype therefore we simply append to the existing prototype.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                Based on functions like getId and toJSON, it seems like you would naturally want to have a base class for your models. As such, it would make sense to use write your functions in a way that support prototypical inheritance e.g.



                Base Model



                function Entity(id) {
                this._id = id;
                }

                Entity.prototype = {
                function getId() {
                return this._id;
                }
                ...
                }

                module.exports = Entity;


                Derived Model



                var Entity = require('./entity');
                var util = require('util');

                function Purchase(customer, orders, ...) {
                Entity.call(this, PurchaseId.purchaseId());
                this._customer = customer;
                this._orders = orders;
                ...
                }

                util.inherits(Purchase, Entity);

                Purchase.prototype.getCustomer = function() {
                return this._customer;
                }
                Purchase.prototype.getOrders = function() {
                return this._orders;
                }
                ...
                module.exports = Purchase;


                Usage



                var purchase = new Purchase(...);
                console.log(purchase.getId()); // Purchase will inherit everything from Entity
                console.log(purchase.getCustomer()); // and have it's own functions too


                Note - notice in base we set the prototype directly but in the derived we modify it? This is deliberate because in the base class the prototype is currently empty so it's fine to completely overwrite. If we did the same thing on the derived class then we'd lose the base prototype therefore we simply append to the existing prototype.






                share|improve this answer












                Based on functions like getId and toJSON, it seems like you would naturally want to have a base class for your models. As such, it would make sense to use write your functions in a way that support prototypical inheritance e.g.



                Base Model



                function Entity(id) {
                this._id = id;
                }

                Entity.prototype = {
                function getId() {
                return this._id;
                }
                ...
                }

                module.exports = Entity;


                Derived Model



                var Entity = require('./entity');
                var util = require('util');

                function Purchase(customer, orders, ...) {
                Entity.call(this, PurchaseId.purchaseId());
                this._customer = customer;
                this._orders = orders;
                ...
                }

                util.inherits(Purchase, Entity);

                Purchase.prototype.getCustomer = function() {
                return this._customer;
                }
                Purchase.prototype.getOrders = function() {
                return this._orders;
                }
                ...
                module.exports = Purchase;


                Usage



                var purchase = new Purchase(...);
                console.log(purchase.getId()); // Purchase will inherit everything from Entity
                console.log(purchase.getCustomer()); // and have it's own functions too


                Note - notice in base we set the prototype directly but in the derived we modify it? This is deliberate because in the base class the prototype is currently empty so it's fine to completely overwrite. If we did the same thing on the derived class then we'd lose the base prototype therefore we simply append to the existing prototype.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 25 at 7:44









                James

                47227




                47227






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199606%2fddd-model-of-a-purchase-in-nodejs%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Ellipse (mathématiques)

                    Quarter-circle Tiles

                    Mont Emei