Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I myself have used the IRepository approach lately that Keith suggests. But, you should not be focusing on that pattern here. Instead, there are a few more pieces in the DDD playbook that can be applied here.</p> <h2>Use <i>Value Objects</i> for your Addresses</h2> <p>First, there is the concept of Value Objects (VO) you can apply here. In you case, it would be the Address. The difference between a Value Object and an Entity Object is that Entities have an identity; VOs do not. The VO's identity really is the sum of it's properties, not a unique identity. In the book <a href="http://rads.stackoverflow.com/amzn/click/1411609255" rel="nofollow noreferrer">Domain-Drive Design Quickly</a> (it's also a free PDF download), he explains this very well by stating that an address is really just a point on Earth and does not need a separate SocialSecurity-like identity like a person. That point on Earth is the combination of the street, number, city, zip, and country. It can have latitude and longitude values, but still those are even VOs by definition because it's a combination of two points.</p> <h2>Use <em>Services</em> for combining your entities into a single entity to act upon.</h2> <p>Also, do not forget about the Services concept in the DDD playbook. In your example, that service would be:</p> <pre><code>public class PersonCompanyService { void SavePersonCompany(IPersonCompany personCompany) { personRepository.SavePerson(); // do some work for a new company, etc. companyRepository.SaveCompany(); } } </code></pre> <p>There is a need for a service when you have two entities that need both need a similar action to coordinate a combination of other actions. In your case, saving a Person() and creating a blank Company() at the same time.</p> <h2>ORMs usualyl require an identity, period.</h2> <p>Now, how would you go about saving the Address VO in the database? You would use an IAddressRepository obviously. But since most ORMs (i.e. LingToSql) require all objects have an Identity, here's the trick: Mark the identity as internal in your model, so it is not exposed outside of your Model layer. This is Steven Sanderson's own advice.</p> <pre><code>public class Address { // make your identity internal [Column(IsPrimaryKey = true , IsDbGenerated = true , AutoSync = AutoSync.OnInsert)] internal int AddressID { get; set; } // everything else public [Column] public string StreetNumber { get; set; } [Column] public string Street { get; set; } [Column] public string City { get; set; } ... } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload