title: Best approach for Id generation and password hashing using DDD for User Domain model
body: <p>I'm new to DDD and right now I'm in the process of implementing user registration. </p>
  
  <p>Basically the flow is:  </p>
  
  <ol>
  <li>Controller receives request  </li>
  <li>Maps the received data from client to User Domain model.    </li>
  <li>Maps the User Domain model to an EF-core entity model.    </li>
  <li>Adds user to database using EF-Core.</li>
  </ol>
  
  <p>I'm having some doubts regarding where I place <code>IdGeneratorService</code> class and <code>BCryptService</code> class. Currently both of them have an Interface in Domain layer: <code>src/Domain/Model/Services</code>. Their implementation is residing inside my Infrastructure layer: <code>src/Infrastructure/Services</code>. They are also both being called inside the User domain model:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class User
  {
      private User(
          long id, 
          string name, 
          string lastName, 
          string contactPhone, 
          string contactEmail, 
          string userName, 
          string password)
      {
          Id = id;
          Name = name;
          LastName = lastName;
          ContactPhone = contactPhone;
          ContactEmail = contactEmail;
          UserName = userName;
          Password = password;
      }
  
      public IEnumerable&lt;Role&gt; Type { get; set; }
  
      public static async Task&lt;User&gt; ConstructAsync(
          string name, string lastName, 
          string contactPhone, string contactEmail, 
          string userName, string password, 
          IIdGeneratorService&lt;long&gt; idGeneratorService, IBCryptService bCryptService)
      {
          var id = await idGeneratorService.GenerateAsync();
  
          password = bCryptService.HashPassword(password);
  
          return new User(id, name, lastName, contactPhone, contactEmail, userName, password); 
      } 
  </code></pre>
  
  <p>This is being called by my controller: </p>
  
  <pre class="lang-cs prettyprint-override"><code>[HttpPost("[Action]")]
  public async Task&lt;IActionResult&gt; Create([FromBody] UserModel userModel)
  {
      var user = 
          await DomainUser.ConstructAsync(
              userModel.Name, userModel.LastName,
              userModel.ContactPhone, userModel.ContactEmail,
              userModel.UserName, userModel.Password,
              _idGeneratorService, _bCryptService);
  
      await _userRepository.AddAsync(user);
  
      return sampleResponse;
  }
  </code></pre>
  
  <p>I feel like calling the IdGenerator and BCrypt inside domain model is bad practice, because from what I understand the Domain layer cannot know about the Infrastructure layer, so I'm not really sure how I go about this. Any other help/suggestions regarding other implementations based on what you understood here is greatly appreciated.</p>