{"id":7913,"date":"2024-10-01T04:19:40","date_gmt":"2024-10-01T04:19:40","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=7913"},"modified":"2024-10-01T06:05:04","modified_gmt":"2024-10-01T06:05:04","slug":"value-object-pattern-with-an-example-in-c","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/","title":{"rendered":"Value Object Pattern with an Example in C#"},"content":{"rendered":"<p><strong>What is a Value Object?<\/strong><\/p>\n<p>In Domain-Driven Design (DDD), Value Objects represent concepts or attributes that don&#8217;t have their own identity but are defined by their attributes. These objects are immutable, meaning their state cannot be changed once they are created. Value Objects are distinguished from Entities, which have their own identity.<\/p>\n<p><strong>Characteristics of Value Objects:<\/strong><\/p>\n<ul>\n<li><strong>Immutability:<\/strong> Once a Value Object is created, its state cannot be changed. This ensures that its value remains consistent and predictable.<\/li>\n<li><strong>Equality by Value:<\/strong> Value Objects are compared based on the equality of their attributes rather than their identity. If two Value Objects have the same attributes, they are considered equal, regardless of whether they are the same instance.<\/li>\n<li><strong>Side-Effect Free:<\/strong> Value Objects do not perform any side effects. They are pure functions of their attributes.<\/li>\n<\/ul>\n<p><strong>Example:<\/strong> Let&#8217;s understand with an example in <a href=\"https:\/\/studysection.com\/blog\/c-coding-standards-and-best-practices\/\">C#<\/a>. EmailAddress Value Object<\/p>\n<p><code><br \/>\nusing System;<br \/>\npublic class EmailAddress : IEquatable<br \/>\n{<br \/>\nprivate readonly string _value;<br \/>\npublic EmailAddress(string value)<br \/>\n{<br \/>\nif (string.IsNullOrWhiteSpace(value))<br \/>\nthrow new ArgumentException(\"Email address cannot be empty.\", nameof(value));<br \/>\n_value = value;<br \/>\n}<br \/>\npublic bool Equals(EmailAddress other)<br \/>\n{<br \/>\nif (other is null) return false;<br \/>\nif (ReferenceEquals(this, other)) return true;<br \/>\nreturn _value == other._value;<br \/>\n}<br \/>\n<\/code><br \/>\n<code><br \/>\npublic override bool Equals(object obj)<br \/>\n{<br \/>\nreturn Equals(obj as EmailAddress);<br \/>\n}<br \/>\npublic override int GetHashCode()<br \/>\n{<br \/>\nreturn _value.GetHashCode();<br \/>\n}<br \/>\npublic override string ToString()<br \/>\n{<br \/>\nreturn _value;<br \/>\n}<br \/>\n}<br \/>\nclass Program<br \/>\n{<br \/>\nstatic void Main(string[] args)<br \/>\n{<br \/>\ntry<br \/>\n{<br \/>\nvar email1 = new EmailAddress(\"user@example.com\");<br \/>\nvar email2 = new EmailAddress(\"user@example.com\");<br \/>\nvar email3 = new EmailAddress(\"anotheruser@example.com\");<br \/>\nConsole.WriteLine($\"Email 1: {email1}\");<br \/>\nConsole.WriteLine($\"Email 2: {email2}\");<br \/>\nConsole.WriteLine($\"Email 3: {email3}\");<br \/>\nConsole.WriteLine($\"Is email1 equal to email2? {email1.Equals(email2)}\");<br \/>\nConsole.WriteLine($\"Is email1 equal to email3? {email1.Equals(email3)}\");<br \/>\n}<br \/>\ncatch (ArgumentException ex)<br \/>\n{<br \/>\nConsole.WriteLine($\"Error: {ex.Message}\");<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n<\/code><\/p>\n<p><strong>Explanation:<\/strong><\/p>\n<ul>\n<li>We define the EmailAddress class as a Value Object representing an email address.<\/li>\n<li>The constructor ensures that the email address provided is not null or empty.<\/li>\n<li>We override the Equals method to compare email addresses based on their string value.<\/li>\n<li>In the Main method, we create three instances of EmailAddress representing different email addresses.<\/li>\n<li>We print out the email addresses and check for equality between them.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is a Value Object? In Domain-Driven Design (DDD), Value Objects represent concepts or attributes that don&#8217;t have their own<\/p>\n","protected":false},"author":1,"featured_media":7917,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Value Object Pattern with an Example in C#<\/title>\n<meta name=\"description\" content=\"In Domain-Driven Design (DDD), a Value Object represents a concept that doesn&#039;t have its own identity but is defined by its attributes.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Value Object Pattern with an Example in C#\" \/>\n<meta property=\"og:description\" content=\"In Domain-Driven Design (DDD), a Value Object represents a concept that doesn&#039;t have its own identity but is defined by its attributes.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-01T04:19:40+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-01T06:05:04+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2024\/10\/Add-a-subheading-5.png\" \/>\n\t<meta property=\"og:image:width\" content=\"940\" \/>\n\t<meta property=\"og:image:height\" content=\"788\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"Value Object Pattern with an Example in C#\",\"datePublished\":\"2024-10-01T04:19:40+00:00\",\"dateModified\":\"2024-10-01T06:05:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\"},\"wordCount\":218,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\",\"url\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\",\"name\":\"Value Object Pattern with an Example in C#\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2024-10-01T04:19:40+00:00\",\"dateModified\":\"2024-10-01T06:05:04+00:00\",\"description\":\"In Domain-Driven Design (DDD), a Value Object represents a concept that doesn't have its own identity but is defined by its attributes.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Value Object Pattern with an Example in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Value Object Pattern with an Example in C#","description":"In Domain-Driven Design (DDD), a Value Object represents a concept that doesn't have its own identity but is defined by its attributes.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/","og_locale":"en_US","og_type":"article","og_title":"Value Object Pattern with an Example in C#","og_description":"In Domain-Driven Design (DDD), a Value Object represents a concept that doesn't have its own identity but is defined by its attributes.","og_url":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2024-10-01T04:19:40+00:00","article_modified_time":"2024-10-01T06:05:04+00:00","og_image":[{"width":940,"height":788,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2024\/10\/Add-a-subheading-5.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"Value Object Pattern with an Example in C#","datePublished":"2024-10-01T04:19:40+00:00","dateModified":"2024-10-01T06:05:04+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/"},"wordCount":218,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/","url":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/","name":"Value Object Pattern with an Example in C#","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2024-10-01T04:19:40+00:00","dateModified":"2024-10-01T06:05:04+00:00","description":"In Domain-Driven Design (DDD), a Value Object represents a concept that doesn't have its own identity but is defined by its attributes.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/value-object-pattern-with-an-example-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Value Object Pattern with an Example in C#"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":207,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7913"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=7913"}],"version-history":[{"count":4,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7913\/revisions"}],"predecessor-version":[{"id":7918,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/7913\/revisions\/7918"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/7917"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=7913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=7913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=7913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}