什么是处理 ApiControllers 结果的好模式?
What is a good pattern for handling results from ApiControllers?
我有一个 ApiController。
<HttpGet>
Public Function Find(ByVal planId As Integer) As IHttpActionResult
Dim results As IHttpActionResult
Try
Dim model As PlanModel = plansDAL.GetPlan(planId)
If model IsNot Nothing Then
results = BadRequest(String.Format("Plan {0} was not found.", planId))
Else
results = Ok(model)
End If
Catch ex As Exception
results = BadRequest(String.Format("Plan {0} was not found.", planId))
End Try
Return results
End Function
我不想有这样的嵌套语句。
我想知道在控制器操作 fails/redirect/finished 插入时 return 获得正确结果的更好模式。所以控制器可能 return 200 ok and content,或者它可能 return 404 not found.
我有一个尝试的想法,但我宁愿使用一些既定的模式。我的想法是添加一个 Response 类型的私有成员。然后我可以在我的控制器中更新该对象并 return 它最后一次且仅一次。
但是,我不确定如何做到不给开发人员造成负担,让他们每次都正确地制作响应。
我认为这个 article on mvc4 提供了我正在寻找的模式。
它似乎简化了方法,并提供了一种机制来执行具有明确期望的单元测试。它还确保未处理的异常冒泡并 return YSOD,直到它们也被处理。可能,它们可以在全局错误模块中处理。
All code below was written without an IDE, mistakes in syntax probably exist
<HttpGet>
Public Function Find(ByVal planId As Integer) As IHttpActionResult
Dim model As PlanModel = plansDAL.GetPlan(planId)
If model is Nothing Then
throw new HttpResponseException(HttpStatusCode.NotFound)
End If
Return Ok(model)
End Function
如果我现在要写单元测试,是这样的。
<TestMethod>
public sub Should_Return_Ok_When_Plan_Is_Found()
dim mockedService as new Mock(Of plansDAL)
dim mockPlan as new PlanModel 'set this up however to be the expected values.
dim controller as new PlanController()
mockedService.setup(sub(s) s.GetPlan(it.isAny(Of Integer))).returns(mockPlan)
dim result as IHttpActionResult = controller.find(1)
Assert.areEqual(mockPlan,result.contents)
end sub
我有一个 ApiController。
<HttpGet>
Public Function Find(ByVal planId As Integer) As IHttpActionResult
Dim results As IHttpActionResult
Try
Dim model As PlanModel = plansDAL.GetPlan(planId)
If model IsNot Nothing Then
results = BadRequest(String.Format("Plan {0} was not found.", planId))
Else
results = Ok(model)
End If
Catch ex As Exception
results = BadRequest(String.Format("Plan {0} was not found.", planId))
End Try
Return results
End Function
我不想有这样的嵌套语句。
我想知道在控制器操作 fails/redirect/finished 插入时 return 获得正确结果的更好模式。所以控制器可能 return 200 ok and content,或者它可能 return 404 not found.
我有一个尝试的想法,但我宁愿使用一些既定的模式。我的想法是添加一个 Response 类型的私有成员。然后我可以在我的控制器中更新该对象并 return 它最后一次且仅一次。
但是,我不确定如何做到不给开发人员造成负担,让他们每次都正确地制作响应。
我认为这个 article on mvc4 提供了我正在寻找的模式。 它似乎简化了方法,并提供了一种机制来执行具有明确期望的单元测试。它还确保未处理的异常冒泡并 return YSOD,直到它们也被处理。可能,它们可以在全局错误模块中处理。
All code below was written without an IDE, mistakes in syntax probably exist
<HttpGet>
Public Function Find(ByVal planId As Integer) As IHttpActionResult
Dim model As PlanModel = plansDAL.GetPlan(planId)
If model is Nothing Then
throw new HttpResponseException(HttpStatusCode.NotFound)
End If
Return Ok(model)
End Function
如果我现在要写单元测试,是这样的。
<TestMethod>
public sub Should_Return_Ok_When_Plan_Is_Found()
dim mockedService as new Mock(Of plansDAL)
dim mockPlan as new PlanModel 'set this up however to be the expected values.
dim controller as new PlanController()
mockedService.setup(sub(s) s.GetPlan(it.isAny(Of Integer))).returns(mockPlan)
dim result as IHttpActionResult = controller.find(1)
Assert.areEqual(mockPlan,result.contents)
end sub