Code (and slides) from Chicago Code Camp

by Administrator 6. May 2010 15:49

I had a great time at the Chicago Code Camp this year, although I was really only able to dash in, give my talk, and dash out.  What’s more, I was competing with some famous people, so I was pleased that my room was packed and that there were so many thoughtful questions.  One gentleman even told me he drove three hours to this event just to see me.  Wow!

Here are the materials from my presentation:

Slides

Code

Since the topic of run time composition came up more than once, see here for a use case and sample code.

I hope to be invited back next year!

Tags:

Chicago Code Camp 2

by Administrator 23. April 2010 01:19

[Update: the schedule has been posted]

I’m pleased to announce that the latest version of my “What can I do with MEF?” talk has been accepted for Chicago Code Camp 2 on May first.  While I actually have a release at work that day they’ve been kind enough to let me go towards the end of the day.

In this talk I’ll go over MEF soup-to-nuts with an emphasis on isolation and Silverlight applications.  In staying true to the code camp manifesto there will be only a couple of slides and a lot of code.  I hope to see a lot of familiar faces from the metro-Milwaukee area.

If (for some reason) coming to see me won’t get you into a Chicagoland code camp on a Saturday, there’s also the .NET Rocks! Visual Studio 2010 Road Trip.  Come meet Carl and Richard and do a review of the event in order to get a free special edition .NET Rocks! mug.

Tags:

MIX it Up! at FVNUG

by Administrator 11. April 2010 14:35

Last year I had a lot of fun doing a little something called the MIX it Up! tour in the Midwest.  While this year we don’t have quite as much of a formal coalition there are still several of us bringing a recap of MIX 2010 to user groups, code camps, and Days of .Net. My first stop this year will be at the Fox Valley .NET User’s Group meeting this week Wednesday.  While there were a lot of great announcements at MIX, I’ll be focusing on Silverlight 4 and Windows Phone 7 Series.

Why not register and come see me?

Tags:

Cascading ItemsSource Bindings in Silverlight

by Administrator 15. March 2010 21:35

When building a complex data visualization application in Silverlight, I discovered a disturbing issue when dealing with multiple DataGrids on the same screen.  If the ItemsSource of a particular DataGrid gets updated as the result of a SelectedItem change in another DataGrid the child grid would not display any items.  Worse, adding code to query the ItemsSource of the child grid shows that does have a valid and correct ItemsSource, so why doesn’t it display?  In this article we’ll quickly demonstrate the problem and show a solution.

Sample App

I’ve created a simple example with a ‘parent’ DataGrid containing Orders and a ‘child’ DataGrid meant to display the Order Details of the selected Order.  Throw in a small amount of sample data and we wind up with this:

dbf0

You can see that we have selected the first and only row in the top DataGrid and it has a valid OrderDetails property.  Clicking on the Check Items Source button reveals that something should be shown in the bottom DataGrid:

dbf1

So what’s going on?  Let’s take a look at the XAML and data binding code to see if anything is amiss.

Data Binding Code

I have created a simple ViewModel for this screen, with code as follows:

public class ScreenModel : BindableType
{
    public ScreenModel()
    {
        Orders = new ObservableCollection<Order>();
        Orders.Add(new Order());
    }


    private ObservableCollection<Order> _Orders;

    public ObservableCollection<Order> Orders
    {
        get { return _Orders; }
        set
        {
            _Orders = value;
            OnPropertyChanged("Orders");
        }
    }

    private Order _SelectedOrder;

    public Order SelectedOrder
    {
        get { return _SelectedOrder; }
        set
        {
            _SelectedOrder = value;
            //Doesn't work:
            OnPropertyChanged("SelectedOrder");

BindableType implements the basic INotifyPropertyChanged functionality.  Hopefully you agree that this is extremely vanilla code, and that it’s a bit odd that this does not work.  Maybe something is wrong with the XAML?

            <TextBlock>Master Grid</TextBlock>
            <my:DataGrid x:Name="MasterGrid" MinHeight="100"
                         ItemsSource="{Binding Orders}" SelectedItem="{Binding SelectedOrder, Mode=TwoWay}" />
            <TextBlock>Detail Grid</TextBlock>
            <my:DataGrid x:Name="DetailGrid" MinHeight="100" ItemsSource="{Binding SelectedOrder.OrderDetails}" />
            <Button x:Name="ChkBtn" Content="Check Items Source" Click="ChkBtn_Click"></Button>

The XAML is extremely basic as well.  How can you fix this?  Searching through forums it seems that when Silverlight 3 was in BETA changing the bottom DataGrid to have a TwoWay binding to its ItemsSource would fix this but I didn’t have luck with this.

Problem Solution

By groping in the dark and changing a single line of code I was able to get the correct behavior:

dbf2

The only code change is this:

private Order _SelectedOrder;

public Order SelectedOrder
{
    get { return _SelectedOrder; }
    set
    {
        _SelectedOrder = value;
        //Doesn't work:
        //OnPropertyChanged("SelectedOrder");

        //Works:
        Deployment.Current.Dispatcher.BeginInvoke(() => OnPropertyChanged("SelectedOrder"));
    }
}

All I’ve done here is to invoke the Property Change Notification for SelectedOrder back onto a Dispatcher thread.  Why is this necessary?  I can’t say for sure.  My guess is that something down in the bowels of Silverlight is attempting to detect some sort of layout cycle or infinite recursion here.  If you find this situating in your code this should work for you.

Tags:

AddRange for ObservableCollection in Silverlight 3

by Administrator 4. March 2010 01:43

It’s great that the built-in controls in Silverlight understand INotifyCollectionChanged, and that ObservableCollection<T> ships with Silverlight.  However, when adding large data sets to ObservableCollection<T> the performance  can be brutal due to the fact that ObservableCollection<T> fires an event with every operation on the collection.  Most of the time this is what you want however some bulk operations like AddRange would be nice.  Let’s take a look at a way to add AddRange() to ObservbleCollection<T> using a technique that vastly increases performance.

Example UserControl

We’ll use a simple Silverlight root visual with a DataGrid on it to demonstrate this technique. Note that we are using a new class called SmartCollection<T> and an Item class with some randomly generated field values.

namespace HandWaver.AG.OCAddRange
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            _collection = new SmartCollection<Item>();
            DasGrid.ItemsSource = _collection;            
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(() => BuildItems());
        }

        TimeSpan Time(Action a)
        {
            var start = DateTime.Now;
            a();
            var end = DateTime.Now;
            return end - start;
        }

        SmartCollection<Item> _collection;
        
        private void BuildItems()
        {
            Action slow = () =>
            {
                for (int i = 0; i < 25000; ++i)
                {
                    _collection.Add(new Item());
                }
            };

Using this technique and displaying the time it took to add these items to the data grid gives me this result.  Note that Stopwatch is not present in Silverlight 3.

time0

Nearly 10 seconds to add 25,000 items to the DataGrid even though most of them are not visible.  Let’s see what SmartCollection<T> can do with slightly different code:

Action fast = () =>
{                
    var sourceList = new List<Item>();
    for (int i = 0; i < 25000; ++i)
    {
        sourceList.Add(new Item());
    }
    
    _collection.AddRange(sourceList);                
};

var time =
    //Time(slow);
    Time(fast);

So we’ve built a source collection and used the AddRange implementation.  How does it do?

time1

That’s a pretty significant speed up! Let’s go ahead and look at the code for SmartCollection<T> now.

using System;
using System.Linq;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Collections.Specialized;

namespace HandWaver.AG.PresentationModel
{
    public class SmartCollection<T> : ObservableCollection<T>
    {
        public SmartCollection()
            : base()
        {
            _suspendCollectionChangeNotification = false;
        }


        bool _suspendCollectionChangeNotification;

        protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
        {
            if (!_suspendCollectionChangeNotification)
            {
                base.OnCollectionChanged(e);
            }
        }

        public void SuspendCollectionChangeNotification()
        {
            _suspendCollectionChangeNotification = true;
        }

        public void ResumeCollectionChangeNotification()
        {
            _suspendCollectionChangeNotification = false;
        }


        public void AddRange(IEnumerable<T> items)
        {
            this.SuspendCollectionChangeNotification();
            int index = base.Count;
            try
            {
                foreach (var i in items)
                {
                    base.InsertItem(base.Count, i);
                }
            }
            finally
            {
                this.ResumeCollectionChangeNotification();
                var arg = new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset);
                this.OnCollectionChanged(arg);
            }
        }

    }

}

Note that we could do an AddRange implementation via an extension method but we would not gain the performance improvements since events would still fire for every operation.  By extending ObservableCollection<T> we gain the ability to create our own AddRange method.  The AddRange method sets a flag that temporarily suppresses the collection changed notification for ObservableCollection<T>.   At the end it uses NotifyCollectionChangedAction.Reset to indicate that a significant change was made to the collection.  The UI rebuilds itself in a fraction of the time it took to add the items one by one.  You can use this technique to gain huge performance improvements for your Silverlight 3 apps dealing with large data sets.

Tags:

Speaking at FVNUG Day of .Net

by Administrator 2. March 2010 02:40

I have the privilege for the second year in a row of speaking at the Fox Valley Day of .NET.  Last year the event was a little later in the year (or MIX was earlier?) and I gave the keynote as part of the MIX It Up! tour.  This year I am actually going to MIX so maybe I’ll have to come back.

I will be giving the MEF talk I mentioned not too long ago and while I will have some good Silverlight content this is more about MEF itself.  The guys up there are gracious hosts and I am looking forward to getting up there and sharing one of my favorite new technologies.

If MEF is not your thing, please consider seeing my colleague Christopher Barwick’s talk on Business Intelligence: “BI: from Database to Warehouse  to OLAP to Excel with SQL Server 2008 & Excel 2007” running in the same time slot as my presentation.  Chris has really been stepping up his involvement in the community and he has a lot to share.

For my part I will be watching Dave Bost talk about Silverlight 4 right after my talk.  While I imagine I’ve already consumed this content I can’t get enough Silverlight and Dave is a good presenter so I’ll be sitting in.

Please check out their site and come see us!  They have a lot of swag this year so your chances of coming home with something extremely useful are very good.

Tags:

BigHammer/EdgeNet is hiring

by Administrator 27. February 2010 20:13

If you’ve ran into me at a community event lately you may have caught me saying something along the lines of working too many hours.  You no doubt felt bad for me and wished you could do something, anything, to help me in my predicament.  Most of the time your sympathy came in the form of beer, which is greatly appreciated.  However if you really want to contribute to the Save Damon Foundation you should come and work for me!

Ok, bad jokes aside, we are hiring for my team.  We currently have offices in Waukesha (WI), Nashville, and Atlanta, although people have worked remotely from all over the place in the past.

What would you be working on?  A large family of applications with terabytes of data.  Our current process and technology mix looks something like this:

  • Asp.net
  • jQuery
  • WCF
  • All Microsoft application stack
  • Team System
  • Scrum
  • Silverlight 3
  • Silverlight 4
  • MEF
  • Unity
  • PRISM
  • AbunchofotherstuffIcanthinkofrightnow

Leave me a comment or contact me for more details.

Tags:

Would you go see this talk? (MEF)

by Administrator 14. February 2010 20:56

I am preparing a talk for the spring code camps and conferences.  Would you go see this talk?

What can I do with MEF?

The Managed Extensibility Framework has left excitement and confusion in its wake as .NET 4 moves towards release. What can I use this for? What should I use this for? Is this another IoC/DI container? What about Unity? What about the System.AddIn namespace? In this talk we will explain the workings of MEF using straightforward examples and then dig deep into the real power of MEF using examples including MEF for Silverlight, MEF for WPF, and MEF for Enterprise services.

Leave a comment, and thanks!

Tags:

Preparing for the 2010 Global MVP Summit

by Administrator 14. February 2010 20:24

Starting tomorrow I’ll be attending the 2010 Global MVP Summit in the Seattle area.

After working way too hard since, well, pretty much since July of 2009, I will be taking some me time.  Not everyone would consider this kind of event a break but for me this is as exciting as an exotic vacation.  Of course I can’t talk about anything that’s going on during this week, so to the untrained eye following me on twitter it may appear that I am merely goofing off and drinking beer for five days.  In the past I have always found events like this to be a mix of actual learning value and chance to take one’s self out of the daily grind of deadlines and features to gain perspective and recharge.  While I expect this to be the most learning-heavy event I’ve ever attended I’ve also really never needed to recharge as badly as I do right now.  Between the actual session content, putting faces with virtual names, and the organized social events it looks like sleep is going to be hard to come by.  This means my recently worsening insomnia may prove to be a valuable (well, OK, tolerable) asset.

Work and personal factors have kept me from writing much for the past month (since December, really) so I have three personal technical goals for this week as well.  I hope to have time to hack on some personal items as well.

I’ll be using the twitter tag #mvp10.  Here’s to a great geek holiday.

Tags:

Going to MIX 2010

by Administrator 30. January 2010 20:53

I haven’t decided where I’m staying yet, but I will be attending MIX 2010 March 15th – 17th.  Are you going to MIX? Drop me a comment!  I will likely be accompanied by someone from BigHammer but I’m always looking for other people to drink beer learn with!  I’ll obviously be leaning heavily towards the Silverlight sessions.  If MIX 2009 is any indication there should be some great content in March. 

Of course, I am hoping to finally see a completely awesome Windows Mobile 7 announcement.  Please?

Tags:

About the author

Damon Payne is a Microsoft MVP specializing in Smart Client solution architecture. 

INETA Community Speakers Program

Month List

Page List

flickr photostream