Wintellect Blogs

Fluent Validation with MEF and PRISM

Jeremy Likness

27 Aug , 2010  

Validation is a topic that comes up quite frequently. One concern is how to honor DRY (don’t repeat yourself) principles and create a framework that is reusable across layers. There are some very robust and tested frameworks out there, but sometimes they might be more than what you are looking for. In this post, I’ll show you how to build a standalone validation library that can be shared between Silverlight and the server, then integrated via MEF and PRISM for fluent validation.

Rules and Violations

First things first. We need to lay down the validation framework. One technique I’ve learned when working with Silverlight is to build your main classes as Silverlight classes first, so they take advantage of the “lower denominator.” Class libraries built in Silverlight are then easily shared on the server – the easiest, “out of the box” way is to create a mirrored .NET project and link the classes.

I suspect when a rule is violated, it might pertain to more than one property, so I created a rule violation to look like this:

public class RuleViolation 
{
    public RuleViolation(string message, IEnumerable<stringproperties)
    {
        Message = message;
        Properties = properties;
    }

    public string Message { get; private set; }

    public IEnumerable<stringProperties { get; private set; }
}

Next, we need to define what a validation rule looks like.… Read more

Wintellect Blogs

Silverlight UI Automation Testing using Prism 4.0

Jeremy Likness

26 Jul , 2010  

One popular gripe about Silverlight has been the lack of integrated testing tools. There are several types of tests you may perform against a software project. Unit tests can be performed with the aid of the Silverlight Unit Testing Framework and automated with a third-party tool such as StatLight.

Automation testing involves hosting the actual Silverlight application in a browser and performing a walkthrough based on a script. Typically, this script will follow a “happy path” through the application, but more detailed automation tests check for known error conditions as well. The automation tests simulate entering text, clicking buttons, and scanning results.

Silverlight automation testing is possible, and has been for some time, but it is far easier to do with some helper projects supplied by the Prism team. If you are interested in running actual tests within the Visual Studio IDE, this post will provide step-by-step instructions to get you where you need to be.

Please take the time to go through this step-by-step if you are serious about automation testing. There are lots of small pieces that may seem complex at first, but once you’ve walked through the steps, it should be fairly straightforward and easy for you to set up new projects and automate the testing to a greater extent than shown here.… Read more

,

Wintellect Blogs

Silverlight UI Automation and Full VS 2010 Integration

Jeremy Likness

16 Jun , 2010  

OK, so it’s not exactly “out of the box” but the most recent PRISM 4.0 Drop (drop 2) seems to contain something Silverlight developers have been begging for … for quite some time.

It’s buried within the “QuickStart” folder under the “BasicMVVM” project. This project provides guidance for the Model-View-ViewModel pattern and is worth a deep look. It may provide a basic framework for your future applications.

In that same folder, you’ll find BasicMVVM.Tests.AcceptanceTest. Fire this project up and take a look at it. It contains some helper classes and launchers to host the Silverlight automation piece. If you’re impatient like me, build and fire it off. That is where you start to have fun!

Silverlight Test Integration

Yes, that is the test panel within Visual Studio 2010, not the Silverlight Unit Testing Framework in a browser window. Even more interesting is watching the form appear on the screen, then automatically get filled out by the engine as the acceptance tests are run.

So what we have here is full UI automation for tests as well as VSTS integration.

This is obviously a huge jump for Silverlight … I’m looking forward to digging in deeper to explore what’s possible with this!

Jeremy Likness

Read more

, ,

Wintellect Blogs

MEF instead of PRISM for Silverlight 3 Part 2 of 2: Region Management

Jeremy Likness

1 Mar , 2010  

In my last post, I showed you how to dynamically load modules on demand using the latest MEF release in Silverlight 3. This post, I will take you through managing regions with MEF. This will enable us to have a 100% MEF-based solution in Silverlight 3 if the only pieces of PRISM we were using were the dynamic module loading and region management.

Download the Source Code for this Post

Quick note: technically, the Composite Application Guidance (PRISM) is more of a guidance than an actual library. The library that is often used is the reference library for the guidance. I only mention this because in using the concept of the region manager and views, technically we are implementing the PRISM concept here and using CAG, we’re just using MEF as the engine instead of the included reference library.

OK, let’s get going …

The first thing I did this time around was to rip out all of the IModuleInitialization pieces. They aren’t needed because MEF will initialize what we need, as it’s loaded. I destroyed the classes in the modules (it was good to see them load, anyway) and updated the navigation class to look like this:

[Export(typeof(INavigation))]
public class ViewNavigator : INavigation 
{        
    [Import]
    public ICatalogService CatalogService { get; set; }
    
    private readonly Dictionary<ViewType, string_viewMap =
        new Dictionary<ViewType, string{
                { ViewType.MainView, "DynamicModule.xap" },
                { ViewType.SecondView, "SecondDynamicModule.cs.xap" }
            };   

    private readonly List<string_downloadedModules = new List<string();
                                                                   
    public void NavigateToView(ViewType view)
    {
        if (!_downloadedModules.Contains(_viewMap[view]))
        {
            var catalog = new DeploymentCatalog(_viewMap[view]);
            CatalogService.Add(catalog);
            catalog.DownloadAsync();
            _downloadedModules.Add(_viewMap[view]);
        }
    }           
}

Thanks to Glenn Block for pointing out the excellent sample that comes included with the MEF bits (the deployment sample) … this shows a different way of abstracting even the XAP file from the action of loading the catalog.… Read more

, ,

Wintellect Blogs

MEF instead of PRISM for Silverlight 3 Part 1 of 2: Dynamic Module Loading

Jeremy Likness

26 Feb , 2010  

Recently I’ve been having lots of conversations about the Managed Extensibility Framework (MEF), the Composite Application Library (CAL or PRISM), and how they relate. One point of confusion that many people has comes when they try to force the two solutions to work together. In a recent conversation, I mentioned that PRISM has some great features, but that if you are only using it for dynamic module loading and view management, MEF should do fine. Then I promised to post a blog with a reference project … and here it is.

Download the source for this project

First, let me share that I love PRISM and have been working with it in almost all of my projects for the past year. My Wintellect colleague Rik Robinson has an excellent article on PRISM you can read here. You can also scan this blog with the PRISM tag. However, I’ve started to really enjoy working with MEF and believe it is quickly becoming the solution of choice for composite Silverlight applications … especially with it’s inclusion in the upcoming 4.0 release. In these 2 posts I’ll show you how to tackle dynamic module loading and region management using exclusively MEF instead of PRISM.… Read more

, ,

Wintellect Blogs

Vancouver Olympics – How’d We Do That?

Jeremy Likness

25 Feb , 2010  

The Silverlight team recently posted a blog entry entitled “Vancouver Olympics – How’d we do That?” in which they detailed the massive effort across multiple partners to pull together the on-line solution for streaming HD videos, both live and on demand.

This was an exciting post for Wintellect and me because it detailed the effort we contributed to making the project a success. What’s more important to me for the Silverlight community, however, is that you don’t overlook the key detail they posted on the blog:

Development of a Silverlight application to interface with the service agents via WCF. This tool allows Microsoft to quickly publish new configuration files (such as web.config and clientaccesspolicy.xml) to groups of servers, as well as dynamically manipulate configuration settings through a visual interface (for example, turning off compression on a remote smooth streaming server when required) on multiple remote servers at once.

This is important because as a Silverlight developer I am constantly bombarded with questions about how viable Silverlight is, if it is ready for the enterprise and what practical application it has for business. This is a specific example where it was the right tool for the right job.

I won’t go into more details about the tool other than what Microsoft has publicly shared, but I wanted to emphasize that this is another example of how Silverlight helps to solve real world problems.… Read more

,

Wintellect Blogs

Auto-Discoverable Views using Fluent PRISM in Silverlight

Jeremy Likness

10 Jan , 2010  

One reason a developer would use a technology like MEF is to, as the name implies, make an application extensible through a process called discovery. Discovery is simply a method for locating classes, types, or other resources in an assembly. MEF uses the Export tag to flag items for discovery, and the composition process then aggregates those items and provides them to the entities requesting them via the Import tag.

Download the source code for this example

It occurred to me when working with PRISM and MEF (see the recap of my short series here) that some of this can be done through traditional means and I might be abusing the overhead of a framework if all I’m doing is something simple like marrying a view to a region.

Challenges with PRISM include both determining how views make it into regions and how to avoid magic strings and have strict type compliance when dealing with regions. This post will address one possible solution using custom attributes and some fluent interfaces.

Fluent Interfaces

Fluent interfaces simply refers to the practice of using the built-in support for an object-oriented language to create more “human-readable” code. It’s really a topic in and of itself, but I felt it made sense to simplify some of the steps in this post and introduce some higher level concepts and examples along the way.… Read more

, ,

Wintellect Blogs

PRISM, MEF, and MVVM Part 3 of 3: Dynamic MEF Modules in PRISM

Jeremy Likness

3 Jan , 2010  

Series recap:

In the final part of this series, I will show a dynamically loaded module (using PRISM) that takes full advantage of MEF.

Here is a preview of the final product, illustrating the different modules and areas I have pulled together to demonstrate (click for a full resolution view):

Download the Source for this Example

Why even bother with dynamic modules? Dynamic module loading can be a powerful way to build scalable (and more stable) applications in Silverlight. By creating dynamic modules, you ensure the user only loads what they need, when they need it. The dynamic module only comes into play when the user demands a feature that requires the module.

Before we dive into the new MEF-based module, let’s break down a few best practices when using dynamically loaded modules:

  • The best option I’ve found for this so far is to use PRISM’s “on demand” functionality for loading modules.
  • Typically, you will want to put your main dependencies in the “main” or “host” Silverlight project.
Read more

, ,

Wintellect Blogs

PRISM, MEF, and MVVM Part 2 of 3: Making PRISM MEF Friendly

Jeremy Likness

30 Dec , 2009  

In the first part of this series, we explored using the Unity container to bind the view model to the view. The next logical step is to explore how to use MEF. PRISM provides several useful mechanisms that relate directly to views and modules, such as the region manager and the module manager. This allows us to dynamically assign views to regions on the display and dynamically load modules.

Logically, it seems we could create a MEF module, dynamically load it through PRISM, and glue its parts together as well. We’d probably start with a view model that looks like this:


[Export]
public class MEFViewModel 
{
    [Import]
    public IService { get; set; }

    public DelegateCommand<objectCommand { get; set; }
}

And then easily pull it into a view like this:


public partial class MEFView : UserControl
{
   [Import]
   public MEFViewModel ViewModel { get; set; }

   public MEFView() 
   {
      InitializeComponent();
      PartInitializer.SatisfyImports(this);
      LayoutRoot.DataContext = ViewModel; 
   }
}

That is the essence of how it works, right?

Unfortunately, if you try this (like I did) you’ll quickly find that your parts won’t compose. You’ll receive errors, and the view won’t render. The view model won’t get instantiated. Why is this? Where did we go wrong?… Read more

,

Wintellect Blogs

PRISM, MEF and MVVM Part 1 of 3: Unity Glue

Jeremy Likness

21 Dec , 2009  

PRISM, also known as Composite WPF, has established itself as a very popular framework for building modular, scalable Silverlight applications. A newer contender, the Managed Extensibility Framework (MEF), has also grown in popularity. In fact, these two frameworks have left people scratching their heads wondering which one to use, when, how, and why.

Download the source code for this project.

Special note: the source won’t run “as is.” You need to take two steps: first, right click the PRISMMEF.Web project and choose, “set as start project.” Second, right click the PRISMMEFTestPage.aspx and choose “set as start page.” Then the project will run fine.

MEF will be packaged with Silverlight 4, and indeed has several preview releases available that will work on Silverlight 3 and 4. PRISM is coming out with newer releases that embrace the MEF framework. In fact, both frameworks work well together and know how to talk to each other’s containers.

In this series of posts I want to explore some concepts and aspects of solving the Silverlight application problem using both PRISM and MEF. I will use PRISM primarily for its ability to integrate views into regions, to dynamically load modules, and to provide an abstract messaging contract with the event aggregator.… Read more

, , , ,