One of the most highly anticipated of the new technologies coming in .NET 4.0 and Silverlight 4.0 is the Managed Extensibility Framework (MEF). Folks that are familiar with Prism and that have been looking at MEF have been wondering how Prism and MEF relate to each other. At a high level, MEF shares some of the same goals as Prism in terms of supporting easy to extend applications, and this has led to a lot of questions on how MEF and Prism relate to each other and about the future direction of Prism once MEF is released next year. Hopefully this post will go a long to clearing up the confusion and give you a sense of how we think Prism and MEF will work together.
First of all, it’s important to note that Prism and MEF are very much complementary technologies. MEF allows you to create extensible applications by providing support for automatic component discovery and composition. It does not focus on application- or UI-level patterns or on any specific UI technology. Prism on the other hand provides support for creating modular composite client applications built on Silverlight and/or WPF. As such it supports a wide range of patterns including the higher level UI patterns and concepts that are need to support composite clients – such as modularity, module deployment, loosely coupled communication, commanding, and separated presentation patterns such as Model-View-ViewModel, etc.
There is clearly some overlap between MEF and Prism (since modular composite client applications are by definition extensible!!) but MEF by itself in no way replaces or obviates the need for Prism – they are each targeted at different parts of the overall puzzle. MEF in fact is fantastic news for Prism, because it means that we can now leverage core capabilities that are provided by the .NET Framework, whereas in the past we’ve had to build those capabilities as part of Prism itself.
In the next version of Prism (Prism 4.0) we’ll be focusing on leveraging MEF as much as possible and ensuring that Prism and MEF work seamlessly together. We’ll be kicking off the Prism 4.0 project early next year and, as always, we’ll be providing drops every two weeks during development so you’ll be able to provide feedback and direct the scope and focus of the project as we go. But since MEF integration is likely be one of the first things that we’ll tackle, I think it’s worth describing our current thinking on how we think Prism can leverage MEF and how they can complement each other so that you can provide feedback to us before we start. In the rest of this post I’ll outline a couple of the areas where I think MEF and Prism can be closely integrated. Let me know what you think.
Firstly, let’s talk about component composition. Prism uses the Dependency Injection pattern (DI) quite extensively because it helps to maintain loose coupling between the various components in an application. I’m not going to get into the “is MEF a DI container or isn’t it” discussion here :-) but suffice it to say that there are similarities and differences between the way MEF works and the way that typical Dependency Injection containers work. The upshot is that there are things that MEF does well and there are things that DI containers do well, but the important thing is that both MEF and DI containers promote loose coupling between components, which is a very good thing.
In the Prism 2.0 Quick Starts and the Reference Implementation we use the Unity Application Block as our DI container of choice. However, it’s important to note that the Prism code libraries themselves do NOT depend explicitly on Unity, or in fact on any specific DI container implementation. This is an important point. It was an explicit design decision to allow the use of any other DI container (such as CastleWindsor, StructureMap, AutoFac, Ninject, etc) or the use of any other kind of component composition strategy that suites your needs – such as the component composition strategy supported by MEF!
So the design of Prism allows you to use MEF with Prism 2.0 today, even though Prism 2.0 was shipped early this year way before MEF was broadly available. It’s pretty easy to use MEF instead of a DI container within a Prism module to help you wire up Views, ViewModels, Controllers, or Presenters, or to get references to EventAggregator or RegionManager instances provided by the Shell. In fact, using MEF like this can actually simplify the code somewhat since it can automatically discover available types without you having to register them explicitly with the DI container. You can also use MEF’s meta-data support to do conditional component composition. We’ll be exploring and providing guidance for these scenarios in Prism 4.0.
Another interesting possibility is to use both MEF and a DI container together in the same application. There are some advantages to this since it allows you to leverage the strengths of each in various situations. In fact, Unity 2.0 (which is currently in development as part of EntLib 5.0) will support the hybrid use of MEF and a DI container and make it seamless and straightforward. In Prism 4.0, we’ll be exploring and providing guidance for the use of MEF + DI container hybrid approaches in the context of composite client application development.
The use of MEF and/or DI container to help with component composition is pretty straightforward, and largely independent of the higher level constructs and patterns in Prism. But one area where the overlap between MEF and these higher level Prism concepts is not quite as clear cut is in the area of Catalogs and Modules. This area has caused some confusion in part because of overlapping terminology and, in some cases, because of the similar concepts applied at different levels of granularity.
To recap, Prism uses the notion of a module to represent a collection of features, components, and resources that make up a functional unit within the application. Prism modules can be developed, tested and deployed independently. They frequently consist of multiple assemblies and other resources packaged up into an easily deployable unit (for example in a XAP file). Prism provides infrastructure for the flexible deployment of modules. In the case of Silverlight, for example, modules can be downloaded with the core application, or can be downloaded in the background or on-demand.
Prism applications use a module catalog to tell it which modules to load. Module catalogs can be static (i.e. the list of modules is predetermined before run-time) or dynamic (i.e. the list of modules is determined at run-time and can take into account the user’s ID, role, configuration, installed modules, wind direction, etc). Dynamic module catalogs can be used to extend an application after its initial deployment. They can be implemented in many different ways. For example, you can build a dynamic catalog by reading a (local or remote) config file, or by using a web service that specifies the modules to load, or by scanning a particular folder to see what modules have been installed.
Once a module is loaded, Prism looks for an entry type that supports the IModule interface. This interface has only one method – Initialize – and can be used to provide any initialization logic that the module provides. Typically, this method is used to register types within the module with the DI container so that they can be accessed by other modules and the shell. Another common use of this method is to register Views contained within the module with UI regions. The module can do anything it needs to though, including kicking off background threads, making web service calls or downloading additional data or resources. Of course, you don’t have to provide any initialization logic if your module doesn’t require it.
Now, MEF too has the concept of a component catalog. When you import a type in MEF (i.e. you’ve expressed a dependency in one class to another class of a certain type), its resolves that type by looking in the MEF catalog which defines all of the types that are available for export (i.e. types that can be used by MEF to satisfy imports). This is a very powerful feature of MEF and allows you to express dependencies between components and have them fulfilled for you pretty much automatically, without having to explicitly register types with a DI container or having to resort to closely-coupled techniques like ‘new’ or CreateInstance. Note, I say ‘type’ here to keep things simple but MEF generalizes the relationship between importer and exporter using contracts, but contracts most often equate to types, and types can be interfaces.
So Prism module catalogs are really not the same as MEF catalogs since they’re specifying things at completely different levels of granularity. In addition, MEF does not provide any support for the packaging or conditional deployment of functional units within an application. However, there is clearly some overlap here since, at a lower level, Prism modules essentially contain a bunch of components that can import and export types so that they can be hooked up to components from other modules or the shell.
You can imagine using both Prism module catalogs and MEF catalogs together. The key here I think is for Prism to focus on the packaging, deployment and activation of coarse grained functional units (modules), as defined by either a static or dynamic module catalog, and to use MEF to support the flexible, meta-data driven composition of components within and across modules and the shell.
For example, imagine using Prism to build modules that represent functional units within an application and having it take care of their packaging and deployment via a dynamic or static module catalog. As each Prism module is loaded, the component dependencies within it (i.e. imports and exports) can be made available to MEF for component composition. In a sense, each module represents a catalog of components that MEF can use to fulfill and resolve component dependencies. There are other possibilities I am sure, and we’ll be exploring these as part of Prism 4.0…
Our goal for Prism 4.0 is to leverage MEF, and other .NET 4.0 and Silverlight 4.0 features, as much as possible so that we can provide a flexible and powerful environment for building modular, composite client applications on WPF and Silverlight 4.0. MEF is an exciting new technology and it’s great to see some level of support for loosely coupled component composition finally make it into the .NET Framework. I’m looking forward to exploring the possibilities here and I think it’s going to be pretty cool…
As well as MEF integration, for Prism 4.0 we’re looking at also providing more guidance on the ViewModel pattern, navigation, out of browser applications, developer/designer workflow, user experience patterns, visual studio templates, and a whole host of other areas. Which ones we tackle will depend on you, so as always, if you have any ideas or feedback, please let us know!
As you continue to enhance Prism I hope the team won't lose sight of the need to keep the framework footprint small and make it easy to understand and use.
Absolutely Terry, on both counts. A benefit of leveraging features in the platform is that we can remove stuff from Prism, to either make it smaller or make room for more Prism features. Our goal is to keep the size of Prism about the same as it is today though. We're also looking at simplifying some of the more complex areas in Prism and providing more in the way of Visual Studio templates to make development easier and faster.
Nice post David. We're excited to see several p&p deliverables integrating/supporting MEF where appropriate. We look forward to the possibilities!
>>You can imagine using both Prism module catalogs and MEF catalogs together. The key here I think is for Prism to focus on the packaging, deployment and activation of coarse grained functional units (modules), as defined by either a static or dynamic module catalog, and to use MEF to support the flexible, meta-data driven composition of components within and across modules and the shell.<<
David, this was the best blog that brought Prism and MEF together and made a clear black & white distinction between the two. I'm very much looking forward to this next generation of SL4/Prism4/MEF/RIA Services.
Why are there so many different directions Microsoft is pushing for WCF\Silverlight Business Applications?
Why is there a Silverlight Business Application project type in Visual Studio 2010 with absolutly no talk of the Prism project, yet it uses a View-Model-ModelView?
We need to be given better direction as to where Microsoft is going with its overall P&P's so we know with certainty a direction to go in.
I work for a large oil & gas producer; 3 different developers I work with looked at Prism and "ran for the hills"! I think Microsoft needs to understand that us developers that are swamped with our day to day activities find Prism overwhelming, one of our top project managers commented that these things only work well "in a vaccuum" (not in the real world).
Our architects have not yet suggested the use composite applications (yet we have more than 1500 apps company wide and over 8000 employees).
I am unsure of using Prism, as I prefer the WCF RIA LOB App's simplicity. I guess I must wait for Prism v4 to decide if I should look in that direction again because the whole Enterprise Libray thing is just too darn bloated.
I wouldn’t necessarily say that that Microsoft is pushing in different directions for Silverlight business applications, but it is definitely true to say that overlapping technologies, that each focus on different aspects of Silverlight application development, can make it very difficult to see the common elements and how they all fit together. Each technology focus on different aspects of the application – MEF focuses on loosely-coupled meta-data driven component composition, while WCF RIA Services focuses on building and consuming data services within a Silverlight application, whereas Prism focuses on application design patterns – and all of these technologies can be used together very effectively.
But there are some overlaps. The fact that the MVVM pattern is used within RIA Service apps illustrates this I think. Bad naming and terminology doesn’t help either. RIA Services choice of the name ‘Business Application’ is unfortunate because not only business apps need data services, and other Silverlight application types can be used for business apps… :-(
The goal of P&P is to provide guidance that shows how to build applications that leverage the various Microsoft technologies and have them work together. Prism sits above the individual technologies in the platform and provides patterns that helps you to integrate them. Prism will evolve to leverage new platform features like MEF and RIA Serves as they become available. One of our primary goals for Prism 4.0 is to make it easier to use – including using Visual Studio Project templates – but Prism is complementary to these technologies so it won’t show up in the project templates of individual technologies.
Prism has been very successfully used on many ‘real-world’ projects by many different customers all over the world. In a way, the patterns that Prism embodies (modularity, separated presentation, loose-coupling, etc) are overkill for small scale applications, so the more ‘real-world’ the application is, the more benefit you’ll get from using Prism. These patterns can help you manage complexity in your application, or portfolio of applications. It sounds like you have 1 app for every 6 employees which, while not atypical, can be costly to maintain and evolve. The patterns in Prism can help manage that.
The application architecture you get with WCF RIA Services project template is simpler because it does not focus on composite applications. You can of course use Prism with RIA Services (see my post on that), but it really depends on your requirements and whether your apps can benefit from the patterns in Prism.
Great to see this "meeting of the minds" between prism and MEF as they are both strategic to my way of thinking. however I too echo what jason is saying as the Silverlight options are now a little confusing however I disagree that Prism is hard to use as there is no requirement to use all the parts only what u need. I hope that there is more than guidance in how to put all the players like WCF Ria and Navigation Framework together with Prism/MEF. I know that there are blogs on how to integrate Prism with navigation framework but I believe there should be a "cleaner" solution out of the box. Also most seem to ignore the fact that many of us have to worry about Blendability as we must deliver a great UX and MOST developers are not graphic artists. It is the combination of great UX and architecture that sells software!
Thanks jmcfet. Prism 4.0 will focus on how to integrate these new technologies to form a clean application architecture. We're also planning to provide a baseline solution template that has all of the pieces nicely integrated, allowing you to start implementing your application right away.
Blend and Ux patterns are another area that we'll be looking at. Using Blend to create and edit views in a Prism MVVM architecture is specifically something that we'll be working on. The key here is to make it really easy for a UI designer to hook up the ViewModel - we'll be looking at using Blend behaviors to help with this. Out of the box UI patterns are another area that we'll be looking at.
>> It is the combination of great UX and architecture that sells software!
Absolutely! Couldn't agree more!
Really, the only thing that gets me down about CAB/CAL/PRISM is the fact that a Module has to know everything about the Shell in order to integrate correctly into it. What I mean is that we need a better way for the "shell" to configure which Regions modules belong in. How did the responsibility of the UI (e.g. Shell) end up in the Module? That makes no sense. I currently have to have a seperate config file which redundantly defines the Modules and tells the shell which Region to load them in. This has also helped with Dynamically loading modules on demand because now the Shell can interrogate whether the module in the newly loaded Region has been loaded before or not and load it on demand. It's nice. However, not many developers ever see that they can do this because they're so caught up with the online examples that always show a Module doing something like "regionManager.RegisterViewWithRegion(...)". It's really not intuitive and tightly couples Modules to one Shell. Which the methodology I hinted to above, I could use the same Module in many different Shells. Why can't the ModuleCatalog.xaml just support the markup to do something like that?
Just my 2cents.
Is there any guidance on using Prism with MEF now? I am starting a .NET 4.0 project. I want to move to Prism 4 when available.
Hi tymberwyld – I’d like to more fully understand your scenario here so we can hopefully support it better in Prism 4.0…
Prism today provides a way to associate views (defined in individual modules) with regions/placeholders that are defined in the Shell (or in sub-views). Virtually anything can be a view and you can make any layout control a region. This means that you can separate _what_ gets displayed (the view) from _how_ it is displayed (its layout). The view-region coupling mechanism is currently based on a simple string that denotes the region name. The shell only has responsibility for laying out views; the modules only have responsibility for defining the views. The region name is the only piece of information that the modules and shell have to share.
This means that you can switch layouts and views independently. So for example, if you have a shell that displays a view from module A on the left and a view from module B on the right and you want to switch them around, you can simply change the region names in the shell. You can even do this dynamically (at run-time) or make it configuration driven (e.g. user preferences). In other words, the association between views and regions can be direct (regions names specified in the shell and modules) or indirect (via a configuration file or service that maps available regions to available views) in a more dynamic way. The former is the default in Prism, but we tried to make Prism extensible so that you can pretty easily do the latter as well.
Your second scenario is also interesting. By default, when a region is shown Prism’s region manager will look for views to display in it (i.e. views that were registered by individual modules when they were loaded). In your case, though, it sounds like you want to load modules on demand when specific regions are shown. Today, module loading is not tied to the display of regions – this was not the default scenario that Prism was designed to address out of the box – but hopefully it’s extensible enough to allow you to do what you want easily.
For Prism 4.0, we’re going to look at extending the visual composition mechanisms and patterns (i.e. how views are mapped to available regions and vice-versa). One possibility is that we’ll leverage MEF to help with this. MEF is a component-level composition framework, not a UI framework, but regions and views are really just components looking to be introduced to each other in a specific way. It's likely that we’ll keep module loading separate from visual composition, but maybe using MEF will help to make Prism easier to extend to support your scenarios…
Hi Geoff - We don't have any specific guidance available today. We're going to produce that as part of the imminent Prism 4 project. But probably the best way to think about using MEF in Prism today is to use it to help wire up components (services, viewmodels, models, etc). There are quite a few folks in the blogosphere who've successfully used MEF and Prism in this way. How MEF helps with modularity is something that we’ll be looking closely at in Prism 4.0…
Thanks for this post David, it helps to understand the direction you guys are taking.
I didn't have a chance to play with Prism in its current state, however in my company we built all our systems on top of CAB/SmartClient Software Factory. On this end I wanted to ask you what is the direction on providing Software Factories for Prism 4.0, I can tell you that there is a great value on it, and it helped us a lot in particular aligning the development team on the same direction.
You made this post in December, what is the current state of envisioning/development for Prism 4.0? Any schedule you can share as far as when is going to be made available to public?
Can anybody suggest me a way for injecting the CAB Application Shell at run-time in Microsoft .NET Smart Client (CAB - Composite Application Block) GUI
Applications using Unity Application Block ?
Is it possible ?
Any help will be appreciated.
I would love to see the capability to load modules in different AppDomain for :
- Limiting the impact of a module crashing
- Be able to run a module with different credentials
- Be able to have different configuration file per module
What do you think about this ?