简体   繁体   中英

EF Core DDD best practice

I'm trying to refactor some code using DDD approach. I'm a bit confused about where I should call a service to get some value, which is needed when creating the object.

Eg when creating new user, I need to first create the user in Firebase, get the id, then store the user on our own database. I have an overloaded user constructor like this:

public User(string displayName, string email, FirebaseService firebase, Role role)
{
   var uid = firebase.CreateUserAsync(...).Result;
   // Set private properties
   FirebaseId = uid;
   Email = email;
   ....
}

I'm not sure if this is the right approach. It just feels wrong to inject a service to the constructor, also can't await on an async method in constructor. Just like to check if there are other options...

Thanks in advance!

I ended up using a static function on the domain entity, which has firebase api obj as a parameter, and calling firebase to create the user there, then storing passing the user id to the constructor.

Not ideal, but think this is better than calling firebase directly in the user constructor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM