MVVM Light + Unity or Prism? - c#

I am a little out-of-date in WPF right now and would be interested to hear peoples opinions on the latest version of Prism (which I used a couple of versions ago) vs an MVVM Light + Unity approach (which I have never done - decent examples URLs would be good).
My project will be a large one comprising multiple modules written by several developers. There is also the funds to bring in a 3rd party control suite in order to set up a nice workspace using one of the fancy Docking/Workspace layout managers out there (and I know some play better with the Prism regions than others).
If you were starting a project from scratch right now, what would you go with and why?
Details on specific recommended architecture patterns would be useful (e.g. auto discovery of module dlls? Injecting logging service?).
Basically, any thoughts and advice would be good. I'd like to get a good discussion going. Perhaps there is another direction entirely that you would suggest going in? I'm very much in the research phase and would like as much input as possible.
I put this on here because I thought it would get less biased points of view than the Prism/MVVM Light forums but, if there is a more suitable place for this question, please point me in that direction.

If you need modularity, you'd want to look at Prism. Prism has some elements that can help you with MVVM (DelegateCommand and CompositeCommand, for example), but I think it's more complete with another MVVM framework.
There was a question a few days ago about how to model Prism. Check that out for a detailed explanation of how to consider Prism's functionality.
High Level Modelling Advice for Prism MVVM
Unity is an implementation of an inversion of control container and it's definitely good, but Prism has the ability to use other containers. It has builtin support for MEF (which, in turn, is builtin to .NET 4.0), but it's not your only choice. Look at some of the samples included in Prism and decide what approach you like better. Unity is not complete on its own for UI compositing in my opinion. MEF might be a closer choice if you wanted to try to composite a UI with the MVVM Framework + IoC Framework approach.
MVVM Light is actually a complimentary framework to Prism. Other MVVM frameworks to consider:
MVVM Foundation (very lightweight... good for small projects)
Caliburn (very robust framework)
Caliburn Micro (shares a name and an author from Caliburn, but it's akin to MVVM Light with some nice conventions)
ReactiveUI (Formerly "ReactiveXAML". This is a bit of a brain melter, but if you learn Reactive Extensions for .NET (Rx), this framework is simply amazing... magical in my opinion.)
If I were going to start a new project: I would go with Prism and ReactiveUI.
Prism because you have to have modularity with large projects and I like the ability to remove and add large units of functionality to an app just by deleting or adding DLLs (and you don't have to implement the DLL sniffing feature like you would with just an IoC + MVVM approach). Easier to test, easier to debug, easier to develop separately. Nice all around.
ReactiveUI because with UI programming these days, most of your time is spent managing your time on the UI thread. Blocking is a no-no... users don't want to see a UI freeze; they want to see that animated GIF wait symbol spinning so they know they can work on something else while your data is loading. In addition, so much of the value applications provide these days is taking data from disparate systems and putting them together in the UI... not only will you need a good compositing system (Prism), but you will also need a good MVVM framework that treats asynchronous operations as its bread and butter... ReactiveUI is it.

Related

If I am concerned about memory usage, should I avoid MVVM?

I have a WPF application that should be compact, with minimal possible memory footprint. The GUI is simple from the point of view of flow, few pages and grids, but quite rich from the point of view of graphics, animations etc.
I have experience writing applications with Prism and I like it very much. But it looks too heavy for simple tasks. It is not a LOB application, but I still need testability, GUI/flow/business separation etc.
Should I use MVVM anyway?
I am not sure if the overhead of using the MVVM design pattern is something I should worry about or not when trying to minimize the memory usage of my application.
Yes, use MVVM if you're working with WPF
WPF is designed so you have a UI layer and a data layer, and this suits the MVVM design pattern perfectly. I find it makes the coding and maintenance much faster and easier.
You don't have to use a full fledged MVVM framework, or even any MVVM framework at all. You can pick and choose the parts you're interested in using (a base object that inherits INotifyPropertyChanged, a RelayCommand or DelegateCommand, the messaging system, etc) and drop all the rest. Or you can build your own.
The amount of overhead is minimal and definitely not worth avoiding the pattern for, however some MVVM frameworks do include unneeded features and can cause some overhead, so be sure you only pick the pieces you want out of them.
The point is, use the MVVM design pattern if you're working with WPF. It will make your life, and any future developers that work on the project lives, much easier :)
I really would recommend using MVVM.
We used the pattern in a huge project and it worked well, with the following recommendations:
we use the very useful, and as the name says, light, MVVM Light Toolkit lib
we do not use the ViewModelLocator thing. Too hard to make it work when you dynamically instantiate Views. The good old this.Datacontext = new TheViewModel(); in the code-behind is fine
we do not use the Messenger thing. Too complicated for very little benefits. A good old event fired from the view model is way more simple IMO.
we do not use the EventToCommand thing, which complicates the XAML for nothing in lots of cases.
we do write code behind when the code is purely related to the view. For mouse gesture features (drag & drop...) or other specific UI things, code-behind is fine for control events handling.
First of all: Yes, MVVM is a recommend way of how to architect an C# / WPF application
Second: You can use MVVM with not framework at all, then it is as lightweight as you build it.
Look here for an overview of possible MVVM frameworks:
SO in depth discussion of different MVVM frameworks

MVVM WPF Scientific application with dialogs

I'm moving existing C++/CLI application to C#/WPF. When initial application was written no design was done whatsoever. Application has an ability to load one or more image files (only one image is visible at a time), also there is an option to connect to a microscope and see and save images in real time (images from microscope show at the same image box as the loaded ones). Then, there are a lot of dialogs with different settings. There are buttons on the main view to make different kind of calculations based on currently loaded image files and selected settings.
There is a button to send images loaded to another window which has more functionality - makes a movie from loaded files, calculates statistics etc.
I'm fairly new to C#/WPF world, but want to make a nice design. I did some research and it looks like MVVM is the pattern that is mostly used in WPF. Now I'm reading about PRISM. I more or less understand what's going on with MVVM, but cannot apply its logic to my situation yet. May be I should stick to MVC?-but there are not a lot of resorces on how to use it with WPF... I found examples on where to put dialogs, but don't know where to put my calculations... looks like they don't belong to either of M/MV/V. Also, I cannot understand if I can have only one Model associated with many ViewModels.
First of all, this is common misunderstanding.
MVVM is a pattern just like MVC. There is various frameworks that help you with MVVM. MVVMLight, Caliburn is some of the examples. I think you need to learn what MVVM is and then look for different implementations. Personally I wrote my own base View Model class and that's all to MVVM
PRISM is a framework and actually it has almost nothing to do with MVVM. It does provide INotifyPropertyChanged implementation but that's about it.
PRISM is a big framework, it takes time to learn it. It is not something you can start using tonight. However, it is "a la cart" which means you don't have to use everything it provides and can use only what you need.
The more I learn the more I realize that PRISM solves pretty much all the problems you will have while developing big business application with heavy UI. For me, time invested in learning definitely paid back.
It provides regions, modularity, messaging, IoC and other stuff that needed when you build big application. MVVM looks tiny and easy after you see bigger picture..

ReactiveUI and Caliburn Micro together?

I've been doing some prototype work on a new Silverlight application using Caliburn Micro as our MVVM Framework. The team has generally been happy with it. In order to address some issues with throttling requests to services, it was suggested that I look into ReactiveUI's ReactiveCollections and their implementation of INotifyPropertyChanged.
Does anyone have any experience around using the two together? Since they are both primarily MVVM Frameworks, there's a good bit of overlap, so I wonder if it might be more trouble than it's worth to try and make them work together.
Some of the things we really like about Caliburn Micro are:
The Convention based binding, etc...Very nicely done in our opinion.
Bootstrapping. We like the way this is handled, it's easy to extend when you need to, but the out of the box stuff works well for many of our use cases.
Composition/Screen Management. We really like Rob's notion of conductors, screens, etc. It flows very nicely for us.
The ReactiveUI stuff that has drawn us to it (at least initially).
The Reactive Collections and INotifyPropertyChanged stuff. Particularly
the ability to throttle the reactions.
Reactive's asynchronous stuff seems a bit cleaner to deal with than Rob's Co-routine implementation.
I've barely had a chance to play with ReactiveUI yet, but as I was looking at it and seeing the overlap between the two, I wondered who might have tried to work both into a project. I haven't been able to find anything via Google (which could well be my fault).
I'd love to know if you tried this; what issues did you have? Which parts of each framework did you use and why? Are there any good examples or blog posts out there on using them together?
Cheers,
Steve
Use both! This blog post should get you most of the way there - it's actually very easy to take existing ViewModels that use other frameworks and "RxUI'ify them". This way you can try out ReactiveUI on a single ViewModel without having to pick either Caliburn Micro or RxUI.

Can I use Ninject insted of PRISM? or maybe it sould replace Unity in PRISM?

I'm going to start an pretty large project witch contains 30+ modules, I have goggled almost for a capable framework ? Need some help to find out witch one of these is soutable?
1) PRISM
2) Ninject
3) Unity or MEF
any why?
I'm using MVVM in WPF4.
Thanks
The most appropriate framework will depend on the problems that you are trying to solve. There is nothing to stop you using more than one of these frameworks together if it is appropriate, they are not mutually exclusive.
For example, we started a large scale enterprise composite app and used Prism, then we started to use MEF and slowly phased out Prism as it wasn't necessary to meet any of the business requirements. So now we use a mixture of Unity for our app's main DI infrastructure, and MEF for dynamic discovery of plugins.
I would also strongly recommend looking at something like Caliburn.Micro for simplifying MVVM.

Is MVVM more necessary/important to Silverlight/WPF than MVP to Webforms?

I hope the title reflects what I'm really asking. It seems to me that when people undertake XAML based development whether to use MVVM is not even a question, rather an understood fact. Coming from a background of Winforms and Webforms, I found that we almost never used MVP (for example). It was well understood that the main reasons to go down the MVP path (other than being a purist) is higher level of unit testing, and the ability to share "view logic" with different UIs. As far as the later is concerned, it has never been a requirement in my projects. If anything we might have had several user controls for this purpose.
As far as unit testing, I never found it necessary to test my code behind because it did nothing more than handle UI events and act as a proxy (create instance of and bind) to my business layer. I can understand if people bypass the business layer and create instances of their favorite data access layer directly in the code behind, then apply business logic to it right there, where MVP would be a huge benefit. In my cause I always front the DAL with a BLL, so where is the real "win" here as far as unit testing?
Point being it was never automatic to use MVP. I am now looking at possibly using Sliverlight (for the first time) in a project and I feel like I'm going to be committing a sin if I don't use MVVM.
I know there are a slew of little things that are MVVM specific perhaps that were not a consideration before when I was making the "to MVP or not to MVP" decision. I've read about Blend support (I don't even have Blend), maybe some other important aspects? The bottom line is though, is MVVM really that much more important to Silverlight than MVP was to Webforms for example? Or are we riding the renaissance days of .NET development where patterns and best practices are taking center stage (sort of like Java).
We moved from Vb.Net winforms to C# and WPF/Silverlight under MVVM. It is definitely a change of pace from our adhock coding. I love being able to create multiple views to work with the same view model. Makes it easy to customize screens for our customers that want it without a whole lot of programming changes.
I found these posts in Stack Overflow that might answer your questions:
Benefits of MVVM over MVC
Why MVVM and what are it's core benefits?
MVVM Unique Benefits
Those have some good answers comparing MVP to MVVM.
Hope that helps.
**** Additionally **
If you were referring to Expression Blend, I used blend when I first learned xaml. Now that I know what I am doing in it, I rarely use it unless I need to do something complicated with animation or whatnot. I use visual studio for coding it since, from my experience, Blend is still cumbersome. Don't get me wrong, it's a great program and does animations great, but it's for designers, not coders really.
From my limited experience with WPF and MVVM, my opinion is this:
MVVM is a pattern, but is not the end-all-be-all of WPF. Especially in the case of MVVM "purists." There are times and places for event handlers and code behind, even in WPF. The benefit of the xaml development and MVVM is the separation of logic from design, but at times, you logic involves your design much more directly than MVVM allows.
I guess the main reason why MVVM is so advocated because of the power of DataBinding.
With web forms for example, you will need to create your own interfaces, framework etc... to implement MVP, whereas in Silverlight/WPF, databinding is just there, so whether or not you use it, it is up to you.
In my opinion, I think MVVM simply adds in a layer of testable view logic (ViewModel) instead of like the MVP pattern where your business logic can really be in the presenter itself.
In summary, if it is already there, easy to use, use it.. it is like you have a car, but u can still choose to walk 10miles to your nearest market, or you can drive (most would advocate you to drive).
Thats my 2 cents.

Categories