I'm new to DDD and right now I'm in the process of implementing user registration.
Basically the flow is:
- Controller receives request
- Maps the received data from client to User Domain model.
- Maps the User Domain model to an EF-core entity model.
- Adds user to database using EF-Core.
I'm having some doubts regarding where I place IdGeneratorService
class and BCryptService
class. Currently both of them have an Interface in Domain layer: src/Domain/Model/Services
. Their implementation is residing inside my Infrastructure layer: src/Infrastructure/Services
. They are also both being called inside the User domain model:
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<Role> Type { get; set; }
public static async Task<User> ConstructAsync(
string name, string lastName,
string contactPhone, string contactEmail,
string userName, string password,
IIdGeneratorService<long> idGeneratorService, IBCryptService bCryptService)
{
var id = await idGeneratorService.GenerateAsync();
password = bCryptService.HashPassword(password);
return new User(id, name, lastName, contactPhone, contactEmail, userName, password);
}
This is being called by my controller:
[HttpPost("[Action]")]
public async Task<IActionResult> 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;
}
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.