views:

68

answers:

1

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

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.

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)

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 ....

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 .

A: 

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.

I do something similar, though directly from the 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.

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>.

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

James

James S