DDD, how do you change a ValueObject?












1














The question refers to a situation in which you have to update/delete a valueobject.



Consider this situation.



You have an Order and you have Products and Products have ProductItems. Where ProductItem is a ValueObject. If you would have to update the list of productItems in Product (let's say update the quantity), how would you do it? How will you identify the exact ProductItem that requires the update ?



This is from the perspective or an OrderingContext, there should be a backing context where you have ProductItem stock and ProductItem pricing (InventoryContext), context where a ProductItem is more then just a ValueObject, but since we are under the OrderingContext, how would you update a ProductItem if you don't have a way to identify it?










share|improve this question






















  • What is a ProductItem? Value Objects by their very nature are identified by their value (D'oh). If you have a Money value object, then it's identified by "Amount" (i.e. a decimal value) and "Currency" (either another value type or a string)
    – Tseng
    Nov 20 '18 at 10:55










  • @Tseng: a ProductItem is something that has a name and a quantity. So assuming you want to update the quantity, how would you do it ? I'm guessing you identify it by its ProductId and the name ?
    – MCR
    Nov 20 '18 at 11:07












  • product.ProductItem = new ProductItem(5, "Some Description") ;) That's the very nature of it. On a collection, you first need to remove it then add a new, value type to it. Also generally you seem to have a bad understanding of DDD. Are Orders and Products really a different bounded context? If yes, you don't change one of it from the other context. A bounded context never directly acts on another BC's aggregates. You always have to use some kind of anti-corruption layer to transfer that to a message and that back into the other bounded context
    – Tseng
    Nov 20 '18 at 11:13






  • 2




    As for how to identify it? You search for an ProductItem which as quantity 5 and Some Description as description. Thats how value objects are identified, by the SUM of their properties unlike entities which are identified by an unique id
    – Tseng
    Nov 20 '18 at 11:14
















1














The question refers to a situation in which you have to update/delete a valueobject.



Consider this situation.



You have an Order and you have Products and Products have ProductItems. Where ProductItem is a ValueObject. If you would have to update the list of productItems in Product (let's say update the quantity), how would you do it? How will you identify the exact ProductItem that requires the update ?



This is from the perspective or an OrderingContext, there should be a backing context where you have ProductItem stock and ProductItem pricing (InventoryContext), context where a ProductItem is more then just a ValueObject, but since we are under the OrderingContext, how would you update a ProductItem if you don't have a way to identify it?










share|improve this question






















  • What is a ProductItem? Value Objects by their very nature are identified by their value (D'oh). If you have a Money value object, then it's identified by "Amount" (i.e. a decimal value) and "Currency" (either another value type or a string)
    – Tseng
    Nov 20 '18 at 10:55










  • @Tseng: a ProductItem is something that has a name and a quantity. So assuming you want to update the quantity, how would you do it ? I'm guessing you identify it by its ProductId and the name ?
    – MCR
    Nov 20 '18 at 11:07












  • product.ProductItem = new ProductItem(5, "Some Description") ;) That's the very nature of it. On a collection, you first need to remove it then add a new, value type to it. Also generally you seem to have a bad understanding of DDD. Are Orders and Products really a different bounded context? If yes, you don't change one of it from the other context. A bounded context never directly acts on another BC's aggregates. You always have to use some kind of anti-corruption layer to transfer that to a message and that back into the other bounded context
    – Tseng
    Nov 20 '18 at 11:13






  • 2




    As for how to identify it? You search for an ProductItem which as quantity 5 and Some Description as description. Thats how value objects are identified, by the SUM of their properties unlike entities which are identified by an unique id
    – Tseng
    Nov 20 '18 at 11:14














1












1








1







The question refers to a situation in which you have to update/delete a valueobject.



Consider this situation.



You have an Order and you have Products and Products have ProductItems. Where ProductItem is a ValueObject. If you would have to update the list of productItems in Product (let's say update the quantity), how would you do it? How will you identify the exact ProductItem that requires the update ?



This is from the perspective or an OrderingContext, there should be a backing context where you have ProductItem stock and ProductItem pricing (InventoryContext), context where a ProductItem is more then just a ValueObject, but since we are under the OrderingContext, how would you update a ProductItem if you don't have a way to identify it?










share|improve this question













The question refers to a situation in which you have to update/delete a valueobject.



Consider this situation.



You have an Order and you have Products and Products have ProductItems. Where ProductItem is a ValueObject. If you would have to update the list of productItems in Product (let's say update the quantity), how would you do it? How will you identify the exact ProductItem that requires the update ?



This is from the perspective or an OrderingContext, there should be a backing context where you have ProductItem stock and ProductItem pricing (InventoryContext), context where a ProductItem is more then just a ValueObject, but since we are under the OrderingContext, how would you update a ProductItem if you don't have a way to identify it?







architecture domain-driven-design software-design value-objects






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 10:43









MCR

527216




527216












  • What is a ProductItem? Value Objects by their very nature are identified by their value (D'oh). If you have a Money value object, then it's identified by "Amount" (i.e. a decimal value) and "Currency" (either another value type or a string)
    – Tseng
    Nov 20 '18 at 10:55










  • @Tseng: a ProductItem is something that has a name and a quantity. So assuming you want to update the quantity, how would you do it ? I'm guessing you identify it by its ProductId and the name ?
    – MCR
    Nov 20 '18 at 11:07












  • product.ProductItem = new ProductItem(5, "Some Description") ;) That's the very nature of it. On a collection, you first need to remove it then add a new, value type to it. Also generally you seem to have a bad understanding of DDD. Are Orders and Products really a different bounded context? If yes, you don't change one of it from the other context. A bounded context never directly acts on another BC's aggregates. You always have to use some kind of anti-corruption layer to transfer that to a message and that back into the other bounded context
    – Tseng
    Nov 20 '18 at 11:13






  • 2




    As for how to identify it? You search for an ProductItem which as quantity 5 and Some Description as description. Thats how value objects are identified, by the SUM of their properties unlike entities which are identified by an unique id
    – Tseng
    Nov 20 '18 at 11:14


















  • What is a ProductItem? Value Objects by their very nature are identified by their value (D'oh). If you have a Money value object, then it's identified by "Amount" (i.e. a decimal value) and "Currency" (either another value type or a string)
    – Tseng
    Nov 20 '18 at 10:55










  • @Tseng: a ProductItem is something that has a name and a quantity. So assuming you want to update the quantity, how would you do it ? I'm guessing you identify it by its ProductId and the name ?
    – MCR
    Nov 20 '18 at 11:07












  • product.ProductItem = new ProductItem(5, "Some Description") ;) That's the very nature of it. On a collection, you first need to remove it then add a new, value type to it. Also generally you seem to have a bad understanding of DDD. Are Orders and Products really a different bounded context? If yes, you don't change one of it from the other context. A bounded context never directly acts on another BC's aggregates. You always have to use some kind of anti-corruption layer to transfer that to a message and that back into the other bounded context
    – Tseng
    Nov 20 '18 at 11:13






  • 2




    As for how to identify it? You search for an ProductItem which as quantity 5 and Some Description as description. Thats how value objects are identified, by the SUM of their properties unlike entities which are identified by an unique id
    – Tseng
    Nov 20 '18 at 11:14
















What is a ProductItem? Value Objects by their very nature are identified by their value (D'oh). If you have a Money value object, then it's identified by "Amount" (i.e. a decimal value) and "Currency" (either another value type or a string)
– Tseng
Nov 20 '18 at 10:55




What is a ProductItem? Value Objects by their very nature are identified by their value (D'oh). If you have a Money value object, then it's identified by "Amount" (i.e. a decimal value) and "Currency" (either another value type or a string)
– Tseng
Nov 20 '18 at 10:55












@Tseng: a ProductItem is something that has a name and a quantity. So assuming you want to update the quantity, how would you do it ? I'm guessing you identify it by its ProductId and the name ?
– MCR
Nov 20 '18 at 11:07






@Tseng: a ProductItem is something that has a name and a quantity. So assuming you want to update the quantity, how would you do it ? I'm guessing you identify it by its ProductId and the name ?
– MCR
Nov 20 '18 at 11:07














product.ProductItem = new ProductItem(5, "Some Description") ;) That's the very nature of it. On a collection, you first need to remove it then add a new, value type to it. Also generally you seem to have a bad understanding of DDD. Are Orders and Products really a different bounded context? If yes, you don't change one of it from the other context. A bounded context never directly acts on another BC's aggregates. You always have to use some kind of anti-corruption layer to transfer that to a message and that back into the other bounded context
– Tseng
Nov 20 '18 at 11:13




product.ProductItem = new ProductItem(5, "Some Description") ;) That's the very nature of it. On a collection, you first need to remove it then add a new, value type to it. Also generally you seem to have a bad understanding of DDD. Are Orders and Products really a different bounded context? If yes, you don't change one of it from the other context. A bounded context never directly acts on another BC's aggregates. You always have to use some kind of anti-corruption layer to transfer that to a message and that back into the other bounded context
– Tseng
Nov 20 '18 at 11:13




2




2




As for how to identify it? You search for an ProductItem which as quantity 5 and Some Description as description. Thats how value objects are identified, by the SUM of their properties unlike entities which are identified by an unique id
– Tseng
Nov 20 '18 at 11:14




As for how to identify it? You search for an ProductItem which as quantity 5 and Some Description as description. Thats how value objects are identified, by the SUM of their properties unlike entities which are identified by an unique id
– Tseng
Nov 20 '18 at 11:14












1 Answer
1






active

oldest

votes


















2














You identify the Value object in a list by whatever means you need, there is not rule regarding that.



For example, in this case, one way to identify it is to use it's numeric index from the list. If you need something more stable (i.e. that works also in case of items reordering) you can have GUIDs instead of numeric indexes, in languages that support this. For example, PHP lets you use strings for array access and GUIDs can be converted to strings before use in an array lookup.



The important rule to remember about Value objects: they should be immutable, that is, if you need to update one then you replace it with another instance.






share|improve this answer





















    Your Answer






    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: "1"
    };
    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',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    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%2fstackoverflow.com%2fquestions%2f53391249%2fddd-how-do-you-change-a-valueobject%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









    2














    You identify the Value object in a list by whatever means you need, there is not rule regarding that.



    For example, in this case, one way to identify it is to use it's numeric index from the list. If you need something more stable (i.e. that works also in case of items reordering) you can have GUIDs instead of numeric indexes, in languages that support this. For example, PHP lets you use strings for array access and GUIDs can be converted to strings before use in an array lookup.



    The important rule to remember about Value objects: they should be immutable, that is, if you need to update one then you replace it with another instance.






    share|improve this answer


























      2














      You identify the Value object in a list by whatever means you need, there is not rule regarding that.



      For example, in this case, one way to identify it is to use it's numeric index from the list. If you need something more stable (i.e. that works also in case of items reordering) you can have GUIDs instead of numeric indexes, in languages that support this. For example, PHP lets you use strings for array access and GUIDs can be converted to strings before use in an array lookup.



      The important rule to remember about Value objects: they should be immutable, that is, if you need to update one then you replace it with another instance.






      share|improve this answer
























        2












        2








        2






        You identify the Value object in a list by whatever means you need, there is not rule regarding that.



        For example, in this case, one way to identify it is to use it's numeric index from the list. If you need something more stable (i.e. that works also in case of items reordering) you can have GUIDs instead of numeric indexes, in languages that support this. For example, PHP lets you use strings for array access and GUIDs can be converted to strings before use in an array lookup.



        The important rule to remember about Value objects: they should be immutable, that is, if you need to update one then you replace it with another instance.






        share|improve this answer












        You identify the Value object in a list by whatever means you need, there is not rule regarding that.



        For example, in this case, one way to identify it is to use it's numeric index from the list. If you need something more stable (i.e. that works also in case of items reordering) you can have GUIDs instead of numeric indexes, in languages that support this. For example, PHP lets you use strings for array access and GUIDs can be converted to strings before use in an array lookup.



        The important rule to remember about Value objects: they should be immutable, that is, if you need to update one then you replace it with another instance.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 20 '18 at 12:36









        Constantin Galbenu

        10.7k21430




        10.7k21430






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53391249%2fddd-how-do-you-change-a-valueobject%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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?