Following the good practices of DDD, Aggregate and Aggregate Root. I have the following scenario:
User
(Aggregate Root)- A collection of
UserEmail
(insideUser
)
Imagining that I have registered a User with 10 Emails, what would be the most correct and perfomable way of updating one of these emails?
Method 1
static void UpdateEmailForExistingUserMethod1()
{
var userId = new Guid("f0cd6e3e-b95b-4dab-bb0b-7e6c6e1b0855");
var emailId = new Guid("804aff75-8e48-4f53-b55d-8d3ca76a2df9");
using(var repository = new UserRepository())
{
// I'm going to return the user with all their emails?
// I will not have performance problems for bringing all emails from this user?
var user = repository.GetUserById(userId);
if (user == null)
{
Console.WriteLine("User not found");
return;
}
// Updating Email in Aggregate Root
user.UpdateEmail(emailId, "updated1@email.com");
// Commit in repository
if (repository.Commit() > 0)
{
Console.WriteLine("E-mail updated with method 1!");
};
}
}
Method 2:
static void UpdateEmailForExistingUserMethod2()
{
var usuarioId = new Guid("f0cd6e3e-b95b-4dab-bb0b-7e6c6e1b0855");
var emailId = new Guid("3b9c2f36-659e-41e8-a1c6-d879ab58352c");
using(var usuarioRepository = new UserRepository())
{
if (!usuarioRepository.UserExists(usuarioId))
{
Console.WriteLine("User not found");
return;
}
if (!usuarioRepository.EmailExists(emailId))
{
Console.WriteLine("E-mail not found");
return;
}
// Grab only the email that I will update from the repository,
// optimizing performance
var usuarioEmail = usuarioRepository.GetEmailById(emailId);
// Updates the e-mail through a method of the e-mail entity itself
usuarioEmail.Update("updated2@email.com");
// Commit in repository
if (usuarioRepository.Commit() > 0)
{
Console.WriteLine("E-mail updated with method 2!");
};
}
}