Skip to content

Instantly share code, notes, and snippets.

@dcomartin
Created October 22, 2025 15:23
Show Gist options
  • Select an option

  • Save dcomartin/ab608bda28f42cc04251989ca48ad7fe to your computer and use it in GitHub Desktop.

Select an option

Save dcomartin/ab608bda28f42cc04251989ca48ad7fe to your computer and use it in GitHub Desktop.
public record NewUser(Guid RequestedByUserId);
public record ResetPassword(Guid RequestedByUserId);
public class User
{
public Guid UserId { get; private set; }
public string Pasword { get; private set; }
public void ResetPassword(Guid requestedUserId)
{
if (UserId != requestedUserId)
{
throw new UnauthorizedAccessException("You are not allowed to reset another users password.");
}
Pasword = PasswordGenerator.Temp();
}
}
public class RestPasswordHandler
{
private readonly DbContext _dbContext;
public RestPasswordHandler(DbContext dbContext)
{
_dbContext = dbContext;
}
public void Handle(ResetPassword request)
{
var user = _dbContext.GetUser(request.RequestedByUserId);
user.ResetPassword(request.RequestedByUserId);
_dbContext.Save(user);
}
}
public class NewUserHandler
{
private readonly DbContext _dbContext;
public NewUserHandler(DbContext dbContext)
{
_dbContext = dbContext;
}
public void Handle(NewUser request)
{
var user = new User();
user.ResetPassword(request.RequestedByUserId);
_dbContext.Save(user);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment