MongoDB Learning note ~mongodb value objects in an entity

Source: Internet
Author: User

Back to Catalog

Note that the value object referred to here is in the MongoDB entity class, not the value object in DDD , however, the two are also related, that is, it is a complement to the class, itself does not have the value of existence , and in the value object, but also do not need to have a primary key ID, This is also the same as DDD, it is understandable, because it is only a supplementary description of the main object, there is no sense of their own, so there is no use of the primary key, hehe.

Look at a MongoDB value object contact

     Public classPerson { PublicPerson () { contact=Newtest.contact (); Addlist=NewList<test.address>(); Id=MongoDB.Bson.ObjectId.GenerateNewId ().        ToString (); } [Bsonid] [Bsonrepresentation (Bsontype.objectid)] Public stringId {Get;Set; }  Public stringName {Get;Set; }  PublicDateTime Lastcontact {Get;Set; }  PublicDateTime Birthday {Get;Set; }  Public intAge {Get;Set; }  PublicAddress Address {Get;Set; }  PublicList<address> Addlist {Get;Set; } /// <summary>        ///Value Object/// </summary>         PublicContact {Get;Set; } }

     Public class Contact    {        publicstringgetset;}          Public string Get Set ; }          Public string Get Set ; }    }

You see the contact in person is a value object, it does not have an ID, it is just a supplement to people, that is, it is a way of contacting the encapsulation, note that in the MongoDB entity, when establishing the main object, it is necessary to assign values to the value object and the relationship object, which is required, of course, after the assignment, The value of the value object is null, which is also normal and necessary, if not for it

Assignment, then MongoDB is not going to let you go to update (C # driver is like this)

When assigning values to a value object, we can assign values by field, assign values by object, assign values by object, and overwrite the original object!

            vare1 = Repository1. Find ("556e8f2c2683c8105c9e501f"); E1. Name="Wet"; E1. Contact.postcode="100001";//Assigning a value to a fieldE1. Contact =NewContact {Email ="[email protected]"};//assign a value to an entity, the original entity is overwrittenRepository1. Update (E1);

The result of the update, only the email field has a value, the other fields are overwritten, this is normal

MongoDB waits for everyone to explore, research ...

Back to Catalog

MongoDB Learning note ~mongodb value objects in an entity

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.