在我们的分层架构中,我正在设计一个名为 AppHandover 的BLL逻辑组件,并为此编写了基本的高级代码。我希望它遵循SOLID原则并且松散耦合,采用分离关注和可测试。
这是AppHandover应该做的事情
问题是,我是否在正确的轨道上并且下面的示例看起来是SOLID吗?
public interface ITransferOwnership
{
void TransferOwnership(string userId, string appId, TransferDirection transferDirection);
}
public interface IOwnershipVerification
{
bool UserOwnsApp(string userId, int budgetId, string appId);
}
public interface IPreserveHistoryCheck
{
bool ShouldDeleteTemporaryBudgetData(string userId, int budgetId);
}
public interface IRemoveHistory
{
void DeleteTemporaryBudgetData(string userId, int budgetId);
}
移交流程实施
public class AppHandoverProcess : KonstruktDbContext, ITransferOwnership
{
private IOwnershipVerification _ownerShipVerification;
private IPreserveHistoryCheck _preserveHistory;
private IRemoveHistory _removeHistory;
private ITransferOwnerShip _transferOwnership;
public AppHandoverProcess()
{
}
public AppHandoverProcess(IOwnershipVerification ownerShipVerification,
IPreserveHistoryCheck preserveHistory,
IRemoveHistory removeHistory)
{
_ownerShipVerification = ownerShipVerification;
_preserveHistory = preserveHistory;
_removeHistory = removeHistory;
}
public void PerformAppHandover(string userId, string appId, int budgetId)
{
if (_ownerShipVerification.UserOwnsApp(userId,budgetId,appId)) {
if (_preserveHistory.ShouldDeleteTemporaryBudgetData(userId, budgetId))
{
_removeHistory.DeleteTemporaryBudgetData(userId, budgetId);
}
//handover logic here..
_transferOwnership.TransferOwnership(userId, appId, TransferDirection.Forward);
}
else
{
throw new Exception("AppHandover: User does not own app, data cannot be handed over");
}
}
}
答案 0 :(得分:4)
关于您在上面列出的代码,我绝对认为您走在了正确的轨道上。我会进一步推动设计并将 TransferOwnership 定义为附加接口。
按照这种方法,您的 AppHandoverProcess 与其客户端完全分离,行为将在服务配置中定义。
为 TransferOwnership 实施隔离将允许您轻松地对实现该接口的任何对象进行UnitTest,而无需模拟 AppHandoverProcess 依赖项。
此外,任何 AppHandoverProcess 测试都应该是微不足道的,因为您需要确保调用您的服务或抛出异常。
希望这有意义,
问候。
答案 1 :(得分:1)
我会将KonstruktDbContext作为注入依赖项。 AppHandoverprocess不应该继承它,因为它看起来是一个不同的责任。