using MVVM binding for composite fields - mvvm

I have a quick (hopefully) question on how to implement a composite field using MVVM.
I have 2 examples, one is on the sql end I am storing gps coordinates in the following manner "Latitude,Longitude" for instance "45.55555,-80.00000". the other example is storing feet and inches as a single double field with it being ft.in.
How should I go about this? Should I have two fields and in the model or Viewmodel composite them if the other piece exists? Should I bind them to the same field and somehow validate the sets?
thanks!

In the Model I normally use a structure/layout that is closest/easiest to the source so it can be easily/quickly read and written.
In the ViewModel I aim for a representation of the View to accommodate the bindings.
In general I make the ViewModel responsible for the transformation between the Model and ViewModel.
So whether or not a property of the ViewModel and the Model should have one field or two depends on the requirements of the View and the Source.

Related

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

EF- Replacing inheritance

I am using inheritance currently in EF and feel like it is causing more issues than it is helping, especially with binding an aggregation of tables to a datagrid. I have given a screen of part of the model. What I am trying to do is bind FREQUENCY to a datagrid, and have the grid fields be based on the type of FREQ_POOL(which is a base class). So for instance, if I want a POOL_IA datagrid, then it would have those fields, as well as the few fields in FREQUENCY. I was using inheritance because it made since from an OO perspective. The alternative is to just have lots of 0..1 relationships that show the ability for FREQ_POOL to have an extension, but then I have no constraint in place saying that FREQ_POOL can only be ONE type. What is a better design to accomplish this and make data binding easier? Thank you for any guidance.
One approach may be creating a data grid that gets the data from FREQ_POOL and then put all the variables of POOL_IA (or the all the properties of derived class using reflection) and FREQUENCY .
If you really don't need using objects while binding your data grid and able to use DataSet the another approach may be getting all the properties and the values of entry with Context.Entry method on the fly and put it into DataSet dynamically.

MVVM viewmodel and model questions

I'm trying to learn MVVM, and i'm struggling a little on differentiating between a model and viewmodel.
If someone could answer these 2 questions it would help clear a lot up for me:
Say I have an Objects class, which is a viewmodel that contains multiple ObservableCollections of Object.
The Object class contains an ObservableCollection of strings that are displayed on the GUI.
Is the Object class a model or viewmodel?
What if the Object class contains just a string and a integer (name and value), is it a model or viewmodel?
The Model is the class that holds your data. The data can be strings /integers or whatever.
The Model can also be a list / collection of those objects. For example a List of Person objects can still be your Model.
The ViewModel is the tier between your Model and the View. It should be used to perform whatever tasks you need on the data (for instance, if your Model is a list of Person objects but you only want to show in your view people that are aged older then 18, this logic is done in the ViewModel)
So to answer your question:
If you have an object which contains the data (in your example a list of strings) it is the Model.
Even if the object is a little more complex (with relation to the number of properties it holds) it's probably still the Model.
Business Logic should be kept separate from the model. On the other hand Validation can be added to the Model (for instance to make sure the Age property of a person is non-negative) since this is still rules on how your data should behave

Knockout Model vs ViewModel

I am trying to understand the difference between Model and ViewModel in KO. I understand the conceptual difference, but to me it seems that all Models in KO will become or are candidates to become ViewModels. Ill explain:
Say you have a table with a row of seats, so in your main ViewModel you will initialise and load a collection of objects from a Seat Model into an observable array.
Now you want to hide display seats based on a certain property of seats...this is the point where your model becomes another viewmodel...
So is it right to say that in KO all we have is ViewModels?
Yes. Essentially, anything with a ko.observable is creating a ViewModel. You could create a simple javascript model expressible as nothing but JSON, and wrap it, but the "model" in Knockout's MVVM pattern generally exists only on the server. You get the model data from the server, and you send model data back to the server (remember, you don't send the observables, just their data)
Knockout doesn't really care about where the model comes from, because its primary role is to deal with the databinding between the declarative view (HTML) and the ViewModel (with its View-aware observables).
The MVVM pattern is completed by technology outside of Knockout, since it's a client-side only framework. You could say that it is just the VVM part of the pattern, but that is confusing.

Where should ASP.NET MVC 2 validation go: in the model or viewmodel classes?

I am using automapper to map my models to viewmodel classes to pass to my view.
My question really is where should the validation go? I was planning on using the MetaData decorations - a feature of mvc 2.
But either in the model or viewmodel? Or in both places?
Validation should be done minimum at the View Model because this is what you receive as action argument and contains the user input. You could also have validation at the model.
My answer would be ViewModel because Model can change (for example from using Linq2SQL to EF). This way when you plug another Model you still have your validation intact.
I personally have my validation 2 places using DataAnnotations. My model is not passed up to my view in full. I have separate models for my views and translate the data from the view model into the model. This way, I can put whatever I want in my view model and leave out the pieces I don't want to deal with.
My reasoning, however, is that I have a windows application and an web application using the same model. This way, the same set of validation rules govern the Model for all apps, and my view model can have slightly different rules if need be. Of course, this creates a "duplication of logic" - well, validation logic.
This way I don't have to rebuild the data that wasn't used on the page every trip back to the server or put it in hidden fields and inflate the size of my pages.
You should put validation that is specific to the UI in the ViewModel, and anything that is related to business process or database validation in the Model. These might overlap.
The model should implement the validation it needs to ensure that its state cannot become invalid; that validation most definitely belongs on the model.
for example, a book class must guarantee that its title must be between 1 and 50 characters, its id must be >= 0 etc.
business rules belong elsewhere (in your controllers if you only have the model view and controller layers). this might be something like a user cannot add more than 3 books if their email isnt verified.
validation in the view should be restricted to parsing user input for invalid data: anti xss, sql injection, out of range. etc