在与您的问题相关的讨论中,鲍勃叔叔在他的清洁架构中解释了演示者的目的:
鉴于此代码示例:
namespace Some\Controller;
class UserController extends Controller {
public function registerAction() {
// Build the Request object
$request = new RegisterRequest();
$request->name = $this->getRequest()->get('username');
$request->pass = $this->getRequest()->get('password');
// Build the Interactor
$usecase = new RegisterUser();
// Execute the Interactors method and retrieve the response
$response = $usecase->register($request);
// Pass the result to the view
$this->render(
'/user/registration/template.html.twig',
array('id' => $response->getId()
);
}
}
鲍勃叔叔说:
"演示者的目的是将用例与 UI 的格式分离。 在您的示例中,$response 变量由交互器创建,但由视图使用。这将交互器与视图耦合。例如, 假设 $response 对象中的一个字段是日期。该字段将是一个二进制日期对象,可以以许多不同的日期格式呈现。需要一个非常具体的日期格式,可能是 DD/MM/YYYY。创建格式是谁的责任?如果交互者创建了该格式,那么它对 View 了解太多。但如果视图采用二进制日期对象,那么它对交互器了解太多。
“演示者的工作是采取来自响应对象的数据并将其格式化为视图。 视图和交互者都不知道彼此的格式。“
——鲍勃叔叔
鉴于鲍勃叔叔的回答,我认为我们是否执行选项#1(让交互者使用演示者)并不重要......
class UseCase
{
private Presenter presenter;
private Repository repository;
public UseCase(Repository repository, Presenter presenter)
{
this.presenter = presenter;
this.repository = repository;
}
public void Execute(Request request)
{
...
Response response = new Response() {...}
this.presenter.Show(response);
}
}
...或者我们执行选项#2(让交互者返回响应,在控制器内创建一个演示者,然后将响应传递给演示者)...
class Controller
{
public void ExecuteUseCase(Data data)
{
Request request = ...
UseCase useCase = new UseCase(repository);
Response response = useCase.Execute(request);
Presenter presenter = new Presenter();
presenter.Show(response);
}
}
就个人而言,我更喜欢选项#1 ,因为我希望能够控制何时interactor
显示数据和错误消息,如下例所示:
class UseCase
{
private Presenter presenter;
private Repository repository;
public UseCase(Repository repository, Presenter presenter)
{
this.presenter = presenter;
this.repository = repository;
}
public void Execute(Request request)
{
if (<invalid request>)
{
this.presenter.ShowError("...");
return;
}
if (<there is another error>)
{
this.presenter.ShowError("another error...");
return;
}
...
Response response = new Response() {...}
this.presenter.Show(response);
}
}
...我希望能够在交互器内部而不是外部执行if/else
与演示相关的这些操作。interactor
另一方面,如果我们执行选项#2,我们将不得不将错误消息存储在对象中,将该对象从 中response
返回到 中,然后解析对象...response
interactor
controller
controller
response
class UseCase
{
public Response Execute(Request request)
{
Response response = new Response();
if (<invalid request>)
{
response.AddError("...");
}
if (<there is another error>)
{
response.AddError("another error...");
}
if (response.HasNoErrors)
{
response.Whatever = ...
}
...
return response;
}
}
class Controller
{
private UseCase useCase;
public Controller(UseCase useCase)
{
this.useCase = useCase;
}
public void ExecuteUseCase(Data data)
{
Request request = new Request()
{
Whatever = data.whatever,
};
Response response = useCase.Execute(request);
Presenter presenter = new Presenter();
if (response.ErrorMessages.Count > 0)
{
if (response.ErrorMessages.Contains(<invalid request>))
{
presenter.ShowError("...");
}
else if (response.ErrorMessages.Contains("another error")
{
presenter.ShowError("another error...");
}
}
else
{
presenter.Show(response);
}
}
}
我不喜欢response
在.controller
interactor
controller
此外,如果我们稍后决定重用我们interactor
使用控制台呈现数据,例如,我们必须记住将所有这些数据复制粘贴if/else
到controller
我们的控制台应用程序中。
// in the controller for our console app
if (response.ErrorMessages.Count > 0)
{
if (response.ErrorMessages.Contains(<invalid request>))
{
presenterForConsole.ShowError("...");
}
else if (response.ErrorMessages.Contains("another error")
{
presenterForConsole.ShowError("another error...");
}
}
else
{
presenterForConsole.Present(response);
}
如果我们使用选项#1,我们将if/else
只在一个地方拥有它:interactor
.
如果您使用的是 ASP.NET MVC(或其他类似的 MVC 框架),选项 #2 是更简单的方法。
但是我们仍然可以在那种环境中执行选项#1。这是在 ASP.NET MVC 中执行选项 #1 的示例:
(请注意,我们需要public IActionResult Result
在我们的 ASP.NET MVC 应用程序的演示者中拥有)
class UseCase
{
private Repository repository;
public UseCase(Repository repository)
{
this.repository = repository;
}
public void Execute(Request request, Presenter presenter)
{
if (<invalid request>)
{
this.presenter.ShowError("...");
return;
}
if (<there is another error>)
{
this.presenter.ShowError("another error...");
return;
}
...
Response response = new Response() {...}
this.presenter.Show(response);
}
}
// controller for ASP.NET app
class AspNetController
{
private UseCase useCase;
public AspNetController(UseCase useCase)
{
this.useCase = useCase;
}
[HttpPost("dosomething")]
public void ExecuteUseCase(Data data)
{
Request request = new Request()
{
Whatever = data.whatever,
};
var presenter = new AspNetPresenter();
useCase.Execute(request, presenter);
return presenter.Result;
}
}
// presenter for ASP.NET app
public class AspNetPresenter
{
public IActionResult Result { get; private set; }
public AspNetPresenter(...)
{
}
public async void Show(Response response)
{
Result = new OkObjectResult(new { });
}
public void ShowError(string errorMessage)
{
Result = new BadRequestObjectResult(errorMessage);
}
}
(请注意,我们需要public IActionResult Result
在我们的 ASP.NET MVC 应用程序的演示者中拥有)
如果我们决定为控制台创建另一个应用程序,我们可以重用UseCase
上面的内容Controller
并Presenter
为控制台创建 and :
// controller for console app
class ConsoleController
{
public void ExecuteUseCase(Data data)
{
Request request = new Request()
{
Whatever = data.whatever,
};
var presenter = new ConsolePresenter();
useCase.Execute(request, presenter);
}
}
// presenter for console app
public class ConsolePresenter
{
public ConsolePresenter(...)
{
}
public async void Show(Response response)
{
// write response to console
}
public void ShowError(string errorMessage)
{
Console.WriteLine("Error: " + errorMessage);
}
}
(请注意,我们public IActionResult Result
的控制台应用程序的演示者中没有)