I have search employee page and edit employee date. User can search data in employee search page and it will show first name and last name of employee, so I create SearchEmployeeEntity in my presentation model. When user click edit employee, I will get data from service, this time I get first name, last name, username, birthday and other informations, so I create another presentation model name EmployeeEntity.
I think about my design for a while, there are two way to doing this, one is as I just explain above, other is use only one presentation model (EmployeeEntity) in both service.
Anyway, if I use EmployeeEntry in all place (search page and edit page) it's quite hard to manage data since I use static DomainContext.
Please suggest me, which way is good to create presentation model in Silverlight Application?
P.S. I use MVVM at my client.
It's the same entity so you should only need one. What's different is the context in which it is used - either you're creating a new Employee, or editing an existing one.
Silverlight RIA Services will handle this for you as it knows how to perform change tracking.
So, when you are editing, you will do a lookup against your context and perform a LoadOperation, which will return the entity to you from the DB. Allow the user to edit fields of the Employee on the clients ide, and then perform a SubmitOperation to persist the changes to the DB.
For new Employees, you create a new EmployeeEntity in your ViewModel, and then add it to the Context's Employees collection. Like so:
EmployeeContext.Employees.Add(Employee);
When the fields are filled out correctly (and hopefully you're taking advantage of Silverlight's super-easy validation rules to ensure correct data) then you, again, perform a SubmitOperation to save the new entity to the DB.
Related
I have a typical master-detail scenario. User can click "Add New" in the master view and enter the details in the detail view. So for example lets say I have list of ObservableCollection of Product. When the user clicks "Add New", I add a new Product record to the ObservableCollection and then open a detail view for adding the Product details. This works well if the user does save the product. However if the user decides to cancel the addition of new Product, how do I remove the same from the ObservableCollection of Product in the master view. Right now I could think of two ways, first to pass the reference of ObservableCollection to the Details view, and second, use events to notify master view about the new Product record deletion. What do you suggest ? Also, if there is better way to handle this in prism specifically, please let me know.
regards,
Nirvan.
Passing a reference to the ObservableCollection to the details view is a no-no - it should only know about the record it is bound to.
You have not specified the UI you have in place for this, there can be a couple of ways to do this that may be better suited for your design. However one reasonably agnostic way to do it is to use the EventAggregator to publish the event which the master view can subscribe to. It is up to the master view how to deal with that event, different views can respond in different ways. In this particular case the master view can check the currently selected item, and if it is new (i.e. hasn't been saved, doesn't have key pieces of info like an ID) then it can simply remove it from the collection and discard it.
Suppose I have a WPF/MVVM application for managing some hypothetical customers :).
Domain model contains an entity named Customer (represented as a POCO in code).
The main screen contains a grid, bound to a view model (CustomersViewModel) that loads its data from Repository< Customer>.
The main screen also allows to create new customers (and save it to the DB).
Suppose I need to implement 'add customer' use-case. The most obvious approach is as follows:
Present the user with a dialog window to be filled out with new customer data.
Handle 'Save' button click in the ViewModel.
Create customer (var new_customer = new Customer(..)) domain object using the data from the dialog (step 1).
Call Repository< Customer>.Save(new_customer) to save the new customer to the DB.
Reload CustomersViewModel with fresh data from the DB so that newly added customer is visible in the grid.
Personally I don't like this 'quick-and-dirty' way (because of need to reload the full list of customers from DB every time a new customer is added).
Can anyone suggest a better approach (that wouldn't require refreshing the customer list from the DB)??? I feel there gotta be some best practice for handling scenarios like that:) ).
Thanks in advance!
If the saving of the Customer is successful, why can't you just add that single Customer instance to your collection of customers? No need to re-load all customers unless the user explicitly refreshes the view (usually via a refresh button).
If you are loading the list in your view through a binding (to a list of customers) you can just add the new customer to that list and everything is alredy ;-)
I have a similar application where in the object is created in UI. I solve it by adding the object in VM and then syncing it with Model on click of Save button.
I am assuming you have a list of CustomerViewModel in CustomersViewModel to which the grid view is bound to. You can add a new CustomerViewModel object to the list in CustomersViewModel. While saving the ViewModel data back into the model, the model gets in sync with VM. No need to refresh VM back from Model unless somebody else apart from your app is changing the Model data.
You could create an ObservableCollection<Customer> and fill it with the customers from the database which you want to show in the View. When you add a new customer then add it to this collection as well as save it into the database. The CustomersView binds on the ObservableCollection and is updated automatically without the need to refresh the data from the database.
The BookLibrary sample application of the WPF Application Framework (WAF) shows how this can be done.
I have an application with two forms ( windows )
They represent the front and back of an ID card
Users enter data and the printer spits out something that can be folded and laminated to give the front back effect
I am using different Views to show the front and back and I guess my question is how do I use the same ViewModel instance in both( the data needs to be saved to a DB before printing )
Or am I doing this all wrong and I should just have one view and show hide Front\Back ( i really hate that )
Thanks
Generally, you are going to want one ViewModel per View, since the job of the ViewModel is to provide exactly the data needed by the View it is attached to.
So, in your case, it sounds like you should have one View and ViewModel for the front and one View and ViewModel for the back.
There are several ways you could go about saving the data when the user submits the form. A common pattern is to set up Repository classes for each of your database tables. Repository classes typically allow you to select, insert, update, and delete data. So, you could pass the necessary Repositories into each ViewModel, then when the user submits, have the ViewModel tell the repository to insert or update the data.
I have a simple winforms application that I am connecting to my database with linq-to-sql.
I have generated the classes directly from the database, and I have a DataAccess class that wraps my datacontext and can give me whatever I need.
I have a view that uses an object datasource to populate a DataGridView and a set of related text fields, etc.. for my entity (lets call it EmployeeView)
The view loads all of the existing rows, and as I click through the grid, the fields update appropriately.
If I change fields, the changes will persist through record changes, but I am not sure how to save changes through my data access layer. How can I detect which records are dirty and need to be saved? How do I then save them? What is the best way to add new records? Delete records?
I can find a lot of resources online, but none with the kind of examples I need. Can anyone help me out with some of the basic patterns, or point me to a good place?
The most basic way to use LINQ-to-SQL classes, I believe, is to instantiate a list of them (let's use Employee, for example) to contain the Employees you wish to (potentially) edit. When the properties of those Employee objects are changed, the objects are automatically "dirtied", and a call to DataContext.SubmitChanges() will persist them.
List<Employee> employees = (from e in dataContext.Employees where e.Salary > 50000 select e).toList();
foreach(var employee in employees)
{
employee.CanAffordToyotaPrius = true;
}
dataContext.SubmitChanges();
If you're wrapping the DataContext and only altering the properties of the wrapper object with the DataGridView, you'll need some way to bubble those changes down into the underlying LINQ-to-SQL objects you used when you selected the data. For example, you could use the setter on your wrapper's properties to also set the underlying LtS object's properties.
My concept for MVVM in WPF is that we have a ViewModel for every Model in your application. This means that if we have Customer class (entity) then we will have CustomerViewModel. The CustomerViewModel will have all the properties which are necessary to represent a customer. The CustomerView usercontrol will be responsible for creating the UI for the Customer model.
Now, let's say that we are adding a new customer. So, we have a form which consists of FirstName, LastName etc. Do we need a ViewModel for this scenario. I mean all I have to do is to take all the input values from TextBox and create a Customer object and then save it to the database. Why should I bother creating a ViewModel for this scenario?
First of all, that is not the main purpose of MVVM, to "mirror" everything. The View should provide the means for a user input, and certainly not process calls to any of the database layers. The ViewModel should be a GUI-agnostic application backbone, and it definetly should handle the creating of customers.
That said, what you should do, is have a ViewModel which represents a workspace for handling customers, and not just a customer ViewModel. If you really want to save on a few objects being created, add to that workspace the possibility to create and add a new customer (not CustomerViewModel). That way, you can have a View of the workspace which has elements for each relevant/required property of the customer, and by invoking some command added to that workspace ViewModel, you could get the current values filled in those (data bound to ViewModel) View elements directly to the customer model.
Consider if you could probably drop the specific customer (and other Model) ViewModels if you refactor things a bit, it would be good practice to keep things from adhering blindly to a certain pattern without explicit cause.
Let's pretend for a second that there is no business model. The only thing you have is a view. If you were to model just that view, without any knowledge of what the data means elsewhere in the system, that is a ViewModel.
The goal of a ViewModel is to, well, model the view it backs. This is a different goal than modeling the idea of a customer in your business domain. To say you will have have one ViewModel per business entity, then, is to say you will have one view per business entity, which leads to run-of-the-mill data-focused UI.
In your particular case, think about the customer edit view. It has fields that correspond to a customer's properties, and thus seems like a natural fit for binding to a customer directly. However, where on the customer object is the "Submit" action modeled? Where is the "Cancel" action modeled? Where is it modeled that field X is an enumerated value selected from a list?
How about the password? If persisted as a binary hashed value, does the view know how to hash its text? What if the system has password strength requirements?
The ViewModel is the mortar between the business model and the UI. It takes concerns from one and translates them into terms of the other. It is the point at which all the above issues are addressed. To say a ViewModel isn't necessary is to ignore its necessity.
You don't need to have a separate ViewModel for Add, you only need a single ViewModel which should do Edit and Add scenarios. If you can delete the record from the edit page than that ViewModel should also have the ability to delete. Your ViewModel should reflect the functionality your View exposes regardless of data.
I think you should reconsider having a single ViewModel for each model. I like to think of ViewModels as normalizing behavior inserted of data. By having a ViewModel for each Model class you will run into architecture issues sooner or later. I look at the application from top down overview, what is my UI trying to accomplish and from there I will get to the ViewModel and eventually I will get to my DataFactory and how the ViewModel maps down to data is almost always not 1 to 1 except for the most simplistic Views. If you try to map 1 to 1 you will have bad UI or your data will not be normalized very well.
The Stack we have here is:
View
ViewModel (Controls everything the user can do in the view, wraps properties from our POCO's)
DataFactory (Maps our POCO's to Entity Framework objects and CRUD)
POCO's (Business Logic, all rules and validation)
Entity Framework (Model, Data Access)
One note here is that ViewModel contains properties from multiple POCO's!
We inject the DataFactory through StructureMap and Unit test with xUnit along with Moq.
To answer you second question I would create a separate view only view to drop in as a user control. But you should still have a CRUD ViewModel in you app that encapsulate all that functionality in a user friendly way.
Thanks.
One reason for this VM abstraction is for testability. Another reason why you want a ViewModel is because it's basically a Data Transfer Object that might be a combination of fields from multiple models in a single container that is more relevant to your current view. Yet another reason to have VM is to take advantage of WPF two ways binding capabilities.
Using your regular model (plain POCO), you can update the View when your model change, but since your model does not implement dependency properties (most likely), you won't be able to take advantage of updating your model when the value in WPF control changes. Which mean you have to manual add a handler and do the copy this value from this control back to the model kind of thing.