Passing messages from the application layer to the View - asp.net-mvc

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 .

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

Related

Get Controller Object and Controller Strong Params in Application Controller

Hello people. I'm creating a log process in my Rails 5 application, inside the application controller. I'm creating there because I want to call the log process inside many controllers with a before_save property. The log will save the changes that user performs in the form on edit view template. The problem is that I can't get the <ObjectController:> inside application controller. I've already got the instance variable from the controller, but I need the ObjectController too, because I have to get the strong parameters from controller object. The strong parameters holds all data that user inserted on input fields.
This is what I've done already:
app/controllers/application controller
def log
#controlr = instance_variable_get("##{controller_name.singularize}") #get the edited object
attribs = #controlr.attribute_names #get object table column names
edited_data = controlr_params #stuck here!
ctrlr = #controlr.attributes #retrive object data from db
...
##compare the edited_data with the actual data from db and check if something was changed
end
So, I need to obtain the Controller Object to access the strong parameters in order to compare if user edited any data. I'm not sure if this is the best way/practice to do this. If there is a better way, I'd like to know. But I need to call this process in a great number of controllers that require a data log.
Thanks for you time and sorry any bad english..
If params method won't help you to achieve your goal (but it's worth to try) you can always access current instance of controller object by calling self in context of any instance method or action.
To test you can put byebug in any action, call that action in browser with additional parameters and type self in console.
For example, in controller:
class UsersController < ApplicationController
def show
byebug
end
end
in browser:
localhost:3000/?some_param=1234&another_param=testing
There will be a lot of useful stuff in there, like self.instance_variables => [.... :#_request, ... :#_params].
Also request method contain all info about current request including parameters.
Hope that'll help.

Grails pass object to the view and back again

I have some data that I need to persist through multiple actions within my Grails app. Due to the nature of the data, I would prefer not to store the data in the session. Here is an example of what I would like to do.
class MyController{
def index(){
MyObject object = MyObject.new(params.first, params.second, params.third)
[gspObject:object]
}
def process(){
MyObject object = params.gspObject
//continue from here
}
}
In my GSP if I do
<g:form action="process" params="[gspObject:gspObject]">
Then I get the error
Cannot cast object 'net.package.MyObject#699c14d8' with class 'java.lang.String' to class 'net.package.MyObject'
My question is, If I want to get the object back that I sent to the gsp, how can I get that? Is there some kind of scope that I can save the object in that would be a little safer then session? Is there a way to pass the object into the page itself and pass it back in the next request?
Grails has many layers, but at the bottom you have plain old HTTP just like in any web app. It's a stateless protocol, and you send a text or binary response, and receive text or text + binary requests. But you can't expect to be able to send an arbitrary object to a web browser in HTML and receive it back again in the same state as when you sent it - where is this Java/Groovy JVM object going to be stored in the browser?
You have basically two options. One is to store it at the server, which is less work because it remains as the same object the whole time. The session is a good location because it's coupled to the user, is created on-demand and can automatically time out and be removed, etc. The other is to do what you're trying to do - send it to the client and receive it back - but you are going to have to serialize it from an object (which could be a complex object containing arbitrarily many other objects) and deserialize it from the format you used on the client back into Java/Groovy objects.
JSON is a good option for serialization/marshalling. You could store the stringified object in a hidden form element if your page uses a form, or in a querystring arg if you click a link from this page to the next in the workflow. Don't send all of the object's data though, only what you need to rebuild it. Anything that's available in the database should be referenced by id and reloaded.
Something like
[gspObject: object as JSON]
or
[gspObject: [first: object.first, first: object.firstsecond, ...] as JSON]
will get it in the correct format for sending, and then you can parse the JSON from the request to reinstantiate the instance.

How does flash object map work inside grails framework

Flash, which is a temporary storage map for one request.
I am wondering how this is implemented on the grails core framework
In particular I'm interested in the class(es) responsible for putting the flash map in the request and then taking it out once the request has finessed processing.
Flash is actually a temporary storage map for the present request and the next request only. It won't retain the entries after the next request unless entries are repopulated in the next request (which would be current in future). Here is how it works in Grails:
FlashScope interface which extends Map itself has two methods next() and getNow() is implemented by GrailsFlashScope. All of which can be found in grails-web-mvc.
GrailsFlasScope mainly maintains two concurrent HasMap (one for current request and second for the next request) to hold the entries. It implements next() from FlashScope to do the cleaning and "restricting-to-next-request-only" part as:
a. clear current
b. make next as current
c. clear next
Next thing to focus will be GrailsWebRequestFilter (implements OncePerRequestFilter) which makes sure that there is always a single execution of request per dispatch.
All http servlet requests are filtered by GrailsWebRequestFilter. This filter sets the flash scope to next so that every time the latest and valid info is retrieved.
Now the question is how does FlashScope reconciles current and next map? Well, that is why FlashScope is extended from Map. FlashScope overrides get(key) from map to reconcile both the maps by making sure values are retrieved from next map otherwise switch to current map.
How is flash available to controllers by default? All controller inherit ControllersApi which inherits CommonWebApi.
I hope you get what you were looking for..
If you print the class of the object:
class MyController {
def index() {
println flash.getClass().name
}
}
You will see that's org.codehaus.groovy.grails.web.servlet.GrailsFlashScope. If you look at the code, there are two ConcurrentHashMap's: one for the current request and another to the next request.
To make it available, the instance is stored in the session (see registerWithSessionIfNecessary).

troubleshooting a NullPointerException in grails

preface note: I'm just starting to learn Grails, so I'm sure there are many other problems and room for optimization.
I've got two domains, a parent (Collection) and child (Event), in a one-to-many mapping. I'm trying to code an integration test for the deletion of children. Prior to the code in question, I've successfully created a parent and three children. The point where I'm having problems is getting a single child in preparation to delete it. The first line of my sample code is only there because of my rudimentary attempt to troubleshoot.
// lines 95-100 of my EventIntegrationTests.groovy file
// delete a single event
assertEquals("2nd Event", event2.title) // passes
def foundEvent = Event.get(event2.id) // no apparent problems
assertEquals("2nd Event", foundEvent.title) // FAILS (line #98)
foundEvent.delete()
assertFalse Event.exists(foundEvent.id)
The error message I'm getting is:
Cannot get property 'title' on null object
java.lang.NullPointerException: Cannot get property 'title' on null object
at edu.learninggrails.EventIntegrationTests.testEventsDelete(EventIntegrationTests.groovy:98)
What should my next troubleshooting steps be? (Since the first assertEquals passes, event2 is clearly not null, so at this point I have no idea how to troubleshoot the failure of the second assertEquals.)
This is not evident from the code: did you persist event2 by calling save()? Get will try to retrieve it from the persistent storage (the in-memory database for example) and if the event wasn't saved, the retrieved instance will be null.
If you did save it, did the save go through OK? Calling event.save() will return false if there was something wrong while saving the item (like a validation error). Lastly, you might try calling event.save(flush:true) in case the Hibernate session doesn't handle this case as you might expect (I'm not entirely sure about this one, but it can't hurt to try).
Try to print or inspect the event2.id on line 97 and check if you actually have an id, if so check if you actually get an Event object on line 97.
I dont think you saved the parent and its children successfully. after you save, you should make sure that every object that was persisted has a non null id, in your test.
What you are seeing is you created the event2 with a title, but didnt save it. It passes the first assertion because you created it. When you do the get, null is returned because your save failed.
in general for DAO integration tests i do the following
Setup -- create all objects Ill use in the test.
Save -- assert that all ids on saved objects are NOT null.
Clear the hibernate session -- this is important because if you don't do it, objects can be in the session from the previous operations. In your real world scenario, you are probably going to start with a find, i.e. an empty session. In other words, you are not going to start with anything in the session. If you are you need to adjust this rule so that the session in the test, when you start the actual testing part, is the same as the session of the code in the wild
Load the objects on which you want to operate and do what you need to do.

should_receive in RSpec

As far as I know, should_receive is applied only to mock objects. What I want is to check, if a certain Class (not object) received a certain message, like:
User.should_receive(:all).once
How do I do that?
UPD. Commonly, writing test for models and controllers we can write User.should_receive(:smth).once. But in my case I'm testing an arbitrary class from the lib folder, and somehow I always receive the following message:
<User( [fields] ) (class)> expected :all with (no args) once, but received it 0 times>
Any ideas on why is that so? A test somehow sees the User class, but can't check if it receives a message. Of course I've ten times checked that the User is actually getting a message.
Easy:
User.should_receive(:all).once
What I want is to check, if a certain Class (not object) received a certain message
A class is an object!

Resources