M
M
MIsternik2020-06-13 11:39:52
C++ / C#
MIsternik, 2020-06-13 11:39:52

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 } );


But in the repository, you also need to somehow slip all these dependencies, you can then put the factory in the repository, but if the factory is in the domain layer and they know how to properly initialize the user, this is normal, but the repository is infrastructure and I see it not quite right.

Are there any well-known practices of such entity initializations or what is wrong in my understanding of DDD?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-06-13
@MIsternik

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 question

Ask a Question

731 491 924 answers to any question