【发布时间】:2011-12-27 08:27:34
【问题描述】:
如果一个对象有一个Single Responsibility,以下是否可以接受:
public class Person
{
public string Name;
public DateTime DateOfBirth;
private IStorageService _storageService;
public Person(IStorageService storageService)
{
_storageService = storageService
}
public void Save()
{
_storageService.Persist(this);
}
}
即使用提供的协作者(这也有助于防止领域模型出现问题)。
或者应该是:
public class Person
{
public string Name;
public DateTime DateOfBirth;
public Person()
{
}
}
public class StorageService
{
public void Persist(Person p)
{
}
}
【问题讨论】:
-
我一般都见过后者。前者有什么优势?
-
我认为 person 类没有真正了解 storageservice 的业务,它将 person 类与存储服务耦合在一起,这使得它的可重用性降低
标签: c# oop dependency-injection single-responsibility-principle