PowerShell is really cool. I've been using it alot recently and have found it to be an incredibly powerful too. It blows away the Unix command line in flexibility and feature set; could this be a nail in the coffin of Unix?
One thing that I found incredibly interesting is the object-oriented approach of PowerShell. One of the best samples I have come up with is writing to a stream using the shell and a variable. Take a look at the example below:
- PS C:\> (new-item "helloworld.txt" -type file).AppendText() | set-variable a
- PS C:\> $a.WriteLine("Here is a list of all my processes")
- PS C:\> get-process | foreach-object { $a.WriteLine($_.ProcessName) }
- PS C:\> $a.Dispose()
- PS C:\> get-content "helloworld.txt"
Here is a list of all my processes
afscreds
afsd_service
afsdboot
alg
appmgrsvc
[Truncated]
- PS C:\>
If you don't know much about PowerShell or .NET let me give a quick overview: PowerShell commands return .NET objects that you can manipulate in a pipeline. The pipeline works much like Unix, however piping objects gives you the added flexibility of getting all the properties and methods of those objects.
Line one on the code above calls New-Item to create a file. Once created, a System.IO.FileInfo object will be passed down the pipeline. I call AppendText on the FileInfo to open a StreamWriter, and lastly, save that into a variable "a".
You can see that I can use $a in my future statements, because the stream is open and in that variable. When I'm done, I dispose it as normal, and get-content (equivelent to "type") of the file I created. Cool!
Download sample VS.NET 2005 Solution File
Download Presentation as PowerPoint 2007 (pptx)
Download Presentation as PDF
The Microsoft UI Composite Application Block (CAB) is an extremely powerful Microsoft “Best Practices” pattern for building complex UI applications with reusable and pluggable components. It is a framework of suggested guidelines that allow for extensibility and flexibility in UI application design. I will not go into detail about CAB in this article, as it is covered in many other places, however I will be brief on CAB’s benefits below.
CAB’s benefits stem from
-
Composition patterns using dependency injection to create objects with “loose” references
-
Declarative event brokering for multi subscriber/publisher events without subscriber needing to know of publisher
-
Saving of application state
-
Multiple independent assemblies loaded in one shell, allowing for multiple developers to easily split up an application when developing.
To find more information related specifically to CAB, see Microsoft’s Patterns & Practices site.
Production CAB
A few weeks ago, David Barnhill and I gave a presentation at Lab49 about the use of CAB. We got a bunch of positive feedback regarding our demo, and some good questions came out of it into which we researched further. Both of us (on separate projects), are currently utilizing CAB in our production applications at a financial firm. There have been many benefits to this, including reuse and simplified multi-developer working environment.
Learning CAB was an interesting experience. The concept is certainly different than most others, especially because of the extensive use of dependency injection, and attributes. CAB forces you to think on a “Work Item” basis, where you break up your tasks and UI’s into small bits and loosely couple them together. What we both found was that after building a small app (such as the demo included in this blog post), CAB quickly becomes intuitive and very friendly to use.
Demo
If you haven’t already, I suggest you look over the PowerPoint slideshow provided here. The slides were originally created in PowerPoint 2007, so if you don’t have it, you may download the PDF. These slides describe our experience with CAB and give a brief overview of CAB concepts. Hats off to Matt Davey who influenced some of the slide design from his CAB presentation in the UK.
Download Presentation as PowerPoint 2007 (pptx)
Download Presentation as PDF
To properly show the use of CAB in a financial application, we decided to create a simple (and admittedly ugly) stock trading application. The application has the following features:
-
A virtual “real-time” market to update stock quotes
-
A grid of all market stocks and their current values
-
A feature to buy and sell market stocks
-
A trace window to see what the application is doing.
We took an approach to ensure that we used CAB to its highest level.
First, we designed a shared assembly with an IMarket that contained Buy and Sell methods. This was an interface that ensured and UI components would not be coupled to the market service. The concrete class of Market was then created in the shell, but not distributed to the other modules. Market was designed to implement IMarket, and also update stock ticker symbols. Market exposed three events: TickerUpdated, BuyOrderCreated, SellOrderCreated. These events were not exposed through IMarket¸ but rather are accessed through the event broker by using the [EventSubscription] attribute on subscribers.

Next, we created a separate module to hold our UI components. This module referenced the shared libraries (with the IMarket interface) but not the shell. The module creates the majority of the components, including the race window, ticker list and buy/sell window. We use [ServiceDependency] to get the IMarket in any scenarios where we need access to buy/sell features. For ticker updates and other market events, [EventSubscription] attribute is used.
The workflow of this system is as follows:

(Click Image To Enlarge)
The entire development of this application took only an hour and a half. It is fairly impressive what you can get running in CAB in such a limited amount of time. We suggest you take a look!
Download sample VS.NET 2005 Solution File
Download Presentation as PowerPoint 2007 (pptx)
Download Presentation as PDF