在与您的问题有关的讨论中,鲍伯叔叔在其“干净的体系结构”中解释了演示者的目的:
给定此代码示例:
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。谁负责创建格式?如果交互器创建的格式,那么它知道太多关于观。但是,如果认为需要二进制日期对象,然后它知道太多关于交互件。
“主持人的工作就是拿响应对象中的数据并将其格式化为视图。 视图和交互器都不了解彼此的格式。“
-鲍勃叔叔
(更新:2019年5月31日)
鉴于鲍勃叔叔的回答,我认为我们是否执行选项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,我们将不得不存储错误消息(S)的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
:
// 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
控制台应用程序的演示者)