我试图避免此类ContentDomain成为God类,并将功能隔离为特定的类(以遵循SRP),如下所示
ContentDomain:
public class ContentDomain : IContentDomain
{
private ISolutionDomain solutionDomain;
private IServiceDomain serviceDomain;
private IPhaseDomain phaseDomain;
public ContentDomain(IUnitOfWork _unitOfWork)
{
this.solutionDomain = new SolutionDomain(_unitOfWork);
this.serviceDomain = new ServiceDomain(_unitOfWork);
this.phaseDomain = new PhaseDomain(_unitOfWork);
}
public ISolutionDomain SolutionDomain { get { return solutionDomain; } }
public IServiceDomain ServiceDomain { get { return serviceDomain; } }
public IPhaseDomain PhaseDomain { get { return phaseDomain; } }
}
特定领域类别之一
public class SolutionDomain : BaseDomain, ISolutionDomain
{
public SolutionDomain(IUnitOfWork _unitOfWork)
: base(_unitOfWork)
{
}
public IEnumerable<Solution> GetAllSolutions()
{
return base.GetAll<Solution>(sol => sol.IsActive == true).OrderBy(rec => rec.Name).Select(rec => rec).ToList();
}
}
现在,我的控制器仅了解ContentDomain并在需要时从SolutionDomain / ServiceDomain / PhaseDomain调用特定方法:
public ContentController(IContentDomain domain, ICurrentUser currentUser)
: base(domain, currentUser)
{
}
public ActionResult Home()
{
var myServices = domain.ServiceDomain.GetServicesWithDetails(rec => rec.CreatedBy == currentUser.Name);
var viewModelCollection = myServices.Select(service => new DashboardViewModel(service, domain));
if (currentUser.IsInRole("SU"))
return View("Home_SU", viewModelCollection);
else if (currentUser.IsInRole("Reviewer"))
return View("Home_Reviewer", viewModelCollection);
else return View("Home", viewModelCollection);
}
注意Home()中的第一条语句
domain.ServiceDomain.GetServicesWithDetails(rec => rec.CreatedBy == currentUser.Name);
我发现自己在ContentDomain类中混合了Facade和Composition。
现在的问题是-
使用合成通过Facade公开特定的域功能是否合理?
根据示例,ContentDomain
类和IContentDomain
接口不提供任何功能。更好的组合形式是丢弃两者,并根据它们所需的最小依赖集来定义Controller和其他客户端:
private readonly IServiceDomain serviceDomain;
private readonly ICurrentUser currentUser;
public ServiceController(IServiceDomain serviceDomain, ICurrentUser currentUser)
{
this.serviceDomain = serviceDomain;
this.currentUser = currentUser;
}
public ActionResult Home()
{
var myServices = this.serviceDomain.GetServicesWithDetails(
rec => rec.CreatedBy == currentUser.Name);
var viewModelCollection = myServices.Select(
service => new DashboardViewModel(service, domain));
if (this.currentUser.IsInRole("SU"))
return View("Home_SU", viewModelCollection);
else if (this.currentUser.IsInRole("Reviewer"))
return View("Home_Reviewer", viewModelCollection);
else return View("Home", viewModelCollection);
}
这是真实的成分,因为您撰写ServiceController
与实现IServiceDomain
和ICurrentUser
。
如果没有,那可能是什么呢?
IContentDomain
。的设计存在几个问题。
IContentDomain
,都需要将其作为(只读)属性添加到接口中,这是一项重大突破。ServiceController
,看起来好像只有两个依赖项传递给ServiceController
,但是实际的数量是四个。扁平构造函数注入的一大好处是,它很清楚何时违反了单一职责原则。我有可能违反这种方法的任何SOLID原则吗?
是的,此设计违反了SOLID,因为至少它违反了接口隔离原则,该原则规定不应强迫客户端依赖于不使用的成员。
但是,在上面的示例中,尽管它不使用和属性,但是它ServiceController
被迫依赖于SolutionDomain
和PhaseDomain
属性。
这种设计也很可能导致违反“单一责任”原则,因为您传递给客户端的功能越多,它倾向于自己做的事情就越多,而不是依赖于系统的其他部分。
也有可能会导致违反Liskov替代原则(LSP),因为通常的趋势是,您在接口上定义的成员越多,则遵守LSP的难度就越大。通常,报头接口往往会导致LSP违规。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句