I have a view which is bound to a view model that represents an object model, in DB (it's defined with [Table(Name = "...")] attribute). The view model has EntitySets to allow the view to show data from linked tables(they used as sources for DataGrids). I want to add a DataGrid that will be bound to a SQL View (which composed of partial data from several tables). It should show only related to the main record from the view model rows, which in case of tables is possible through EntitySets. How this should be implemented?
In my investigation I've tried to see how Object Relational Designer (O/R Designer) - the DBML Designer in VS 2012 does it's part and noticed that it doesn't include implementation of INotifyPropertyChanged when it creates classes for SQL views. Is there any special reason for that or it just because the fields shouldn't be updated (since it's view)?
Should I treat the view the same way as a table, or something should be different? I assume that at least the binding should be defined with OneWay mode. And how the relationship to the current record in main view should be defined?
Related
When I try to insert or update data in my WPF usercontrol datagrid, the data is not being saved to the corresponding property. This is being caused by (at least so I believe) my bound property having the [NotMapped] attribute, all the other properties without the annotation are working correctly.
The data which need's to be updated is inside a DataGrid component which is bound to an ObservableCollection with the appropriate model. Inside the model there are several properties with the [NotMapped] annotation, these properties should be excluded in the creation of the table (model) in my database, though I do need them for binding input, hence the use of the [NotMapped] annotation.
Since the data is bound to a ObservableCollection I can't add the [NotMapped] properties to the usercontrol directly (so they wont be a part of the model).
Below an example:
Part of the XAML
In the image below we can see 1 property (pBreedte) which is 1 of the NotMapped properties, as well as the itemsource of the datagrid:
UserControl Code behind (part of it)
Part of the model which is used in the ObservableCollection
The model is being used for EF6 (code first).
Is there any way that the NotMapped property values can be stored / saved?
The easiest would be to just include the NotMapped properties in the database (so removing the annotation completely) but I am trying to avoid this.
More background information
The NotMapped values are added because they function as a placeholder property. Initially I had several decimal properties bound to the datagrid directly, but textboxes can't handle decimal values very well (WPF validation rule preventing decimal entry in textbox?). So I created a string placeholder for those decimal properties, when the form is being saved the decimal properties are being set to their string placeholder counterparts. This way the user can add decimal places without having to use a delay, value converter or anything.
If you don't need that information in your database, then don't store it - meaning your approach is good.
What I think here what might be the problem is that you are using your entity/database model as UI model.
I would suggest that you try to introduce a different model for the UI controls and user input. The models might seem to be duplicate at the beginning but while you are working on your application they will start to differ, but still describing the same items just form different perspectives.
Example:
Entity model has a class CarEntity. It is a pure POCO class, having only the needed properties that will contain the data in the corresponding table.
Ui model has a class CarUi. It has the same properties as the CarEntity. They are loaded and mapped from the database (from the CarEntity) shown to the user. If the user changes something, the diff values are mapped from the CarUi to the CarEntity and then stored to the DB.
With this separation of models approach, you should not face the issue where one constraint (mark column not to be stored in a table) influences other functionality.
Hope this helps,
Cheers and happy coding!
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have spent long time searching about MVVM in WPF. I understand why to use it. This is clear for me.
What I don't is, what is in ViewModel itself. I know it is the connection between the View and the Model. Also, I know (at least, what most of programmers suggest) that implement the INotifyPropertyChanged. Adding to this, the Commands.
Let say, that I have this Model (Code First with EF 6):
public class City
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CityId { get; set; }
[DataType("VarChar"), MaxLength(25), Required]
public string CityName { get; set; }
}
My Questions are:
1- In many examples I've seen, they recreate the same fields again in VM. What is the purpose, is it enough to have the whole object (City in my example). How about if there are many fields!.
2- To be able, to use it in my View (CRUD), how is the CityVm() will be in my example?.
I appreciate any help.
The view model in the MVVM pattern encapsulates the presentation logic and data for the view. It has no direct reference to the view or any knowledge about the view's specific implementation or type. The view model implements properties and commands to which the view can data bind and notifies the view of any state changes through change notification events. The properties and commands that the view model provides define the functionality to be offered by the UI, but the view determines how that functionality is to be rendered.
The view model is responsible for coordinating the view's interaction with any model classes that are required. Typically, there is a one-to many-relationship between the view model and the model classes. The view model may choose to expose model classes directly to the view so that controls in the view can data bind directly to them. In this case, the model classes will need to be designed to support data binding and the relevant change notification events.
The view model may convert or manipulate model data so that it can be easily consumed by the view. The view model may define additional properties to specifically support the view; these properties would not normally be part of (or cannot be added to) the model. For example, the view model may combine the value of two fields to make it easier for the view to present, or it may calculate the number of characters remaining for input for fields with a maximum length. The view model may also implement data validation logic to ensure data consistency.
The view model may also define logical states the view can use to provide visual changes in the UI. The view may define layout or styling changes that reflect the state of the view model. For example, the view model may define a state that indicates that data is being submitted asynchronously to a web service. The view can display an animation during this state to provide visual feedback to the user.
Typically, the view model will define commands or actions that can be represented in the UI and that the user can invoke. A common example is when the view model provides a Submit command that allows the user submit data to a web service or to a data repository. The view may choose to represent that command with a button so that the user can click the button to submit the data. Typically, when the command becomes unavailable, its associated UI representation becomes disabled. Commands provide a way to encapsulate user actions and to cleanly separate them from their visual representation in the UI.
To summarize, the view model has the following key characteristics:
The view model is a non-visual class and does not derive from any WPF base class. It encapsulates the presentation logic required to support a use case or user task in the application. The view model is testable independently of the view and the model.
The view model typically does not directly reference the view. It implements properties and commands to which the view can data bind. It notifies the view of any state changes via change notification events via the INotifyPropertyChanged and INotifyCollectionChanged interfaces.
The view model coordinates the view's interaction with the model. It may convert or manipulate data so that it can be easily consumed by the view and may implement additional properties that may not be present on the model. It may also implement data validation via the IDataErrorInfo or INotifyDataErrorInfo interfaces.
The view model may define logical states that the view can represent visually to the user.
This is a pretty broad topic based a lot on opinion. The MVVM model isn't a set-in-stone thing, and there's lots of different ways of implementing it.
The ViewModel, in my opinion, has two main purposes:
1) To expose the Model to the View. There's two ways of doing this. Having your Model wrap your CRUD object and manage NotifyPropertyChange. In this case, it's common for your ViewModel to just expose the Model to the View and not do much else with it, the View binding directly to the Model, or via simple pass-through properties.
The other common scenario is just to have your model as a CRUD. In this case, the ViewModel manages the NotifyPropertyChange notifications and manages model updates, etc.
2) To manage Presentation logic and data. There's lots of things that you need to bind to and store that have no place in the Model. The Model is the data. It's most likely loaded from a database. Yet the View needs to track other properties. An example: say your city object is displayed in a list, and you want to show or hide it with a button. The logic for hiding it, and the boolean for it being hidden doesn't belong in the View, and has nothing to do with the base data in the Model, so it sits in the ViewModel.
Similarly, maybe you have a CityPopulation property. In your view you want to highlight cities that are larger than 1,000,000 population in red. Your View Model has the a property:
bool LargeCity
{
return CityPopulation<1000000;
}
And you render the label with a style trigger.
Another example, a ViewModel containing a Brush property to bind to, which I think is probably a common but bad practice. slugster also comments below that visual elements should not be present in the ViewModel.
Brush HighlightBrush
{
get
{
if (CityPopulation<1000000)
{
return Brushes.Red;
}
else
{
return Brushes.Black;
}
}
1- In many examples I've seen, they recreate the same fields again in
VM. What is the purpose, is it enough to have the whole object (City
in my example). How about if there are many fields!.
Since the view model is the responsible of implementing view's logic and it performs actions against the model, there're some cases where some properties wouldn't be necessarily part of the model, but they're just there to serve view's requirements.
For example, let's say your model has a Name and Surname, and you want to show a concatenation of both properties in your view. You may be able to implement this using binding expressions or implementing a property in your view model:
public string FullName => $"{Model.Name} {Model.Surname}";
2- To be able, to use it in my View (CRUD), how is the CityVm() will
be in my example?.
This question has no specific answer. But you'll need a Model property at least to store a given City.
My web application currently has a domain model and a view model. Domain model is almost a representation of my database. So when I insert data into a table, i'm inserting my domain model object. My view model handles how I want to display my ui. So if i have a property on view model that has a drop down list, i give it a custom display attribute drop down list with an enum saying what kind of values to load ([DropDownList(Enums.Product)]. My view model also has display labels and calls specific editor templates for the different properties using UIHint. I feel like creating a view model is becoming repetitive. I'm wondering if there's any harm in just putting display attributes (uihint, display(name="blah")) right on my domain model so i can skip the view model unless specifically need it for something. The only other solution is to actually wite out the html so that i'm using Html.Editor("SomeProperty", Model.SomeProperty) instead of Html.EditorFor(x => x.SomeProperty). Any thoughts?
I need some help about MVVM pattern.
I created a ViewModel that expose data and commands to be displayed in a listview in a View named A.
This ViewModel is also used in a view named B. In this view, i just need to expose some properties and no commands but i had to create 2 more properties.
Is it better to create a more specific ViewModel for View B even if it concerns the same object?
I would suggest composition, have two view models which both have a property containing a view model that holds the common properties. The two view models should then only have their specific other properties and commands.
Difficult to answer. But i can tell you what we do for our application. We have one viewmodel, which is more or less view independant, it just functions as a wrapper for our business data and contains all the stuff that is used in almost all parts where this model is shown. Now for the view part, we have very specific viewmodels. Like a ProjectTreeViewModel or a SearchResultViewmodel, with the corresponding Item viewmodels for both of them. The Item viewmodel doesn't need to implement all the logic again, it just needs to agregate the general model view model.
To give a better analogy:
If you have a File, Drive and Folder model. You would create a FileViewModel, DriveViewModel and FolderViewModel. But only one ExplorerItemViewModel. This only needs to provide a property to expose the underlying view model. The rest is depending on your data templates.
Existing Model, Controller and View, all working well.
Due to a requirement change, there is suddenly a change in the db schema, need to add a few new fields.
Database is updated, model (ADO.NET Entity) is refreshed from database and new fields are picked up. Controller is OK since it is working with the "set" and keying off the PK. The view when originally created was strongly typed against the model (as it was at the time)....
Is there an easy way to "refresh" the existing view to pick up the new fields?
Or will this always be a manual addition or where you would need to create a "newer" view to replace the previous view?
Thanks!
On the slim chance you're using the exact view created by the "add view" command, you can just delete/re add it. Otherwise, you can add a new temporary view, copy over the new/changed fields and then delete your temporary view.