Detecting validation errors using PRISM4 - wpf

I'm doing an app using Prism 4 and WPF and I've some inputs validations and it works as expected but when the user clicks a button I need to know in that moment whether there are validation errors presents.
Does Prism4 have something implemented to handle this or I've to implement it by hand?

Dealing with validation errors isn't something Prism provides as a part of the library. What prism does provide that can be useful when implementing validation is the IConfirmNavigationRequest interface, which enables you to stop navigation in certain cases - for example when the page did not pass validation.
To implement validation in your application, I recommend using the IDataErrorInfo interface (or INotifyDataErrorInfo in .NET 4.5/Silverlight).
EDIT
To know whether your view passed validation, you'll need to expose an IsValid property in your viewmodel that will return this information. One way to do so is to examine the viewmodel and return this data; another way is to have your ViewBase register to its errors event using Validation.AddErrorHandler, and call ViewModelBase.AddError()/ViewModelBase.RemoveError() on your view-model. This way your view-model knows about the validation errors and can easily return whether the view is valid or not.

Related

What is best pattern for angular js: MVVM or MVC?

I am new in angular js. I want to know difference between MVVM and MVC framework and which one is best for AngularJS.
I know about MVC and MVVM pattern but I can not find which one is best and why?
Thanks,
Hitesh
In MVVM, the UI (the View), faces the user and takes user input directly. Within the View, Commands within the ViewModel (which is the DataContext of the View) are triggered by this activity. Control flows to the ViewModel which interprets what the View has sent it and prepares its Models. After control flows back to the View it updates itself according to changes in the Models. If a new View is required, the ViewModel communicates this with the NavigationService (or whatever method of navigation your application uses), which is the purview of the Window or Frame--UI components. You can see that the ViewModel isn't first and last to act; the View plays a much greater role than in MVC.
The architecture of WPF/Silverlight is the reason why things are done this way. The command, binding and navigation infrastructures can't be controlled/replaced by the Controller; they are tightly integrated with the UI. So the Controller must sit below the View and take a more passive role.
AngularJS is MV*(MV what ever) either it can be MVC or MVVC.
Model : Data you want to update.
View : HMLT/Template.
Controller : function which maintains application data and behavior.
Example for MVC is binding data to view using Interpolation {{}}.
You can update view(html) using controller.
Example for MVVC is two way data binding using ngModel.
If you bind a ngModel to input tag.
Value changed in input filed is updated in controller.
Value changed in controller is updated in input field.

WPF MVVM Authorization

I'm trying to build an authorization mechanism for my WPF/MVVM app. Normally, I can easily place authorization check (i.e. a call to Authorization service/provider) in the OnStartUp() function of the View's code-behind. To adhere MVVM, however, is there any way to avoid making such a direct call in View, but in ViewModel class instead, so that the View's code-behind remains empty? Or what's the most effective way of implementing auth in WFP/MVVM?
For example, let AuthProvider be a property of the ViewModel, which is bound to the View (as with other properties and commands). How can I proceed that binding in order to implement the authorization mechanism here?
Thank you!
cheers
Create a View for authorization , bind whatever is required in that view to authorization properties in the VM (e.g., User name/password UI elements binds to VM.UserName and VM.Password and the Authorize button binds to a Command that authorize the user input and then sets VM.IsAuthorize property if all successful).
Upon successful authorization set the VM.IsAuthorized property and bind it to show whatever view under the authorization view or to allow navigation to another view.
This is also a good reading http://blog.magnusmontin.net/2013/03/24/custom-authorization-in-wpf/

Doubt about validation in WinForm application

I'm creating my first application in WindowsForms and wondered how I would do to validate the User input logic layer and return to the textbox control such that he filled out incorrectly by ErrorProvider. That is, each would have to return the validation error for each specific control indicating whether the user typed an invalid value.
This is a good practice?
Today, this validation is done on the presentation layer and would like to use my more logical layer so that she is not only acting as an intermediary between the presentation and data access.
If you would like to maintain separation of concerns you should look into implementing the model-view-controller design pattern. This will allow you to keep you UI code cleaner
Overview of MVC
Model
Contains state and implementation
Notifies listeners (views) of changes
View
Displays model state (and responds to model state notifications)
Send all user input to the controller
Controller
Processes all input from the view
Changes model state and calls model methods
Load a different view as needed
Further Reading
http://www.martinfowler.com/eaaDev/uiArchs.html
http://en.wikipedia.org/wiki/Model-view-controller

MVVM firing event back to view

I have a ViewModel where one of its functions is to talk to a service and get some data. However, if there is a problem, I would like a notify the user that the service could not run.
Currently what I am doing is firing an event which the view has subscribed to (my viewModel is created in the resources section of the view) and receiving the event in the view event handler I just do a Windows.Alert().
First, I am trying to reduce the amount of code in the code behind of the view and with the event firing, there must be a better way to do this?
Secondly, since my view knows about my view model (i.e. created in the resources section), I am sure this will cause problems in testing my view. Is this the correct way to do this?
JD.
Yes, I don't think subscribing to an event in the VM from the View is a good idea. Almost better to put the alert in the VM, but that puts UI in the VM and makes it hard to test. There are a couple of other ways to handle this.
Dialogs and ViewModel - Using Tasks as a Pattern
Decoupled ChildWindow Dialogs with Prism in Silverlight 3
Best to use a service here. A service just provides some function through an interface.
public interface IDialogService {
void ShowNotifictation(string message);
}
The ViewModel takes this service and uses it to display the notification. The implementation of this service is then specific to your solution, and can display the notification however you want.
The implementation of such a service might look like this:
[Export(typeof(IMessageService))]
public class MessageService : IMessageService
{
public void ShowMessage(string message)
{
MessageBox.Show(message);
}
...
It uses MEF as IoC Container. The service is registered via the Export attribute as IMessageService.
You might have a look at the WPF Application Framework (WAF) to see the full implementation and sample applications that are using this service.
Hope this helps.
jbe

Redirection Between View In MVVM

I am using MVVM patern for developing my WPF application. It working fine for unrelated pages, means how to go in another view from one view.
Eg:
I have one list page in which some records are coming from one ViewModel and another from another ViewModel, means I have two ViewModel form my single View. And now I want to display another View by some event.
I am using IsSelected property for notification of changes. This mechanism works only upto when any action performed on same ViewModel, what should I do for such senario.
MVVM as a pattern is about separating concerns, improving testability of your code, etc.. so your ViewModel should only be concerned with applying business rules and providing data for your View.
You will need to use this in conjunction with some kind of MVC pattern, where the Controller's concern is handling the application navigation/state, etc.
(edit)
For example, imagine your app has a login screen, so you create a LoginView, which contains a username and password; probably an OK button and a Cancel button.
You create a LoginViewModel class to bind this view and handle the logic of the login within this class.
But once the app is logged in, it is not the responsibility of the login ViewModel to know where to go next; or which View to render next.. maybe you want to navigate to the last screen this user was on the previous time they were logged in? Maybe it goes to a default screen, as per the User's profile? This decision is nothing to do with the login function...
So if you create a Controller class, you can: Instantiate an instance of the LoginViewModel class, then depending on the login result, apply business rules as required to remove the LoginViewModel from scope, and create a new ViewModel, (e.g. HomePageViewModel) etc...
Finally, you'll need to let the app know which Views to use for each VM using DataTemplates
There are heaps of other ways to skin this particular cat, of course... this is just one idea...
As long as the core concept remains: Use MVVM to bridge the gap between View and Model in a clean, testable way... don't try and make it the 'one pattern fits all' :)
HTH :)
I agree with IanR to use a Controller for the workflow/navigation.
The ViewModel sample of the WPF Application Framework (WAF) shows how this might be done.

Resources