在我们的分层体系结构中,我正在设计一个名为AppHandover的BLL逻辑组件,并为此编写了基本的高级代码。我希望它遵循SOLID原则,松散耦合,采取关注点分离并且可以测试。
这是AppHandover应该做的
检查用户是否拥有应用程序。如果没有抛出错误
尽可能删除历史记录(即不再分配任何应用程序给用户)
将所有权转移到下一个实例
问题是,我走在正确的轨道上吗?以下示例看起来很可靠吗?
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");
}
}
}
最佳答案
关于上面概述的代码,我绝对认为您在正确的地方。我将设计进一步推进,并将TransferOwnership定义为附加接口。
按照这种方法,您的AppHandoverProcess将与客户端完全分离,并且行为将在服务配置中定义。
对TransferOwnership实施隔离将使您可以轻松地对实现接口的任何对象进行单元测试,而无需模拟AppHandoverProcess依赖项。
同样,任何AppHandoverProcess测试都应该是微不足道的,因为您唯一需要确保的是调用了服务或引发了异常。
希望这有意义
问候。