简体   繁体   English

EF Core DDD 最佳实践

[英]EF Core DDD best practice

I'm trying to refactor some code using DDD approach.我正在尝试使用 DDD 方法重构一些代码。 I'm a bit confused about where I should call a service to get some value, which is needed when creating the object.我有点困惑我应该在哪里调用服务来获得一些价值,这是创建 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.例如创建新用户时,我需要先创建Firebase的用户,获取id,然后将用户存储到我们自己的数据库中。 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.我最终在域实体上使用了 static function,它以 firebase api obj 作为参数,并调用 firebase 在那里创建用户,然后存储将用户 ID 传递给构造函数。

Not ideal, but think this is better than calling firebase directly in the user constructor.不理想,但认为这比直接在用户构造函数中调用 firebase 要好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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