kick it on DotNetKicks.com   Shout it  

MVVM with Prism 101 – Part 1: The Bootstrapper

Source Code

I recently spoke at a CodeCamp put on by the Northern Utah .NET User Group (NUNUG) on implementing MVVM using Prism. I hadn’t spoken in a long time and so I was over prepared – way over prepared. Then to top in off in my nervousness, I blew the whole presentation by starting 15 minutes late. I was the session after lunch and I assumed lunch was an hour, so I mistakenly assumed my session started at 1:00 PM. :p

Embarrassing stories aside, I really learned a lot more about Prism and Silverlight and what an enterprise-class implementation of Prism looks like. So I am writing a redux of my previous post on Prism and Silverlight. If you want a shorter overview of things, read that one first. However, I intend to introduce new concepts and go deeper than I did in my previous article. This is the first of this series.

MVVM

Before I get started I would like to just make a short comment about MVVM. The pattern known as Model View View-Model is in it’s pure form very simple. You have a Model (your business/domain model), a View (in the case of WPF/Silverlight is your Xaml file) and the distinguishing piece of the pattern: the View Model.

The View-Model is a composite of the abstraction of you view’s state and behavior. The idea behind the View-Model is to create an abstraction of your view which has little to no dependencies on your view. There should be no references to UI controls or classes specific to the UI (like the Visibility enum). There should be no event handlers like what is common in a code-behind class. Your View-Model should know nothing of your UI.

The reason for this and why it is possible has a strong tie to the way data biding works in WPF/Silverlight. I won’t directly get into the specifics of data binding, except to say that because of the specific way you can bind UI elements to objects in a strongly-typed manner MVVM has become the de facto standard pattern for developing applications for WPF/Silverlight.

Once you get MVVM it will become second nature to you. Its power lies in its simplicity. The complexity of implementing MVVM is more about the framework you are using than actually using the pattern. I will explain more about that last statement in a future post. I when I actually get to discussing MVVM specifically I plan on demonstrating what I’d like to term “poor man’s MVVM” so I can demonstrate the MVVM in a more pure form. The rest of the series is either specific to Prism or specific features you’ll want to implement as you’re developing your application.

Bootstrapper

The Bootstrapper is your starting point when developing any application for Prism (aka Composite Application Library for WPF/Silverlight). The Bootstrapper is basically class representing your App_Startup method. Once you’ve completed your Bootstrapper you just initialize it and call Run() inside of App_Startup, like this:

   1: private void Application_Startup(object sender, StartupEventArgs e)
   2: {
   3:     //replaces the call to "this.RootVisual = new Shell();"
   4:     MyBootstrapper bootstrapper = new MyBootstrapper();
   5:     bootstrapper.Run();
   6: }
   7:  

Just remove the call (in Silverlight) to this.RootVisual and replace it by instantiating your Bootstrapper and calling Run().

The basic purpose of your Bootstrapper is to initialize your Inversion of Control (IoC) container and register all the types you’ll need for Dependency Injection (DI). Because of this your Bootstrapper will inherit from an abstract class named after your specific IoC implementation. You can use any library you choose: Ninject, StructureMap, Unity, etc. However, Prism actually only provides a single implementation of Bootstrapper – UnityBootstrapper. If you want to use another container library, you’ll either have to write your own or find an implementation out on the web. This isn’t hard to do as there are a number of implementations out there for each of the widely used libraries.

There are 3 common tasks that you’ll do every time your create a new Boostrapper:

  1. Setup ModuleCatalog
  2. Configure the Container (IoC)
  3. Create Shell

I’ll go over each of these in detail:

Setup ModuleCatalog

A module (in terms of Prism) is a class which implements the Microsoft.Practices.Composite.Modularity.IModule interface. A module represents a pluggable piece of the composite whole. When planning your application you will isolate it into logical parts. If your application were an rss reader you might have modules for Content, Tags, PopularItems, RecentItems, Favorites, etc. They may or may not need to communicate, but you can certainly add , remove and rearrange each one without affecting the others. This is an example of a module.

Think of ModuleCatalog as a registry of all the modules you plan to load in your application. Once you register them when your application loads they will be loaded in the order you registered them and then each module in turn will inject your views into the main window of your application.

Setting up ModuleCatalog is simple:

public class Bootstrapper : UnityBootstrapper
{
    protected override IModuleCatalog GetModuleCatalog()
    {
        ModuleCatalog catalog = new ModuleCatalog();
 
        catalog.AddModule(typeof(ServicesModule));
        catalog.AddModule(typeof(EditorModule));
        catalog.AddModule(typeof(HistoryModule));
 
        return catalog;
    }
}

For the most part, that’s all that’s required. At the very minimum you must return an instance of ModuleCatalog – whether or not you register anything with it. But if you don’t you aren’t likely to see much more than just your shell when the application loads. So you’ll register each module you’ve defined and possibly declare any dependencies (more on that when we discuss modules) and finally you’ll return the catalog to the Bootstrapper.

It is important to note that the order you register your modules is the order they will load in, so pay attention or you’ll have some troubles when your application starts up. I’ll address managing dependencies between modules later in the series when I talk about implementing modules.

As a note I will mention that there are other ways to configure the module catalog and to load modules. Some are dependent upon whether you’re using WPF or Silverlight, but both technologies provide some way or another for you to load modules dynamically or on demand or to use configuration of some sort (WPF – config file / Silverlight app.xaml). But I won’t be going into any of these methods in this series.

Create Shell

The shell is comparable to an ASP.NET MasterPage. The shell is usually a UserControl or Page.  Whereas a MasterPage uses ContentPlaceHolder objects, Prism uses Regions (more on that in my next post). For now, a region is an attached property for specific xaml objects which designates where you can inject your views to display them.

When it comes to Silverlight applications, an important thing to note about the shell is that it will become the RootVisual object. If you aren’t familiar with RootVisual, it is important to note that it can only be set once - you can’t change it once it has been set. But here’s the nice thing about using Prism, the regions you define later on allow you to dynamically replace or load additional content as you need. So your shell will provide the placeholders for you.

Other than that all you really need to remember is to initialize the class which represents your UserControl or Page (the code behind), set it as the RootVisual (call Show() if you’re developing for WPF) and return the instance from your method. The Bootstrapper implementation you inherit from will then register any regions you’ve defined in your xaml with the RegionManager service. Here’s what your CreateShell method should look like:

protected override DependencyObject CreateShell()
{
    Shell shell = Container.Resolve<Shell>();
 
    Application.Current.RootVisual = shell;
 
    return shell;
}

Conclusion

I will continue this series next time by talking about the Shell and Regions. For now, here is a sample project which demonstrates what we’ve just discussed.

tags: , , ,

kick it on DotNetKicks.com   Shout it  

Feedback

# MVVM with Prism The Bootstrapper

Gravatar MVVM with Prism The Bootstrapper 10/5/2009 8:11 AM | Pingback/TrackBack

# MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar MVVM with Prism 101 – Part 1: The Bootstrapper 10/5/2009 8:13 AM | Pingback/TrackBack

# Silverlight Cream for October 04, 2009 -- #704

Gravatar Silverlight Cream for October 04, 2009 -- #704 10/5/2009 8:14 AM | Pingback/TrackBack

# MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar MVVM with Prism 101 – Part 1: The Bootstrapper 10/7/2009 9:36 AM | Pingback/TrackBack

# MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar MVVM with Prism 101 – Part 1: The Bootstrapper 10/7/2009 9:37 AM | Pingback/TrackBack

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Mark,
How integrate SL3 (Prism 2.0) solution with MOSS Web Application?
Andrzej 10/8/2009 2:30 PM | Andrzej

# # re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Andrzej, sorry but I can't answer that one for you. I've never used MOSS.

However, Silverlight is independent of what you're using as a web application platform. You could host a silverlight application on an apache web server and communicate with a PHP application if you wanted to.

It all really depends on what your silverlight application does, whether or not you're communicating with the server and if you are then how you're communicating with your server.

Finally, Prism has even less to do with the question. Prism doesn't communicate with your web server. Prism is a framework for building a composite application from modular blocks (usually projects). So even though I know absolutely nothing about MOSS I would go out on a limb and say you don't need to worry about it - you can develop your SL/Prism app independently of your MOSS application. If you are trying to communicate with MOSS via SL, you can do that as well but I couldn't tell you how.

Someone jump in if I'm totally wrong here. 10/8/2009 5:42 PM | Mark J Miller

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I've been struggling to learn WPF itself along with the, Composite/Prism and MVVM patterns separately. I hope this is tutorial will (finally) be the one that can tie them together for me!

However, my first live app will likely be WPF, not Silverlight (which will be several months off, if not next year). What changes would be necessary to use your example project in WPF rather than Silverlight ? 2/19/2010 1:26 PM | David

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I'm pretty sure the code will compile and run fine as a WPF app as well. I don't believe I have any dependencies on specific SL components. However, you will need to re-create the projects as Windows Class libraries and the Shell project as a WPF application and make sure to point the project references to the correct .NET Framwork Class libraries instead of the Silverlight (v 2.0.5.0) versions. 3/5/2010 1:22 PM | MarkJMiller

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar The IIS Media team has been plugging away for many months at new server and client technologies, and we are ready to start sharing some of them today! The Beta of IIS Media Services 4.0, which allows you to encode once and deliver to three screens across multiple file formats and protocols 6/24/2010 6:02 AM | ride to lax

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I Like your articles the way u explains. I have really confused with the Attached Behavior properties in Silverlight and how it works with prism. Is there any properties for selectionchanged others. 6/25/2010 5:07 AM | Abgezockt

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I had to read it twice and then it became clear to me. but that's not your fault - only mine. thanks for a great post 6/27/2010 2:14 AM | essay services

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar The question of focus is an important one for any entrepreneur. We hear it all the time – from the idea of having a “core competency”, from implementing “niche marketing”, and even in the oft-referenced “elevator pitch” – getting your message so focused that you can literally give it within an elevator ride. 7/14/2010 12:49 AM | Nishu Gupta

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar The Bootstrapper's Bible has a extremely pragmatic approach to starting a business without capital. Having the discipline to follow the basic recipe outlined in the book could greatly reduce your likelyhood of starting a failing business. Without forcing you to write a huge business plan this book gets you consider the extremely important aspects of a business such as 7/14/2010 2:16 AM | kuber dinesh gupta

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar How long have you been in this field? Certainly, you know a lot more than I do, I would love to know your sources! 7/22/2010 3:03 AM | flex chat

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I am also in this field and I can understand that you have a very in-depth knowledge. Can you refer me some good books from where you got this knowledge because what I have studied till now, were very basic 7/22/2010 5:23 AM | cancer symptoms

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar The Beta of IIS Media Services 4.0, which allows you to encode once and deliver to three screens across multiple file formats and protocols 7/25/2010 5:08 AM | Cialis

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar The complexity of implementing MVVM is more about the framework you are using than actually using the pattern 7/26/2010 6:24 AM | Business Travel Blog

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I loved reading your blog. He was very well written and easy to understand. Unlike the more I read blogs that are not really good. I also found your posts very interesting. In fact after reading, I had to show my friend and he liked too!.... 7/28/2010 4:00 AM | casino en ligne

# My view

Gravatar Highly Recommended which you Post here.
Good Points you wrote in very good summarize way.
Great Show It Was. I want to more and more from you so i will come back soon very often.
8/4/2010 1:34 AM | simulation assurance auto

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Though I would’ve loved it much more if you added a relevant video or at least pictures to back up the explanation, I still thought that your write-up quite helpful. It’s usually hard to make a complicated matter seem very easy. I enjoy your weblog and will sign up to your feed so I will not miss anything. Fantastic content. 8/6/2010 9:18 PM | online casino

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar The Beta of IIS Media Services 4.0, which allows you to encode once and deliver to three screens across multiple file formats and protocols 8/7/2010 10:04 AM | Government jobs in India

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Certainly, you know a lot more than I do, I would love to know your sources! 8/9/2010 5:36 AM | 5s red tags

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar good code it is helpful for me and all users. Specially Setup ModuleCatalog code is very good explained. 8/15/2010 10:00 PM | Yeast Infection

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I also found your posts very interesting. In fact after reading, I had to show my friend and he liked too! 8/16/2010 10:43 AM | 6S

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Slate is running a series this week on the concept of risk, something that every entrepreneur is familiar with. Appropriately, the third article in the series is about a bootstrapping web startup ca... 8/16/2010 10:40 PM | agile informatics

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I just finished reading this and it has been a great insight into growing a business. I am a bootstrapper and the information this gave me it just ahead of where I am in my business so it is perfect timing. It is like talking to a mentor that is giving me the inside baseball of bootstrapping a business..... 8/19/2010 6:40 AM | keno online

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Thank you for your information 8/23/2010 11:38 AM | Farmacia

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Please write more 8/24/2010 6:27 AM | Cialis

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Thank you for your informations , i like the website very much!
MLB baseball Jerseys
jade bangle
baseball jerseys
penguins jersey
mlb jerseys
R4i
tiffany jewelry
Nhl hockey jerseys
chanel jewelry
nfl football jerseys
nba jerseys
tiffany rings 8/25/2010 1:51 AM | China travel

# My view

Gravatar Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share. 8/25/2010 2:57 AM | world of warcraft account

# unsecured small business loan

Gravatar Great post! It is very useful for me. 8/26/2010 3:23 AM | robseller81

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar These are really basic instructions…but still important.really its very useful and informative.its good tips and easy steps. so thanks for nice post...... 8/26/2010 5:05 AM | best uk casinos

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar Northern Utah .NET User Group is working good. with the help of ASP.NET you create your master page. in this Setting up ModuleCatalog is also simple. 8/27/2010 1:06 AM | phentermine 37.5

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar
I like The Bootstrapper. At first time I do not understand because of some problems. Now, after reading twice, I see all. 8/27/2010 8:30 PM | Phoenix Pools

# jade dragon

Gravatar Thanks for taking the time to share this, I feel strongly about it and love reading more on this topic.
9/1/2010 1:10 AM | jade dragon

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar .NET User Group working very hard and give good result. Just implementing MVVM using Prism. prism is very god technique. 9/1/2010 2:17 AM | poker bonus codes

# re: MVVM with Prism 101 – Part 1: The Bootstrapper

Gravatar I am a bootstrapper and the information this gave me it just ahead of where I am in my business so it is perfect timing. It is like talking to a mentor that is giving me the inside baseball of bootstrapping a business..... Thanks
9/2/2010 1:09 AM | herbal appetite suppressants

Post a comment





 

Please add 4 and 5 and type the answer here:

 

 

Copyright © Mark J. Miller