Silverlight: beginner questions about mvvm pattern - silverlight

For a couple of weeks I'm reading (and experimenting) with silverlight and mvvm pattern. I've learned a lot about it and I think I understand the basics of the pattern and framework but still I have few questions about practical usage of mvvm pattern. I hope someone with bigger experience then me can help me :)
Here are my questions:
Let's say I have many TextBlocks in my View, should I put readonly properties in the ViewModel for every TextBlocks? Should I do that with every text that I have in View? I feel it as a bit overwhelming and I wouldn't do that.
What about localization? Using resources files is the the best we have? Or should it be translated throughout properties in ViewModel?
Is there any good logging framework for SL? or I shouldn't use any and only focus on logging web services?
I've found this link: MVVM-light + RIA Services best practices - but I can't find answers to all my questions.
Edited:
Thanks to Adam I've found nlog: http://nlog.codeplex.com/ but it's in beta. Does anyone know anything else?

#1 - NO, you would not normally put readonly properties in your VM just for your view to bind its textblocks to. Those text values are just part of the view, just like they would be for an MVP WinForms app, or an MVC Web App.
#3 - possible duplicate of Silverlight Logging framework and/or best practices

For #2 If you use silverlight business application, you already have full support for localization. Check this out Silverlight 4 + RIA Services - Ready for Business: Localizing Business Application and I would do it on XAML level instead of VM

I highly recommend the MVVM In The Box training from my colleague Karl Shifflett of Microsoft patterns & practices.

Related

Silverlight and MV(VM) Pattern - No Code-Behind - Is this possible?

I am working on Silverlight 3.0 and currently using code-behind in my Views. I would like to implement the MV(VM) pattern into my project as the project is growing. I have seen some WPF projects where the code-behind has NO CODE except for the InitializeComponent method, which I believe is the right way to go and just looks clean. Is this possible in Silverlight 3.0, that is, no code-behind?
Can anyone point me to some good Silverlight (3.0) MV(VM) tutorials / articles?
Without using some additional framework, Silverlight 3 does not support what you saw in WPF. Specifically, full Commanding support. Silverlight 4 has included Commanding for Buttons, which is a start.
You can look into other frameworks that provides this feature set, like Prism. This will get you along the right path, but I'm sure there are other options.
You can follow the MVVM pattern for Silverlight 3 applications without using a framework. However, it is much easier to use one of the many MVVM frameworks out there. Here are a few good ones:
MVVM Light
MEF
Simple MVVM
I'd recommend not using Prism as it is more complicated than it needs to be.
Other options include things like MVVM Light, which is lighter weight alternative to Prism for things like handling events and commands, however Prism does have a lot of functionality (and the Prism 4 talk has that sounding like a good option when it gets released).
One good community site to check out is the Composite Development Patterns Community, which was newly started but has an excellent goal of collecting articles and other help for working with development patterns like MVVM.
Lastly, John Papa gives a great overview of MVVM, limited to links so head to Google and look up "John Papa MVVM". :)
One thing to consider though, resources for SL3 aren't going to be as valuable as those for SL4, which opens up additional options for framework based commanding (ICommand that greglev mentioned), although it isn't still the full command support from WPF.
Jesse Liberty has a good primer on MVVM also using Behaviors for wiring events.
Silverlight and MV(VM) Pattern - No Code-Behind - Is this possible?

What WPF frameworks should I use?

I'm a newbie to WPF and I'm developing a brand new windows desktop application and I would like your opinion on what WPF framework I should use. I know this question has been asked before, but the last question was asked at least several months ago. A lot has changed since then.
Right now, I'm using MVVM Light which is more of a library than an actual framework, as the name suggests. I’m looking for something more comprehensive. I prefer a framework that I can use on future WPF projects. Consequently, it should be general purpose and productive.
Any insights or suggestions?
If you are a newby I would suggest that you try to use PLAIN WPF first to get a feeling for it instead of right away extending WPF with other frameworks.
Your title is a bit confusing. WPF is just a graphics/UI layer. WVVM is a Model View ViewModel framework, for implementing a MVVM pattern using WPF ( or Silverlight ).
So, are you asking what design pattern you should use for your WPF application? ( As in MVC, MVVW, MVP, etc... ) Or are you asking which MVVM frameworks are compatible with WPF?
Ugh, acronym soup.
As to my answer, if you are new to WPF and want to learn WPF, I would start with no framework, you can always refactor later once you have a good solid grasp of the underlying technology.
It's definitely on the heavy weight end, and plenty of people will warn you about it, but you seem to be asking for CAG
http://compositewpf.codeplex.com/
It's a pattern that comes with a sample implementation that you can bend into your own. For example, it comes with Unity for IoC, but you should be able to put in another IoC container.
The download comes with a lot of samples for both Silverlight and WPF.
Caliburn (http://caliburn.codeplex.com/) handles a lot of shortcomings of the WPF inherent model (like writing a ton ot stupid routing code for events) and introduces MVVM ;) My bet ;)
You might have a look at the WPF Application Framework (WAF). The sample applications show:
View composition
UI Workflow (Wizards)
Command binding / Shortcut Keys
MVVM pattern, Unit Testing
Validation
Entity Framework
Open/Save FileDialog
Print Preview / Print Dialog
Localization

Wpf Composite application

I am new to WPF application and are developing a new WPF application which does 4 different things (4 different modules).
I was googling around and found regarding WPF composite application.
I was reading through the msdn articles but and a few a different place but all the apps are very large and so understanding them is a bit difficult.
Has anyone got a very simple WPF composite application or any link where i can find it out?
Your question is very broad, but I'll try to give you a few tips.
This is just my $0.02USD since I've reacently approached a similar challenge.
First thing, don't approach this as a WPF composite application, think of it as a composite application that you wish to reflect onto a WPF window. There's a big difference. Each module of your composite should be a self contained, and fully functional unit -- later you can tie some of the functionality to WPF controls. If you try to design this from the ground up to work with and only with a certain WPF interface, you're entering a world of pain with difficult refactoring and untestability.
Research the Model-View-ViemModel (MVVM) approach to WPF application design. For each of your modules create a ViewModel -- which is an adapter class that exposes the functionality of your module ("Model" in MVVM) to a WPF control ("View" in MVVM).
I would suggest you do something like the following:
Design independent classes for each
of your 4 modules.
Create 4 Visual Studio "Test
Projects" that test each method of
your modules.
Create 4 ViewModels that instantiate
a single reference to your modules
and exposes their functionality,
even if these ViewModels seems
redundant at first.
Create 4 WPF UserControls that
instantiate your ViewModels.
Research WPF data binding and have your UserControls access your modules through and only through their respective ViewModels.
Each of these steps are very broad and will take lots of work, but there are plenty around here that will help you each step of the way once you get into the specifics :D
Good luck!
WPF/Silverlight - Prism - Resources for beginners
I started developing a small app using WPF and PRISM. The msdn article helped me and the have look at the above stackoverflow question..

Sample WPF apps for line of business apps?

I am learning WPF and wanted to look at source code to some good full fledged WPF apps which are used for business apps, data binding using data bound controls like grids and good use of WPF design and patterns.
Not interested in WPF apps which show off WPF's graphics capabilities. Yes I am into your typical boring business apps. Also looking further than codeproject.com. If you know something good at CodePlex, a pointer would be helpful.
Any recommendations?
CodeProject.com is a very good resource for finding examples of .NET applications. Some articles are at a "here's a control, here are some example usages", but there are the occasional full-fledged app write-ups too.
Here are a couple of examples of some business-y WPF usage which may fill your needs:
WPF Business Application Series Part
1 of n By Karl Shifflett
WPF DataGrid Practical Examples By
Colin Eberhardt
Binding and using Friendly enums in
WPF By Sacha Barber
You can find many more in the WPF section.
The Patterns and Practices group at Microsoft has published a Composite Application "guidance" that is code-named Prism. It includes a demo stock-trading app and source code with examples of various design patterns. It's here on Codeplex.

Should I use the Model-View-ViewModel (MVVM) pattern in Silverlight projects?

One challenge with Silverlight controls is that when properties are bound to code, they're no longer really editable in Blend. For example, if you've got a ListView that's populated from a data feed, there are no elements visible when you edit the control in Blend.
I've heard that the MVVM pattern, originated by the WPF development community, can also help with keeping Silverlight controls "blendable". I'm still wrapping my head around it, but here are some explanations:
http://www.nikhilk.net/Silverlight-ViewModel-Pattern.aspx
http://mark-dot-net.blogspot.com/2008/11/model-view-view-model-mvvm-in.html
http://www.ryankeeter.com/silverlight/silverlight-mvvm-pt-1-hello-world-style/
http://jonas.follesoe.no/YouCardRevisitedImplementingTheViewModelPattern.aspx
One potential downside is that the pattern requires additional classes, although not necessarily more code (as shown by the second link above). Thoughts?
I definitely think you should use the MVVM pattern for Silverlight applications - and one of the benefits of the pattern is that you can actually make your application really blendable through some simple techniques. I often refer to "blendability" as "design for designability" - that you use certain techniques to make sure your application looks great in Blend.
One of the techniques - like Torbjørn points out - is to use a dependency injection framework and supply different implementations of your external services depending on wether the code is being executed in Blend or in the Browser. So I configure my container to use a dummy data provider when the code is executing in Blend, and that way you get design time support for your list boxes, data grids etc.
The challenge is often how to set the DataContext declaratively - so I often end up using a service locator class a a "front end" to the IoC container. That way I can bind the data context to a property on the service locator.
Another technique is create some kind of ObjectDataSource control (non visual) that has two properties: Design Time DataContext and RunTime Data Context. The control does the job of detecting where is being executing, and then setting the Parent DataContext to the right object.
I'm not sure if I can answer your question, but I have foudn the article below very valuable. Jonas Follesø is using ninject to switch out his services when in design/blend mode. Very nice!
http://jonas.follesoe.no/YouCardRevisitedImplementingDependencyInjectionInSilverlight.aspx
I also agree with Jonas regarding MVVM with Silverlight. I do believe that MVP is also a good choice, but recently I have had time to try both MVP and MVVM with Silverlight and I am much happier with the results from MVVM. (Yep, I changed my mind the more I used MVVM). The VM abstracts the binding of the Model from the View (obviously) in MVVM which allows for more binding scenarios (at least cleaner ways to do them) than with MVP. That's just one aspect, though.
I'll be posting some examples of both MVP and MVVM with Silverlight on my site.
I've tried a few options and I'm settling on MVVM as the best choice for me. Blendability is an important point, and I also find the VM aspect intuitive for rigging up dynamic behaviors and procedural effects and animations (like Nikhil's Silverlight.FX). At one point I tried to avoid Blend altogether through fluent interfaces but am finding the coupling between UI and behavior too painful in the long-run. I want to design my UI in Blend and then add effects and other behaviors in code, this is proving to be the best pattern for me to follow so far.
I think many of us are waiting for the trailblazers to go ahead and create really good sample apps using MVVM in Silverlight (and WPF for that matter). There are a number of tricky areas such as the lack of ICommand in Silverlight, or the difficulty of interacting with animations starting and stopping only using data binding.
Its definitely a pattern to watch for the future though, and is worth trying out if you don't mind 'cheating' occasionally in the places where you can't quite figure it out.
I love the ViewModel pattern and highly recommend it. I have a couple of "getting started with ViewModel" types of posts on my blog.
ViewModel Pattern
HelloWorld.ViewModel
Binding Converts - VisibilityConverter
Silverlight Airlines with a ViewModel
I agree with Jonas. MVVM seems to be the model that works best for me (though John Papa thinks MVP makes more sense). I have an MSDN Article on this coming out in March that hopefully will answer the call for a good example.
BTW, I would like to see some cohesion in the MVVM Framework department. There isn't a good solution for a framework to follow yet. I like Jonas' (I think Jonas' is the FX Framework) but since its not WPF compatible it might not be the right choice for some.
I've been using MVVM lately on a couple of different Silverlight projects and it's been working really well, I would definitely recommend it. Jonas's post is a great place to start, I've recently blogged on my MVVM experiences too and created a really simple solution to demo the main touch points.
There's a very good Techdays 2010 video introduction to the MVVM pattern, clearly explained:
TechDays 2010: Understanding the Model-View-ViewModel pattern
Hanselminutes podcast by Laurent
For more complicated applications that require a higher degree of automated testing it definitely makes sense, and the move away from DependencyProperties to DataContext binding is a lot neater than it's ASP.NET counterpart.
The biggest challenge I've found with Silverlight is testing the actual UI (there's one commercial framework so far I think), and the huge tangle of event calls you get into when using WCF services (or the WebClient for that matter) with Silverlight.
I've always thought MVVM and PresntationModel http://martinfowler.com/eaaDev/PresentationModel.html are essentially the same thing. PresentationModel is a lot easier to say.
I've used it succesfully in java swing, windows forms, WPF and silverlight. If you think in terms of separation of concerns a Presentation Model makes a lot of sense. You have one class whose only concern is providing a Presentation friendly Model.It really doesn't matter what technology is used to show it on the screen. It might change some implementation details but splitting the concerns apart is a good idea no matter how you show the information.
Because of that separation you can easily write tests against your presentation model regardless of the view technology. So that's a plus.
With the Feb 2009 release of Prism v2 by P&P, even better support for MVVM now available for Silverlight and WPF. See microsoft.com/compositewpf for more details.
Take a look at my article about MVVM and Silverlight in real life projects and decide for yourself.
http://alexburtsev.wordpress.com/2011/03/05/mvvm-pattern-in-silverlight-and-wpf/

Resources