title: Where to initialise model objects within a redux / ngrx application architecture
body: <p><strong>Background:</strong></p>
  
  <p>Team is building a large REST-based web application using Angular and the @ngrx library for state management.</p>
  
  <p>We wish to model entities from the server as TypeScript classes. These may be: accounts, users etc</p>
  
  <p>This achieves:</p>
  
  <ul>
  <li>Loose coupling to the API; if the response changes, only the model must change</li>
  <li>Encapsulating basic functionality e.g. string concatenation of first and last name to make <code>fullName</code></li>
  </ul>
  
  <p>The uncertainty lies in when, during the application's timeline, to initialise the model, calling: <code>new Account(accountResponse)</code>.</p>
  
  <p>Conventional logic suggests to do this as early as possible, in a service along side the logic to retrieve the accounts (be it from a cache, server response, etc).</p>
  
  <pre class="lang-ts prettyprint-override"><code>this.apiService.fetch(AccountService.URL)
        .map(accounts =&gt; accounts.map((a: AccountResponse) =&gt; new Account(a)));
  </code></pre>
  
  <p>This method is invoked by an ngrx effect, then following a successful response, the Account objects are added to the store by the reducer.</p>
  
  <p>This works, however... ngrx / redux "<a href="https://stackoverflow.com/questions/32352982/how-to-put-methods-onto-the-objects-in-redux-state/32922984#32922984">best</a> <a href="http://redux.js.org/docs/faq/OrganizingState.html#can-i-put-functions-promises-or-other-non-serializable-items-in-my-store-state" rel="noreferrer">practice</a>" states only plain objects and primitives should be kept in the store, for ease of serialisation among other reasons.</p>
  
  <p>To adhere to this advice, initialising the Account object must happen much further down the line.  Either in individual components, <s><a href="https://stackoverflow.com/questions/43520435/where-to-initialise-model-objects-within-a-redux-ngrx-store-application-archit?noredirect=1#comment87669220_43520435">in a state selector</a></s>, or generally wherever an account is used.</p>
  
  <p>This doesn't make sense to me, as the raw account response objects are being passed around the application, somewhat defeating the point of wrapping them in a model in the first place.</p>
  
  <p>The application is structurally similar to <a href="https://github.com/ngrx/example-app" rel="noreferrer">@ngrx/example</a> book app which, given its simplicity, does not wrap the server responses in model objects.</p>
  
  <hr>
  
  <p><strong>Questions:</strong></p>
  
  <ul>
  <li><p>What are the detrimental effects of keeping initialised classes in the store (besides serialisability)?</p></li>
  <li><p>If only plain objects are to be kept in the store, where in the flow of data through the app are model <code>class</code>es best initialised?</p></li>
  </ul>