मैं डोमेन संचालित डिज़ाइन अवधारणाओं के लिए बिल्कुल नया हूं और डोमेन लॉजिक के लिए कमांड और कमांड हैंडलर के साथ कमांड बस का उपयोग करते समय एक एपीआई में उचित प्रतिक्रियाओं को वापस करने में समस्या आई है।कमांड बस से डेटा कैसे प्राप्त करें?
मान लें कि हम एक डोमेन संचालित डिजाइन दृष्टिकोण के साथ एक एप्लिकेशन बना रहे हैं। हमारे पास बैक एंड एंड फ्रंट एंड पार्ट है। बैक एंड में हमारे सभी डोमेन लॉजिक एक खुला एपीआई के साथ हैं। फ्रंट एंड एप्लिकेशन को अनुरोध करने के लिए एपीआई का उपयोग करता है।
हम कमांड बस में मैप किए गए कमांड और कमांड हैंडलर के साथ हमारे डोमेन तर्क बना रहे हैं। हमारी डोमेन निर्देशिका के तहत हमारे पास CreatePostCommand नामक एक पोस्ट संसाधन बनाने के लिए एक आदेश है। यह कमांड बस के माध्यम से अपने हैंडलर CreatePostCommandHandler पर मैप किया गया है।
final class CreatePostCommand
{
private $title;
private $content;
public function __construct(string $title, string $content)
{
$this->title = $title;
$this->content= $content;
}
public function getTitle() : string
{
return $this->title;
}
public function getContent() : string
{
return $this->content;
}
}
final class CreatePostCommandHandler
{
private $postRepository;
public function __construct(PostRepository $postRepository)
{
$this->postRepository = $postRepository;
}
public function handle(Command $command)
{
$post = new Post($command->getTitle(), $command->getContent());
$this->postRepository->save($post);
}
}
हमारे एपीआई में हमारे पास एक पोस्ट बनाने के लिए एक अंतराल है। यह हमारी अनुप्रयोग निर्देशिका के तहत PostController में createPost विधि को रूट किया गया है।
final class PostController
{
private $commandBus;
public function __construct(CommandBus $commandBus)
{
$this->commandBus = $commandBus;
}
public function createPost($req, $resp)
{
$command = new CreatePostCommand($command->getTitle(), $command->getContent());
$this->commandBus->handle($command);
// How do we get the data of our newly created post to the response here?
return $resp;
}
}
अब हमारे createPost विधि में हम अपने प्रतिक्रिया ऑब्जेक्ट में हमारे नव निर्मित पोस्ट के डेटा लौटाने के लिए तो हमारे सामने के छोर आवेदन नव निर्मित संसाधन के बारे में पता कर सकते हैं चाहता हूँ। यह परेशानी है क्योंकि हम जानते हैं कि परिभाषा के अनुसार कमांड बस को किसी भी डेटा को वापस नहीं करना चाहिए। तो अब हम एक भ्रमित स्थिति में फंस गए हैं जहां हम नहीं जानते कि प्रतिक्रिया ऑब्जेक्ट में हमारी नई पोस्ट कैसे जोड़ें।
मुझे यकीन है कि यहां से इस समस्या के साथ आगे बढ़ने के लिए कैसे नहीं हूँ, कई सवाल मन में आते हैं:
- वहाँ जवाब में पोस्ट की डेटा लौटाने के लिए एक सुंदर तरीका है?
- क्या मैं कमांड/कमांड हैंडलर/कमांडबस पैटर्न को गलत तरीके से कार्यान्वित कर रहा हूं?
- क्या यह कमान/कमांडहैंडलर/कमांडबस पैटर्न के लिए बस गलत उपयोग केस है?
संभावित डुप्लिकेट [सीक्यूआरएस कमांड के लिए एपीआई से क्या वापस किया जाना चाहिए?] (Http://stackoverflow.com/questions/29916468/what-should-be-returned-from-the-api-for-cqrs- आदेश) – guillaume31