持久化无知 (PI)是实现高效率功能测试的关键指标。我相信这应适用于一般的基建用途。然而,在面向的体系结构 (WOA)中,特别是在REST API中,似乎很少遵循这一准则。因为REST似乎是一种以数据为中心的体系结构,我不明白为什么PI不以一种更流行的方式应用于WOA,以防止系统和昂贵的端到端测试。
是否有任何一种架构或实践可以轻松地将PI引入REST体系结构?根据您的经验,您是否尝试在REST体系结构中遵循PI原则?如果没有,为什么?
发布于 2016-06-12 14:27:56
这都是一个层次和抽象的问题。没有必要让REST端点知道数据隐藏在何处或如何持久化。持久性机制可以在运行时注入或交换。端点应该只知道将API调用参数转换为下一层所期望的任何参数(如果需要的话)。诚然,许多REST实现使用持久化知识加载端点,但这并不局限于RESTful API。
实心中的一点D可以用来将PI引入到RESTful端点。
使用WebAPI示例,Controller (端点)只知道下一层的接口。为了测试目的,可以对该层进行模拟,以将测试隔离到API本身。如果需要更复杂的逻辑或对多个存储库的访问,下一层可以是持久性层或服务层。
public class ValuesController : ApiController
{
//controller only knows about the interface to the next layer down
private readonly IServiceOrRepositoryLayer _serviceOrRepo;
//persistence or service layer injected into the constructor
//useful for testing
//could also inject a factory if more flexibility needed at runtime
public ValuesController(IServiceOrRepositoryLayer serviceOrRepo)
{
_serviceOrRepo = serviceOrRepo;
}
// GET api/values
public IEnumerable<SomePOCO> Get()
{
return _serviceOrRepo.ListAll();
}
// GET api/values/5
public SomePOCO Get(int id)
{
return _serviceOrRepo.Get(id);
}
// POST api/values
public void Post(SomePOCO value)
{
//can either pass the value directly or transform it here
//to what the next layer needs
_serviceOrRepo.Create(value);
}
// PUT api/values/5
public void Put(int id, SomePOCO value)
{
_serviceOrRepo.Update(value, id);
}
// DELETE api/values/5
public void Delete(int id)
{
_serviceOrRepo.Delete(id);
}
}https://stackoverflow.com/questions/37766560
复制相似问题