MVVM Light requires a reference to System.Windows in my ViewModel? - mvvm-light

My goal was to enable the ViewModel to tell the View to present a MessageBox and ask the user a question.
I didn't want the ViewModel to have any reference to Windows-specific code.
I take the time to download and install MVVM Light Toolkit. Now I find that even with the toolkit, it is necessary for my ViewModel to reference System.Windows, in order to resolve things like MessageBoxButton and MessageBoxResult.
Why go through all this extra work, when you just wind up with a ViewModel that has a reference to System.Windows? I might as well call MessageBox.Show() straight from the ViewModel. Essentially I have saved nothing by using the MVVM toolkit, so I just don't understand why I would use this.
Can someone please explain to me, why go through this effort, if I still have to reference System.Windows in my ViewModel?
Thanks, Hugh

When you use MessageBox class, you need a reference to System.Windows.Forms.dll. MVVM-Light does NOT require this reference.

It doesn't defeat the purpose of the toolkit. The toolkit provides a light framework for separating your view from your model, which is definitely helpful with unit testing.
They reused the enums, so they didn't have to rewrite them. It definitely does seem a bit strange to reuse UI specific enums, but at least they aren't event handlers and event arguments.
Lastly, MVVM uses commands, which requires the implementation of ICommand interface. That interface is in the PresentationCore and is even more UI specific :)
Overall, the purpose isn't to remove references it is to not use those references as best you can and enums definitely don't break the pattern.

The mvvm light toolkit is there to help you implement a the mvvm pattern. The mvvm light toolkit also has the really nice advantage of being blendable, (some tweaks are made for you so you can use Expression Blend to design very easily). The Mvvm pattern also really helps (as others said) the testing of your application as it is hard to test the view but it is a lot easier to test the viewModel.
If you want to know why you go through this effort and use mvvm light I suggest you check this related answer
Hope it helps and good luck!

Related

Get instance of view registered in Xlabs.ViewFactory

Actually I am trying to structure my project in a clean MVVMV way. Unfortunately to this restriction I need to find a way to set pins to the map without the power DataBinding. One workaround could be to have the View in my ViewModel and set it directly.
Right now I don't have any chance to get the registered view. Does it make sense to extend the XLabs.Forms.Mvvm.ViewFactory?
Before adding a new issue on GitHub, my hope is that there is a better solution to handle such restrictions. I always used MVVM in my (small) WPF projects.
No that doesn't make sense. Your best approach would be to extend the Maps class to make Pins an ObserverableCollection that is bindable.
The new documentation format on the Xamarin site is rather difficult to read and does not provide the information you require at hand like it used to but I believe the Pins object is not bindable.
Take a look at the Xamarin Forms Labs Github project to get an idea of how this can be done. The approach is defined particularly well in the ExtendedPicker classes (here the items are bound to an ObservableCollection).

ExtJS bidirectional Data Binding? Or alternatives

I'm a pretty experienced Flex/.NET developer who is now learning html5/javascript. I've been playing with Ext Core and ExtJS for the last few weeks and I'm interested in whether/how folks are using these libraries to do bi-directional data binding on data that is not list based.
It seems like most of the binding support is directed at binding rows of data to grids. I'm interested in binding UI elements to arbitrary POJOs that are not list-based data.
Maybe I'm just not looking in the right places, but I'm not finding support for this.
I'm really interested in hearing what folks are doing in Ext OR what folks recommend as an alternative.
Although there is no out-of-the-box solution like the #Bindable from Flex for example, Extjs does offer the infrastructure to create bindings.
If you have a look at http://blog.dnet-ebusiness-suite.com/2012/03/data-binding-in-extjs-4-grid-form.html you'll see a demo for this where the models (filter instance and record instance, instances of Ext.data.Model managed or NOT managed by a store) are bound to different views. The functionality is achieved with a thin framework built on top of Extjs.
On the other hand, to bind arbitrary POJOs is exactly the same story, those have to implement an Observable and fire themselves some property change events which can be listened. The blog posts and the framework itself gives a good starting point on how to do this.
Another way/concept is http://www.sencha.com/forum/showthread.php?60809-Ext.ux.data.BindMgr-Databind-Manager which i used with extjs 3 and works fine.
A follow up... I never did find an ExtJS approach and moved on to other js frameworks/approaches and along the way learned about knockout: http://knockoutjs.com/ which is pretty darned awesome!
Are you looking for the BasicForm functionality? (Particularly: loadRecord)
If you want readonly display you can use DisplayFields or disabled TextFields or similar.
Take a look at jQXB , http://www.jqxb.altervista.com. It's seems to provides a very easy and powerfull api to bind data with html elems

How to use MVVM Light Toolkit with existing non-MVVM Light Toolkit project

I need to use the Surface project template for the application that I am working on but I also want to use the MVVM Light Toolkit too. I see that I can "Add | New Item..." and choose an MVVM teamplate for a view, view model, or locator but there seems to be a few more things I need to do to wire this up beyond adding one of those and making sure I have added the reference to the mvvm assemblies.
I am in the process of trying to figure this out by studying what's gets generated when I create a MVVM Light project but I was hoping someone already had this all figured out and documented. I know that Laurent mentioned that he was going to write this up in a blog post but I looked and could not find it. If someone knows where I can find that post that would be killer. Thanks in advance.
It's not too bad adding MVVM Light to an existing project. I finally wrote up the blog post I've been meaning to, just for this situation:
http://chriskoenig.net/2010/07/02/adding-mvvm-light-toolkit-to-an-existing-project/
Enjoy!

Proper implementation of MVP with complex controls

I'm currently implementing a project using asp.net, c# and the MVP (Model-View-Presenter) pattern. The main purpose of this solution is to deliver a graph to the consumer, to be used by a variety of systems. It is basically a custom graph server.
The view page in this particular case has an MSChart control on it, which has to be dynamically populated and configured based on parameters in the QueryString. This can be as diverse as totally different types of data sets, display modes and so on, using a lot of the properties of the chart control.
Many of these properties are again of types which are particular to the chart control and would require the same dependencies as the chart control itself if they are to be set by the presenter.
I'm trying to figure out the best way to expose the properties to the presenter so it can work its magic.
Should I:
Just expose the whole chart object and live with a system.web type dependency in the presenter project?
Make accessor and translation properties for all of the chart control properties so that I don't have the dependency, but add lots of complexity?
Other, that I haven't thought of?
To me it seems that it would be against the MVP pattern to bubble a display control up into the presenter, but it seems that trying to map all the properties to DTOs or similar would be a lot of work that would add a lot of complexity, and while the solution would be somewhat more loosely coupled, I'm not sure the gain would be worth it in this case.
How would you implement something like this, given MVP?
While doing some more research on this topic, I found the following blog post on adding a Presenter Model to handle complex view controls, to map between the view and the presenter. It actually made a lot of sense to me, and it's an idea I think I'm going to follow up on and try.
http://mikewagg.blogspot.com/2009/01/managing-complexity-with-mvp-and.html
In fact, Martin Fowler has written on this as well:
http://martinfowler.com/eaaDev/PresentationModel.html
Check out Automapper. Makes translating from a Business Object to a View Model almost effortless. The general idea is your View Model should only have primitive system types if possible to avoid formatting/conditionals in your view.

AS3/PureMVC Best Practices? Best code examples of well architected projects?

I am a AS3 novice learning PureMVC and want to write code following best practices so that any other AS3 developer can pick up my code and easily understand what I did, I am tempted to do stuff as I would in JavaScript or Asp.Net/C#, but I have a feeling that might not be the best approach.
Thoughts? Links?
Using reverse domain folder structure is common from the Flex code I have seen. ie:
com/mydomain/myproject/view ... model, business, controller (this would make it easy for me to understand your code)
More: http://blog.tsclausing.com/post/11
ASDoc is a tool that creates very pretty HTML documentation from code comments automatically:
http://livedocs.adobe.com/flex/201/html/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Book_Parts&file=asdoc_127_1.html
You may be beyond this point but I have used Cairngorm (MVC) and it was well documented:
http://opensource.adobe.com/wiki/display/cairngorm/Cairngorm
Code Example
This is the Cairngorm store which is the standard Cairngorm example:
http://cairngormdocs.org/blog/?p=17
I found that reading through the docs helped me get a clear definition of each of the parts to PureMVC. On top of that I downloaded the source for the demos and added them in Flex Builder so I could look through them easily and see how they were constructed to get an idea of how I should construct my project.
One thing you have to remember is that you can do things any way you want, but to make using the framework worthwhile you should stick to the structure and way of doing things that it suggests. For example you could give your view a reference to the facade and have it get information from Proxies etc. But you should keep the view decoupled from the framework and just have it dispatch events and have a Mediator deal with the facade.

Resources