What benefits do I get from using ODataModel vs. JSONModel? - sapui5

I'm reading data from HANA using a JSONModel and simply passing in the URL to the source and retrieving it as in the following:
var data = new sap.ui.model.json.JSONModel(urlPath);
Then I can bind it to my view: this.getView().setModel(data);
I have also seen the following way of doing it, where an ODataModel is created and then the JSONModel is created from the data.
var oModel = new sap.ui.model.odata.ODataModel(urlPath);
oModelJson = new sap.ui.model.json.JSONModel();
oModel.read("/Items",
null,
["$filter=ImItems eq 'imputParameter'"],
null,
function(oData, oResponse) {
oModelJson.setData(oData);
},
null
);
What difference is there in creating the ODataModel first than creating the JSONModel at once. So assuming I'm getting back from the database about 5,000 data points, which approach should I use, or would there be no difference?

JSONModel is a Client model to get the data and set data to the view for JSON format.
ODataModel is a model implementation for OData protocol.
This allows CRUD operations on the OData entities. JSONModel doesn't support Create/Update/Delete/Batch operations.
So coming to your scenario, I would suggest to use ODataModel always to do CRUD operations (inclusive of read). Then can use JSON model to bind the data to view.
Note that it's better to have one ODataModel per app and multiple JSONModels bound to views.
Consider using ODataModel V2 and since you have mentioned that you are dealing with 5K data points, if you don't all the data in the UI. Use setSizeLimit to make sure you have set proper upper bound.

Both models can be used without conflict. In fact, most applications will use both.
You want to use the OData model to send/retrieve data from the server. The OData model will handle building the URLs for you. For instance, if you want to filter, sort, or use paging in your data without the OData model, you will need to build the URL yourself.
yourUrl.com/EntitySet?$filter eq Property1='Value'&$sort= ..... &top=... etc.
This will be difficult without the OData model, and makes the application more difficult to maintain and debug. Let the OData model do that for you:
ODataModel.read("/EntitySet, {
filters: [new Filter("Property1", "EQ", "Value")]
});
The greatest benefit of the OData model in my opinion, though, is binding directly from the XML views.
<List items="{/EntitySet}">
<items>
<StandardListItem title="{objectTitle}"/>
</items>
</List>
This will automatically call the backend, retrieve the data from the entity set, and bind it to the list. No need to construct any URLs, make any calls, etc.
Using a JSON model to retrieve the data from an OData service will only make things more difficult than they have to be.
But... that being said... the JSON model is a very powerful tool. You can use it to store configuration data or any data you want to hold in the UI and manipulate. You can use the JSON model as sort of a mini-database in your application that can pass data globally across your application.
To summarize, you should use the OData model to get/send data. You should use the JSON model for local data storage. There will not be conflicts trying to use both.

One major difference between both of them is:
A lot of controls in SAPUI5 for instance, smarttable, bind automatically to the odata entities, meaning it dynamically creates the columns and the tuples based on the Odata metadata XML file. In this scenario, you cannot use a JSON Model.
IMHO, I would go with OData because of this "automatic binding" that a lot of components SAPUI5 have. But, I also ran into scenarios when the OData entities were not structured well, meaning the "automatic binding" that some SAP UI components had, did not work as expected.
In those scenarios, I had to get the JSON out of the OData, created/destroyed a few properties and then I did the bind to the mentioned SAP UI component.

Related

Restful API .Net Core : Static Lookup Table Management. Separate Controller for each table or One generic

We need to manage static lookup tables with API, all tables have same structure so creating separate controller for each table or One generic Controller for all end points is better options?
Also We are getting all Lookup Data in one API Call with the Model for each Table in Response Model, so when we need to get the individual data should we create separate endpoint or we can manage this by providing a query parameter "type" to populate only specified Model. I was just wondering that can we use query parameter for this functionality? Any industry standard for this?

Using several JSONModel and XMLModel towards one component

For a openui5 component I need to bind multiple models and is investigating what the best way to do this is.
The models are both JSONModel and XMLModel.
The background is that I want provide the user choice of multiple source to use for the custom openui5-spitz-reader component https://github.com/elsewhat/openui5-spritz-reader
Each of the selection the user can make (BBC World News, Reddit /r/worldnews), has a single Model populated.
During runtime, I need to combine all the Models the user has selected and bind the combined content towards the items aggregation of the openui5-spitz-reader component.
You can use named models on aggregations see Multimodel Support - example using multiple models on an element JSBin OData Model dynamic column and data binding
I am not sure if you can bind data from 2 models on a single aggregation, you may be able to achieve it using a factory function with the aggregation or a handler on the model binding - using a generic binding to sum aggregation

Data Mapper pattern implementation with zend

I am implementing data mapper in my zend framework 1.12 project and its working fine as expected. Now further more to enhance it i wants to optimize it in following way.
While fetching any data what id i wants to fetch any 3 field data out of 10 fields in my model table? - The current issue is if i fetches the only required values then other valus in domain object class remains blank and while saving that data i am saving while model object not a single field value.
Can any one suggest the efficient way of doing this so that i can fetch/update only required values and no need to fetch all field data to update the record.
If property is NULL ignore it when crafting the update? If NULLs are valid values, then I think you would need to track loaded/dirty states per property.
How do you go about white-listing the fields to retrieve when making the call to the mapper? If you can persist that information I think it would make sense to leverage that knowledge when going to craft the update.
I don't typically go down this path. I will lazy load certain fields on a model when it makes sense, but I don't allow loading parts of the object like this, rather I create an alternate object for use in rendering a list when loading the full object is too resource intensive. A generic dummy list object I just use with tabular data. It being populated from SQL or stored procedures result-sets, usually with my generic table mapper.

Backbone Project Approach

I would like to make an application with backbone.js I understand the basics of backbone however I dont really know what the right approach to my problem might be.
I have a big jsonp file that is being retrieve from the server. So the next step would be to put the data from the jsonp file into a model. The data is bloglike containing a imgurl/title/text.
Now I could start a new model like this:
new modelVar = new BackboneModel;
However would that means that I need to create a new variable for every post I want to retrieve or could I let backbone create a set of models containg the post data.
Any suggestions book / blogs are welcome
Thanks
A quick answer could be "no". You can let Backbone loading data in models using a Backbone Collection.
E.g.
new App.Photos([
{url:"http://(...)_1.png", title:"photo1"},
{url:"http://(...)_2.png", title:"photo2"},
{url:"http://(...)_3.png", title:"photo3"}
]);
You just have to get an array of objects in argument when you create your collection prototype. Backbone will automatically create models based on the model attribute defined into the collection object. It's particularly fitted to your needs because you just have to put in argument the parsed json response and your models will be created.
I suggest you Backbone Marionette which is a good choice to start with Backbone implementation in order to get best practices.
https://github.com/derickbailey/backbone.marionette

Use of DTO in ASP.NET MVC

In the context of ASP.Net MVC 2.0, can anybody please explain why do we need to use DTO (Data Transfer Object) if there can already be Models? I have seen an example where a web service returns DTO to asp.net and then it is converted to Model using some factory class. This web service talks to database and returns data in the form of DTO.
In my previous projects, I used to communicate to DB using Data context and repository, which used to return model object to my controller. Then I used to pass this model to the corresponding view. Isn't this simpler? I cannot find out the exact use of DTO pattren.
Models represent the logical data model that your views are coded against. This may or may not map 1:1 with the source(s) of the data. In a situation where Model == DTO, I agree, the DTO is somewhat redundant.
In most situations where I've used MVC, it's been pretty rare to have a single source of data, or lack the desire to separate the logical view from the physical sources. For example I often make multiple service and database calls to build single logical model.