title: Mapping between the persistence model and the domain model
body: <p>I have done an IMMENSE amount of reading about domain driven design and have done some fairly complex projects with that design.  All of them had their flaws and anti-patterns that were realized along the way.   Understandable as this was a learning process.   I, however, am stuck on a major concept that I cannot seem to resolve via Google (maybe I'm just not coming up with the right search terms) or by my own trial and error.   </p>
  
  <p>I have read several articles that claim emphatically to keep your Domain Model and your Persistence Model separate.  Don't let things like an ID leak into your Domain Model unless there is a domain purpose for that ID.  With that policy, how does one persist a Domain Model IN PRACTICE?  All the articles I have read talk about this in the abstract, but I cannot find a concrete example that DOESN'T violate this.   </p>
  
  <p>I have a relatively large and complex web application that I'm building and would like to achieve the "best" Domain and Persistence separation I can.  I am using a hand-rolled ORM (yes, yes, I know - I shouldn't - blah blah blah - however, the underlying tables and queries are much too complex to use something like EF or NHibernate well).  In this accounting package at a large university, I have General Ledger Journal Entries with the following structure:</p>
  
  <pre><code> Public Class Journal
  
      Public Property AccountCode As SFSAccountCode = Nothing
  
      Public Property Amount As Decimal = 0
  
      Public Property BudgetCategory As BudgetCategory = Nothing
  
      Public Property [Date] As DateTime = Nothing
  
      Public Property ChildAccount As ChildAccount = Nothing
  
      Public Property Description As String = ""
  
      Public Property FiscalYear As SFSFiscalYear = Nothing
  
      Public Property Fund As Fund = Nothing
  
      Public Property JournalID As Int32 = -1
  
      Public Property Notes As String = ""
  
      Public Property Program As String = ""
  
      Public Property Source As JournalSource = Nothing
  
      Public Property Status As JournalEntryStatus = JournalEntryStatus.Open
  
      Public Property TransactionType As TransactionType = Nothing
  End Class
  </code></pre>
  
  <p>Without including the uniqueID  (JournalID), how can I map an instance in the Domain Model to an instance in the Persistence Model?   From my understanding, you are to consider an object unique by its invariants.   Easy, if your object is only one or two properties as strings or ints. I clearly have many properties of which several are domain models themselves. </p>
  
  <p>I'm sure there is some key concept that I have just missed - can anyone point me to a resource (with concrete code as bonus!) to help explain how one can map between a persistent model with database IDs to a domain model without?</p>
  
  <p>By the way, yes I know my properties should be private sets for good domain design.  And they will be once I can figure out the mappings between persistence and domain better.  And I happen to code in VB.NET but can read Java or C# fine, as I'm sure most examples will be in one of those two languages.</p>