Answer the question
In order to leave comments, you need to log in
Creating models in DDD, how to pass dependencies?
I'm trying to apply DDD to the authorization service.
If the user (User) is the main entity of the aggregate, then the tokens are its internal entities and it must somehow manage and verify them. So he needs to access the repository. In addition to storage, there is a need for other functions, such as IHashProvider, ITokenProvider for hashing and password verification, generating tokens.
The question is how to shove it all into the User unnoticed by the application layer?
When creating a new user, you can make a factory and use a private method through reflection
static User New( string email, string name, string password, IHashProvider hashProvider, IUserRepository repo )
return (User)typeof( User )
.GetMethod( "New" )
.Invoke( null, new object[] { email, name, password, _hashProvider, _repository } );
Answer the question
In order to leave comments, you need to log in
What are you doing wrong.
The user does not create himself, this is nonsense, this is done by a specially trained class.
if (ModelState.IsValid)
{
var user = new AppUser { UserName = Input.Email, Email = Input.Email };
var result = await _userManager.CreateAsync(user, Input.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question