I am new to MVC and from what I understood so far, each web request is independent, and in the controller you usually create a new Model object and load it from some data source.
That's great when you work against a DB and load the model object you need.
But what if my data source is a live real-time streaming data, and when the user click on a button I need to give him the last data I got from the stream ?
The simplest example I can think of is stock data:
Assume that I have a data source object that connects to the exchange and subscribes to a live feed of stock prices, and, when the client click on a button "Get last quote", I need to return the latest price I got (note: I do NOT need to stream data to the client, only return a snapshot when he clicks, or every 5 seconds etc).
So what I'm looking to do is, when the application starts create my static data source object, have it connected to the exchange and start listening to the feed, on each price change it stores it on some local variable, and in my Controller all I need to do is to call to my static data source and ask for the last tick (and it will return the value of the local variable).
Hope my question is clear. I know, it might be simple, but I just don't know, what is the correct pattern for this in MVC. Any ideas?
I would create it from 4 parts:
background process/daemon which continuously pushes stock data to NoSQL database
main application, which serve the web site
javascript driver frontend app, either listening to open socket or requesting data each few seconds
data provider, which pulls information from NoSQL and handles the frontend requests
It would make sense to use some MVC-inspired patterns for main application and frontend (yes, two MVC triads). But the rest are just glorified transaction scripts.
Related
I need to load some data before the first request in ASP.Net Web API. For example, immediately after deploy the ASP.Net Web API application starts and load some data.
How is it possible?
One way would be to change the Application_Start method.
You can check this to see how it works : https://learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/configuring-aspnet-web-api
So, in the application start method, you do what you need, load the data that you want.
Now, assuming this data is supposed to be available to the application at all times, I would cache it.
Some ways of doing that can be explored here for example: How to manage cache in ASP.NET WebApi2?
I assume that this data that you want loaded is something which is not user specific which means that loading it in Application_Start, before any requests arrive will work just fine.
I am in the process of creating a simple application with Spring Mvc and thymeleaf and I am currently thinking of what functionality I want to implement but I don't know exactly how to do it.
Let's say I have a model class Person. Regularly I have a form and a controller where I am passing the new person object and persist it with JPA.
No problem there but what if I want to have a page that I give some of the person basics info and then hit the "next" button and give some additional information. Then hit "next" again, review the data and hit "save"?
You can do it by integrate Spring Webflow in your project.
Webflow is basically extensive part of WebMvc. Webflow has some configuration that, where you have to start and where you should go. If you have 5 page and you would like to all these data will put into database in one process then Webflow will help you. One more advantage is, you can add validation in particular pages and particular means you have five model and all these model will work in one flow.
Read more, https://projects.spring.io/spring-webflow/
I have not used Thymeleaf, but usually this kind of problem can be solved using some of the following methods or something similar:
1.) Save the unfinished data to database using the same schema or some other schema for this (or in session; in general sense, save it somewhere on server side). Problem with this is how to get rid of abandoned data where user has not moved to finish.
2.) Drag the data from page to page with request parameters. If the requests are of type POST then just in POST body, if they are type GET then as query parameters. Problem with this is it's not very clean.
3.) Don't do full page requests. Solve it with some front end solution using Javascript. Depending on the app it might or might not be possible.
4.) Do full page requests but still solve it in front end using local storage or session storage. Similar problems as with keeping the data in server side session.
I am currently designing an application that will have a few different pages, and each page will have components that update through AJAX. The layout is similar to the new Twitter design where 'Home', 'Discover', and 'Connect' are separate pages, but interacting within the page (such as clicking 'Followers' or 'Following') uses AJAX.
Since the design requires an initial page load with several components (in the context of Twitter: tweets, followers, following), each of which can be updated individually through AJAX, I thought it'd be best to have a default controller for serving pages, and other controllers with actions that, rather than serving full pages, strictly handle querying the database and returning JSON objects. This way, on initial page load several HMVC requests can be made to gather the data for each component, and AJAX calls can also be made to update each component individually.
My idea is to have a Controller_Default that handles serving pages. In the context of Twitter, Controller_Default would contain:
action_home()
action_connect()
action_discover()
I would then have other Controllers that don't deal with serving full pages, but rather components of pages. For instance, in the context of Twitter Controller_Tweet may have:
action_get()
which returns a JSON object containing tweets for a specific user. Action_home() could then make several HMVC requests to get the data for the several different components of the page (i.e. make requests to 'tweet/get', 'followers/get', 'following/get'). While on the page, however, AJAX calls could be made to the function specific controllers (i.e. 'tweet/get') to update the content.
My question: is this a good design? Does it make sense to have the pages served through a default controller, with page components served (in JSON format) through other function specific controllers?
If there is any confusion regarding the question please feel free to ask for clarification!
One of the strengths of the HMVC pattern is that employing this type of layered application doesn't lock you into a workflow that might be difficult to change later on.
From what you've indicated above, this would be perfectly acceptable as a way of serving content to a client; the default controller wraps sub-requests, which avoids multiple AJAX calls from the client to achieve the same goal.
Two suggestions I might make:
Ensure that your Twitter back-end requests are abstracted out and managed in a library to make the application DRY'er and easier to maintain.
Consider whether the default controller is making only the absolutely necessary calls on each request. Employ caching to avoid pulling infrequently changed data on every request (e.g., followers might only be updated every 30 seconds). This of course depends entirely on your application requirements, but if you get heavily loaded you could quickly find your Twitter API request limit being reached.
One final observation: if you do find the server is experiencing high load and Twitter API requests are taking a long time to return, consider provisioning another server and installing a copy of your application. You can then "point" sub-requests from the default gateway application to your second application server, which should help improve response times if the two servers are connected by a high-speed link.
I'm using (not programming) an application that sends my browser, using a technology called "lightstreamer" (which i have no clue about), data every second or so (I guess using AJAX?). these are constant changing stock values.
Now... is there any program/thing I can use to automatically fetch/sniff/whatever the raw data that my browser gets, so that for example i could later paste it to Excel and create charts?
Why not just copy the data from the browser window you might ask, and the reason is that the application always shows me only the last 20 values for a given stock, and i wish to automatically get, let's say, the last 1,000 values and throw it to Excel.
Thanks :)
PS I see that the app is written in asp id it matters.
There is no way that I know of to (easily) reconstruct sniffed Lightstreamer communications into tabular data. Lightstreamer pushes updates to the client using a hidden IFRAME, but those updates are efficient, but intended for consumption only by the Lightstreamer client code.
Developers using the Lightstreamer Javascript API can easily hook into update events if they wish to.
However as an application user, you are best off raising a change request with the application owner to add some form of Excel export functionality.
General question, when do people access their DAL in a prism application?
That is to say, if a module requires data do you query the DAL on the module load (I've currently been using OnImportsSatisfiedandINavigationAware.OnNavigatedTo` (passing in a parameter from the previous view).
Obviously I wouldn't want different modules to be tightly coupled but for an example where I have multiple views in a module would it be better from the UI responsiveness point of view to retrieve the data up front and pass it into the new view?
Anyone got any thoughts on this that they could share? Thanks.
In my current project we build the application this way that all view models asynchronouls query their data from one wcf service proxy after their own initialization. The proxy itself queries them from the server and caches it internally. Thus you have to think about a caching strategy.
But this leads to the following behaviour: The user interface is build up by the region manager. At the beginning it is empty. After a short time the data arrives from the server, the view models get their model, read the data from it, the data context of the view (which is the view model) is filled up and so the view is populated.
The answer to your question is: The view model queries the DAL (in my case the wcf service proxy) after it's creation in asynchronous way.