开发者

Good place for complex methods that answer a question

开发者 https://www.devze.com 2023-01-29 04:13 出处:网络
Here is some example code in which we have a method that answers the questions \"Can User X review Product Y?\"

Here is some example code in which we have a method that answers the questions "Can User X review Product Y?"

public namespace Domain
{
    public class User
    {
        ...

        public bool CanWriteReview(Product product)
        {
             return this.IsApproved
                 && !Kernel.Get<ReviewRepository>().UserHasReviewed(product)
                 && !Kernel.Get<SellerAccountRepository>().UserHasSellerAccount(this);
        }
    }
}

I don't like having this method in a Domain POCO because it has too many dependencies that can't be injected and must b开发者_如何学运维e retrieved directly from the kernel instead. Where would you put this type of logic?

I find that the domain events pattern is a useful means of taking service-like logic that depends on Domain state changes, out of the Domain. Is there a similar pattern for the sort of methods above?


The Specification Pattern is a good candidate for this kind of thing.

0

精彩评论

暂无评论...
验证码 换一张
取 消