0

我想在我的项目中应用领域驱动设计原则,但无法确定我应该如何处理依赖模型的业务逻辑。

例如,假设这种情况:
我有PersonCar域模型。每个人都适合根据年龄/预算/偏好/等从 db 购买某些汽车。在我的模型中,我想要一个SuitableCars适合此人的汽车列表 ( )。

public class Person
{
    public List<Car> SuitableCars {get; set;}
}

但现在为了做到这一点,我必须调用一个服务方法 ( GetSuitableCarsForPerson) 从 db(带有存储库的 DI)中获取数据,运行我的(有时相当复杂的多模型相关)自定义逻辑并获取汽车。

public class PersonService : IPersonService
{
    private IRepository _repo;

    public PersonService(IPRepository repository)
    {
        _repo = repository;
    }

    public List<Car> GetSuitableCarsForPerson(Person person)
    {
        // business goes here right now.
    }

}

所以SuitableCars财产的声明会变成:

private IPersonService _personService;
public List<Car> SuitableCars 
{
    get
    {
        // I have to inject a PersonService in my model. Bad practice?
        return _personService.GetSuitableCarsForPerson(this);
    }
}

AFAIK,服务应该保持精简(参考)并用于让您将与非域模型相关的业务放入其中。但我相信我所描述的逻辑属于模型本身。

那么,如何处理这些我应该访问相关模型并进行各种自定义验证/过滤器以获取适当数据的逻辑?
谢谢你。

4

2 回答 2

1

希望这个答案https://stackoverflow.com/a/1209765/145595将提供一些指导如何继续。

于 2012-04-30T21:09:12.890 回答
0

It seems like the definition of the set of suitable cars for a person depends on factors outside of the person model and can vary independent of the person. The set of suitable cars depends not only on the person's preferences but also on the set of all cars. The set of cars varies independently of the person and so the set of suitable cars for a given person is a cache of an operation that determines the set of suitable cars. These observations indicate that a repository or domain service should return sets of suitable cars for a person and the association between a person and the set of suitable cars should not be expressed on the person model directly. What could be an appropriate association to express directly on the person model is the set of cars that person specified as preferred models because in this case the person is the "owner" of this data. In DDD, it is acceptable to express associations directly in models or through the use of repositories and selecting a specific approach depends on several factors, some of which are outlined above. Take a look at this series of articles for an in-depth treatment of how to design your models.

于 2012-05-01T17:05:42.800 回答