title: DDD construct AggregateRoot Entity from external API, Repositories and Domain Services
body: <p>Trying to apply DDD principles to small project... I have <code>PlayerProfile</code> aggregate root, which consists of <code>Club</code> entity and collection of <code>Rating</code> value objects. Periodically I have to sync all <code>PlayerProfile</code>'s entities from external portal, parsing the raw HTML.</p>
  
  <p>For now I come up with the solution to wrap the code, which renew the <code>PlayerProfile</code>'s in simple <code>PlayerProfileRepository</code>, something like this:</p>
  
  <pre><code>public interface IPlayerProfileRepository
  {
      Task&lt;IReadOnlyCollection&lt;PlayerProfile&gt;&gt; SyncPlayersProfilesFromPortal(string sourceUrl);
      // other methods, which works with data storage
  }
  </code></pre>
  
  <p>First, I don't really like the idea of mixing method, which work with data storage with the methods, which work with external resource (HTML pages) to periodically create <code>PlayerProfile</code>. For me it sounds more like <code>PlayerProfileFactory</code> responsibilities?</p>
  
  <p>The actual implementation of <code>IPlayerProfileRepository</code> delegates parsing of actual pages to 3 <code>IPageParser</code>'s, which actually lives in the same layer as my repositories do. Something like this:</p>
  
  <pre><code>    public PlayerProfileRepository(
          IPageParser&lt;ParseClubDto&gt; clubPageParser,
          IPageParser&lt;ParsePlayerProfileDto&gt; playerProfilePageParser,
          IPageParser&lt;ParseRatingDto&gt; ratingPageParser)
  
      {
          _playerProfilePageParser = playerProfilePageParser;
          _clubPageParser = clubPageParser;
      }
  </code></pre>
  
  <p>I am not quite sure if all these <code>Dtos</code> are in fact <code>Dtos</code> as soon as they are used only from <code>IPageParser</code>'s to save intermediate data while parsing the pages. I would like to keep them closely to <code>IPageParser</code> implementations in data service layer, but not to share them in separate Dtos project and maybe named differently.</p>
  
  <p>After <code>ParseClubDto</code>, <code>ParsePlayerProfileDto</code> and <code>ParseRatingDto</code> parsed, I passed it to <code>PlayerProfileFactory.Create</code> factory method, something like this:</p>
  
  <pre><code>var playerProfiles = new List&lt;PlayerProfile&gt;();
  
  var clubs = await _clubPageParser.ParseAsync(sourceUrl);
  
  foreach (var club in clubs)
  {
      var clubPlayers = await _playerProfilePageParser.ParseAsync(club.PlayersPageUrl);
  
      foreach (var clubPlayer in clubPlayers)
      {
          var ratings = await _ratingPageParser.ParseAsync(clubPlayer.RatingsPageUrl);
  
          playerProfiles.Add(PlayerProfileFactory.Create(club, clubPlayer, ratings));
       }
  }
  
  return playerProfiles;
  </code></pre>
  
  <p>After this is done I have to perform actual syncing with existing agreggate roots in DB, which I do simple by calling <code>ResyncFrom(PlayerProfile profile)</code> on aggregate root or should it be more like separate <code>PlayerProfile</code> domain service?</p>
  
  <p>In general I got a feeling that I am doing something wrong, so please any comments are welcomed?</p>