将消息从应用程序层传递到View

时间:2023-02-07 04:02:33

Let's say I have an AddProductToCartTask with a method Execute().

假设我有一个带有Execute()方法的AddProductToCartTask。

Now, the task is being called from a Controller. In the Execute method , there is a check that if not met - the action is not performed.

现在,正在从Controller调用该任务。在Execute方法中,检查如果不满足 - 则不执行该操作。

Let's say the message returned would be: "You do not have enough bonus to buy this product".

假设返回的信息是:“你没有足够的奖金购买这个产品”。

I thought about launching an event when the domain validation fails - but that would mean that in the Controller I have to have all kinds of class variables that need checking in the action (to determine if I need to set an error message, or i need to redirect .. etc)

我想在域验证失败时启动事件 - 但这意味着在Controller中我必须拥有需要检查操作的各种类变量(以确定是否需要设置错误消息,或者我需要重定向..等)

Instead of this I could have a method on the task : GetErrorMessages(). If empty return the JSON object if not empty return the Message. Or the method could return an enum that would tell if i need to redirect or set a message or return the object ....

而不是这个,我可以在任务上有一个方法:GetErrorMessages()。如果为空则返回JSON对象(如果不为空)返回Message。或者该方法可以返回一个枚举,告诉我是否需要重定向或设置消息或返回对象....

I'm not sure which road to take. Any input would be appreciated. How do you bubble up messages from your domain layer ?

我不确定要走哪条路。任何输入将不胜感激。如何从域层冒泡消息?

Edit: this is mainly in an AJAX context. Even though I'm not sure it matters as it's an action that it's getting called from somewhere .

编辑:这主要是在AJAX上下文中。即使我不确定它是否重要,因为它是一个从某个地方调用它的动作。

1 个解决方案

#1


I might be misunderstanding your request, but it seems to me like you want a central messages functionality, rather than something specific to the task object. If you leave it in your task, then the task must be kept in scope and "alive" until the AJAX request.

我可能会误解你的请求,但在我看来,你想要一个*消息功能,而不是特定于任务对象的东西。如果将其保留在任务中,那么任务必须保持在范围内并且“活动”直到AJAX请求。

I do something similar, though directly from the Controller.

我做了类似的事情,虽然直接来自Controller。

I have a static class called Messages. It has AddMessage(), GetLastMessage(), and GetAllMessages() methods. Each one, when first called, will check the user's session variable and, if nothing is found, creates and saves a Queue<string>() object. The methods are basically just an interface to the Queue. The Queue is nice because it handles push/pop which automatically removed "viewed" messages.

我有一个名为Messages的静态类。它具有AddMessage(),GetLastMessage()和GetAllMessages()方法。每次调用时,每个都将检查用户的会话变量,如果没有找到,则创建并保存Queue ()对象。这些方法基本上只是Queue的接口。 Queue很不错,因为它处理push / pop,自动删除“已查看”的消息。

My controller does:

我的控制器:

Messages.AddMessage("Product Saved");

You could potentially do:

你可能会这样做:

Messages.AddMessage(task...GetErrorMessages());

Then, from my View, I have an html helper that checks how many error messages there are and, if any, creates a <ul> with each message as a <li>.

然后,从我的View中,我有一个html帮助程序,它检查有多少错误消息,如果有的话,创建一个

    ,每条消息都作为

You could just as easily have a GetMessages() controller that returns any messages as a JSON object.

您可以轻松拥有一个GetMessages()控制器,它将任何消息作为JSON对象返回。

James

#1


I might be misunderstanding your request, but it seems to me like you want a central messages functionality, rather than something specific to the task object. If you leave it in your task, then the task must be kept in scope and "alive" until the AJAX request.

我可能会误解你的请求,但在我看来,你想要一个*消息功能,而不是特定于任务对象的东西。如果将其保留在任务中,那么任务必须保持在范围内并且“活动”直到AJAX请求。

I do something similar, though directly from the Controller.

我做了类似的事情,虽然直接来自Controller。

I have a static class called Messages. It has AddMessage(), GetLastMessage(), and GetAllMessages() methods. Each one, when first called, will check the user's session variable and, if nothing is found, creates and saves a Queue<string>() object. The methods are basically just an interface to the Queue. The Queue is nice because it handles push/pop which automatically removed "viewed" messages.

我有一个名为Messages的静态类。它具有AddMessage(),GetLastMessage()和GetAllMessages()方法。每次调用时,每个都将检查用户的会话变量,如果没有找到,则创建并保存Queue ()对象。这些方法基本上只是Queue的接口。 Queue很不错,因为它处理push / pop,自动删除“已查看”的消息。

My controller does:

我的控制器:

Messages.AddMessage("Product Saved");

You could potentially do:

你可能会这样做:

Messages.AddMessage(task...GetErrorMessages());

Then, from my View, I have an html helper that checks how many error messages there are and, if any, creates a <ul> with each message as a <li>.

然后,从我的View中,我有一个html帮助程序,它检查有多少错误消息,如果有的话,创建一个

    ,每条消息都作为

You could just as easily have a GetMessages() controller that returns any messages as a JSON object.

您可以轻松拥有一个GetMessages()控制器,它将任何消息作为JSON对象返回。

James