More than one way to skin the WPF cat

Posted on February 13th, 2010 in .NET, UI | No Comments »

My plunge down the hole of all that is WPF continues. So far I’m still impressed with the view (pun intended). For some time I kept expecting to find some juicy nugget to be the barrier to using WPF in practice but I’m just not finding it. If anything the biggest issue seems to be that there are so many features it is hard to get your head around which one to use at what time.

At this point, I’m trying to take a very purest approach to MVVM. I’ve got a great Model layer and I find creating View-Models rather common-sense-able (I’m patenting that word so watch out) and even enjoyable. I’ve been known in the past to create command line utilities for all sorts of needs and while I do enjoy the command line for many tasks, there have been times when I wanted a good UI but just didn’t have the time to invest in cobbling one together. The more fluent I get with WPF the more I start to think that creating command line utilities is no longer an easier/faster way to get a small tool up and running. WPF just makes it so easy once the monstrous learning curve is surpassed.

So the M in M-V-VM is all good and the VM in M-V-VM is all good….now lets talk a bit about the V.

I’m currently taking the approach of trying very hard to keep the view to purely XAML. No code behind with the exception of the already mentioned ViewModel. Yes there will be a converter or 6 to map properties of one type and value in the Model to another value and type in the View but these are small, simple, reusable components. What I want to avoid is the more cumbersome type of code that UIs can so often become overrun with such as event handlers that scrape values from one UI element and pass them along to another. Essentially trying to avoid writing any C# code which is purely View focused and which begins to represent some sort of business logic.

I initially slipped off of this band wagon for a small feature and it seems like an OK thing to do at the time and then it bit me. I have since come up with a palatable alternate solution which is working well. I trust however that this is in no way to only solution or even the best. WPF just has so many features and abilities, there seems to always be more than one way to solve any given issue.

Let us look at my issue.

I have a ListView to which new items are added from time to time as the application runs. I want the most recent item at the bottom to scroll into view when it is added. Simple enough requirement.

I initially implemented this using a ScrollViewer around the ListView and then listening for the SizeChanged event and scrolling to the bottom of the ScrollViewer in the event handler. Something like this.

            <ScrollViewer SizeChnaged="ScrollViewer_SizeChanged">
                <ListView AlternationCount="1"
                          ItemsSource="{Binding MyCollection}"
                          IsSynchronizedWithCurrentItem="True">
                </ListView>
            </ScrollViewer>
      private void ScrollViewer_SizeChanged(object sender, SizeChangedEventArgs e)
      {
         ScrollViewer sv = sender as ScrollViewer;
         sv.ScrollToEnd();
      }

Now this worked, and all of the code is in a user control so it really does seem a fine place to relax a bit in terms of trying to avoid code in the view layer. But….

During some later refactoring I pulled a good portion of the XAML for this user control out into a resource dictionary as a ControlTemplate to both reduce the growing code in the user control as well as make what was a generally reusable bit of UI formatting into a reusable package….the ControlTemplate. All worked as expected except that you can’t make use of an event handler in a ControlTemplate. This makes sense as a traditional event handler needs some code to wire up the event listener. Now if this was a Button then we would just move away from the older style Click event and make use of the new WPF wiz-bang Command. These work just fine in a template….and yes this is where an appreciation for the WPF Command architecture really sets in. But alas the ScrollViewer does not have an equivalent command version of the SizeChanged event and so we need another solution.

Now again, I’m not suggesting this is the only solution and I would even bet that there is an easier one. And I would love to hear what they are. But my solution does have some nice aspects and is relatively short, sweet, and reusable.

I created a custom control which sub-classes the ScrollViewer and called it ScrollToEndScrollViewer. This new control exposes a single new property AutoScrollActive which controls turning on/off the new feature. This new property is bindable and so it can be activated via data in your ViewModel. While I initially felt like a new control type was heavy handed for this solution, I do like several aspects of this solution. Firstly the logic for the scrolling feature is hidden away from both the View XAML and from the View Model. Secondly, allowing the control over turning this feature on/off to be bindable allows for simple connectivity between the presentation and the data without any hard dependency. Thirdly, by providing this feature via the ScrollViewer it is not tied at all to the content actually being displayed. As an alternative, one could likely devise a solution using a ListView or a DataView more directly but then other typos of content would not benefit.

So lets look at the code….

The custom control simply sub classes the standard ScrollViewer and implements the ability to always scroll to the bottom when new content is added.


public class ScrollToEndScrollViewer : ScrollViewer
    {
        public ScrollToEndScrollViewer() : base()
        {
        }

        static ScrollToEndScrollViewer()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ScrollToEndScrollViewer), new FrameworkPropertyMetadata(typeof(ScrollToEndScrollViewer)));
        }

        /// <summary>
        /// Controls whether the viewer auto scrolls to the bottom when new content is added.
        /// </summary>
        public Boolean AutoScrollActive
        {
            get { return (Boolean)this.GetValue(AutoScrollActiveProperty); }
            set { this.SetValue(AutoScrollActiveProperty, value); }
        }

        /// <summary>
        /// Supporting dependency property for AutoScrollActive
        /// </summary>
        /// <remarks>
        /// The property much be implemented as a dependency property to allow data binding.
        /// </remarks>
        public static readonly DependencyProperty AutoScrollActiveProperty =
            DependencyProperty.Register("AutoScrollActive",
                                        typeof(Boolean),
                                        typeof(ScrollToEndScrollViewer),
                                        new PropertyMetadata(true));

        /// <summary>
        /// Overloaded event handler
        /// </summary>
        /// <remarks>
        /// Monitor for when the viewable area is larger than the total area and the height of the
        /// total area has changed.  In this case scroll to the bottom.
        /// </remarks>
        /// <param name="e"></param>
        protected override void OnScrollChanged(ScrollChangedEventArgs e)
        {
            base.OnScrollChanged(e);

            if (AutoScrollActive)
            {
                if (e.ViewportHeight < e.ExtentHeight &&
                    e.ExtentHeightChange > 0)
                {
                    base.ScrollToBottom();
                }
            }
        }
    }

Then the XAML makes use of this control including binding the boolean control value to a value in the ViewModel.

            <cc:ScrollToEndScrollViewer AutoScrollActive="{Binding SomeMVPropToControlScrolling}">
                <ListView AlternationCount="1"
                          ItemsSource="{Binding MyCollection}"
                          IsSynchronizedWithCurrentItem="True">
                </ListView>
            </cc:ScrollToEndScrollViewer>

All in all this does the trick. I still suspect I will come to find another solution in time, but even then this has been a useful learning tool in exploring the process and value in sub-classing an existing control to gain similar but enhanced behavior in the view.

Wpf binding bug when using a value converter….VS2010 Beta2

Posted on February 7th, 2010 in .NET, UI, Uncategorized | No Comments »

I’ve continued to dive into WPF and continue to like what I find.  I’m a bit saddened that WPF is useful only in the Windows fat client and silverlight arenas.  I wish it could be applied more generally….perhaps in time.  As a developer I long for a world where your choice of software development tools is not bound to a specific platform. It is a shame Sun hasn’t come up with a good solution for this:)

I think the WPF style of approach to UI design will either excel as a clear technical winner or die out due to a combination of its radical divergence from traditional concepts and its ties to the MS platform.  I’m certainly rooting for its success.  I would love to see Java or some other development platform come out with a similar tool set to help push the general concepts to a new level and give certain life to the approach.  All in all I think MS deserves kudos on their bold direction away from WinForms.

So along my Wpf way I have found many pleasing things….but also a rather obnoxious bug.  It shows its head in the designer when using data binding and a converter.

Here is a simple stack panel where the visibility is a function of the “Quantity” property. The stack panel has a DataContext set to an object which has a property named Quantity. The converter simply decides if the quantity data is relevant to display and returns either Visible or Collapsed.

<StackPanel Visibility="{Binding Quantity, Converter={StaticResource Quantity2VisConv}}">

This works as expected at run-time but not in the xaml designer. In the designer the converter is always returning Visible regardless of the input value. After some time I gave up and assumed that the designer might always show the panel to aid in design time editing of the panels content….seems odd but perhaps.

Later I encountered a similar issue using a converter to alter a value by padding it to a fixed width. Again this worked as expected at run-time but not so much in the designer. Rather than the padded value I got the parent objects ToString() value. Not exactly friendly in the designer to expect a Quantity value padded to X digits and instead I get MyNameSpace.MyObject. Again I spent a little time looking at this but thought that perhaps the 2010 beta just has some issues with the designer and converters in general.

Later I circled back around to the visibility converter and looked at the issue again. Since the issue only happens in the designer, a break point or debug trace doesn’t help me too much. So I added a bit of direct file IO tracing to the converter to see when it is called and what is passed to it. Oddly when run from the designer it was not getting passed the Quantity property at all, but rather it is being passed the parent object MyNameSpace.MyObject. Since the converter has no idea how to deal with this type it just returned Visible. And yes I then looked at the padding converter and found it doing the same thing. At run time it gets the correct property but in the designer it is getting passed the parent object….hence the value coming back as the ToString of that object.

So now I at least know there is a common issue at the core of both issues….but what is it? Just a bug in Dev Studio? Rather troubling on my end as I hope to use my work here soon to show some fellow developers the wonders of Wpf….much less impressive if things in the designer don’t actually look as they do at run time.

After a bit of playing I found it to be this….

I’m taking advantage of the fact that the default property of the Binding is the “Path”. So I had {Binding Quantity} rather than {Binding Path=Quantity}. It seems the designer has an issue with this when passing the value to a converter. After changing the xaml to the following it all worked.

<StackPanel Visibility="{Binding Path=Quantity, Converter={StaticResource Quantity2VisConv}}">

Easy enough work around once you know it.

A jount through Interop Hell (due to sins of the past).

Posted on January 26th, 2010 in .NET, COM, Interop | No Comments »

I ran into an issue this week that was “on of those”.  Random GPFs and no cause in sight.  It wreaked of heap corruption but knowing that doesn’t really help you find or fix it.  Fortunately I was able to recreate this in the debugger relatively easily and this is always a life saver on such an issue.

The application is about 50/50 C++ and C# as it is undergoing a migration from the former to the latter.  There is little COM involved except to provide the needed interop, though there is some COM which preceded the migration in dealing with some Active X UI controls.  I tell you all of this just to set the stage for what sort of environment this issue was found in and because this is no doubt the state of a lot of applications out there today.  I in fact hesitated to even write this up at first as the issue found ultimately seemed so esoteric and unlikely to be common.  But in my adventures of finding the issue and resolving I found another blog which discussed the issue in great detail and had the exact same underlying cause….exactly.  And so I decided that perhaps this is/will be more common than I first expected as older applications are given the C# face lift.

Firstly I found an option on Dev Studio that proved to be extremely useful in an environment such as the one described here.  It seems that by default when you start a project in the debugger that is managed code you can only set break points in managed code.  These break points can be in the current project or others but only in managed projects.  The vice versa of this also seems to hold true.  Particularly annoying when the issue you are chasing is crossing the managed/unmanaged boundary a time or two.  After a double look at this, there seems to be a rather simple solution.

I don’t know about you but in the software development circles I have been in over the years we have often laughed about the “make it work flag”.  Situations where there is some horrid problem and after beating your head against it for sometime,  someone in the know finds you beaten and bloody and so helpfully (and smugly perhaps) informs you of this simply flag you can flip to just make it work.  Well this is just such a moment.

In a managed project you have the “Debugger Type” optionCppDebugMixed

In a managed project you have the “Enable unmanaged code debugging”:

C#DebugMixed

Making use of these increased my ability to diagnose the issue.  I still found different results in how the error would manifest depending on which type of project I started the debugging in and at this time I’m unclear if this is expected or if it was simply the nature of a heap corruption issue.  It could be either.

The issue showed itself in several different ways.  Initially it would cause an application break in malloc.c during a call to HeapAlloc() and other times it would show in free.c with a call to HeapFree().  Neither very useful.

Then there was this one:

An unhandled exception of type ‘System.ExecutionEngineException’ occurred in Unknown Module.

If that one was suppose to be useful it escapes me.

From time to time it would provide something far more interesting from a managed debugging assistant.

Managed Debugging Assistant ‘DisconnectedContext’  has detected a problem in ‘C:\Program Files\blah\blah.exe’.
Additional Information: Context 0×15a170′ is disconnected.  Releasing the interfaces from the current context (context 0×15a2e0).  This may cause corruption or data loss. To avoid this problem, please ensure that all contexts/apartments stay alive until the applicationis completely done with the RuntimeCallableWrappers that represent COM components that liveinside them.

This no doubt was a better clue but it was a bit misleading as the application in question is largely single threaded so the idea of the owning thread having gone away was unlikely.

As I mentioned before the application is crossing the managed/unmanaged/and back again boundary rather often.  This  nature of the application made the stack traces sort of go cold at various points.  But using the debugging switches mentioned above as well as loading the Microsoft symbols was able to bring life to the majority of the stack.  The nature of the crash was random and often the stacks were useless but when you can catch one of these in the debugger you have the luxury of just crashing it over and over until you get a useful stack or a pattern begins to emerge.  The black art nature of chasing these issues is not something I enjoy.  Several crashes later I got this stack.

MyApp.exe!006d0072()
mscorwks.dll!SafeReleaseHelper()  + 0×70 bytes
mscorwks.dll!SafeRelease()  + 0×2f bytes
mscorwks.dll!IUnkEntry::Free()  + 0×50 bytes
mscorwks.dll!RCW::ReleaseAllInterfaces()  + 0×18 bytes
mscorwks.dll!RCW::ReleaseAllInterfacesCallBack()  + 0×2f bytes
mscorwks.dll!RCW::Cleanup()  + 0×22 bytes
mscorwks.dll!RCWCleanupList::ReleaseRCWListRaw()  + 0×16 bytes
mscorwks.dll!RCWCleanupList::ReleaseRCWListInCorrectCtx()  + 0×7c bytes
mscorwks.dll!CtxEntry::EnterContextCallback()  + 0×8c bytes
ole32.dll!CRemoteUnknown::DoCallback()  + 0×3b bytes
rpcrt4.dll!_Invoke@12()  + 0×30 bytes
rpcrt4.dll!_NdrStubCall2@16()  + 0×215 bytes
rpcrt4.dll!_CStdStubBuffer_Invoke@12()  + 0×82 bytes
ole32.dll!SyncStubInvoke()  + 0×33 bytes
ole32.dll!StubInvoke()  + 0×73 bytes
ole32.dll!CCtxComChnl::ContextInvoke()  + 0xd2 bytes
ole32.dll!MTAInvoke()  + 0×1a bytes
ole32.dll!STAInvoke()  + 0×4e bytes
ole32.dll!AppInvoke()  + 0×6a0 bytes
ole32.dll!ComInvokeWithLockAndIPID()  + 0×2590 bytes
ole32.dll!ComInvoke()  + 0×5a bytes
ole32.dll!ThreadDispatch()  + 0×1a bytes
ole32.dll!ThreadWndProc()  + 0×93 bytes
user32.dll!7e418734()
[Frames below may be incorrect and/or missing, no symbols loaded for user32.dll]
ole32.dll!ThreadWndProc()
dcbaabcd()
ole32.dll!ThreadWndProc()
0012d9bc()
ole32.dll!ThreadWndProc()
00370736()
ole32.dll!ThreadWndProc()
00370736()
ole32.dll!CCliModalLoop::PeekRPCAndDDEMessage()  + 0×210 bytes
ole32.dll!CCliModalLoop::BlockFn()  + 0×5ad bytes
ole32.dll!_CoWaitForMultipleHandles@20()  + 0xe6 bytes
mscorwks.dll!NT5WaitRoutine()  + 0×39 bytes
mscorwks.dll!MsgWaitHelper()  + 0×97 bytes
mscorwks.dll!Thread::DoAppropriateAptStateWait()  + 0×51ae9 bytes
mscorwks.dll!Thread::DoAppropriateWaitWorker()  + 0×104 bytes
mscorwks.dll!Thread::DoAppropriateWait()  + 0×40 bytes
mscorwks.dll!Thread::JoinEx()  + 0×77 bytes
mscorwks.dll!Thread::Join()  + 0×14 bytes
mscorwks.dll!RCWCleanupList::CleanupWrappersInCurrentCtxThread()  + 0xcfb87 bytes
mscorwks.dll!RCW::Initialize()  + 0×78 bytes
mscorwks.dll!RCW::CreateRCW()  + 0×65 bytes
mscorwks.dll!COMInterfaceMarshaler::CreateObjectRef()  + 0×45 bytes
mscorwks.dll!COMInterfaceMarshaler::FindOrCreateObjectRef()  + 0xd160 bytes
mscorwks.dll!GetObjectRefFromComIP()  + 0×10d bytes
mscorwks.dll!ConnectionPoint::AdviseWorker()  + 0×89 bytes
mscorwks.dll!ConnectionPoint::Advise()  + 0×39a bytes
MyExe.exe!MyClass:MyFunction(WindowObject * owner=0×1251a790, IMyInterface * pObj=0×1242d170)  Line 23    C++

As you can see this starts out trying to wire up a COM event sink via Advise.  This is in C++.  The object which is the event source is over in C#.  Well truth be told it is also C++ but there is some C# in between.  So we see the dirty process of getting this Advise call pushed over into the managed space.  This starts somewhere around the first mscorwks.dll!RCW::CreateRCW() where a Runtime Callable Wrapper is being created to expose the COM object to the managed code.  We then see a Thread:Join and an NT5WaitRoutein.  This interests me as again the application is largely single threaded from my POV.  I will look to get to the bottom of this later.  At the NT5WaitRoutein() we see things transition from managed in mscorwks.dll over into unmanaged in ole32.dll.  Then for reasons that are not yet clear to me we see the flow transition again over to managed code here:

mscorwks.dll!CtxEntry::EnterContextCallback()  + 0×8c bytes
ole32.dll!CRemoteUnknown::DoCallback()  + 0×3b bytes

At this point we see a RCW do some cleanup and it is in the cleanup that things go horribly wrong.  I suspect what this is, is a RCW that was used at a prior time but is no longer needed being recycled for use in our new call.  It is trying to divorce itself from the COM object is previously managed by calling Release().  Now why did this fail……

So just the recall my frame of reference at this point here are the facts that matter:

  1. Heap Corruption
  2. Bad Releases of COM objects
  3. Advise event sinking across an interop boundary
  4. The application used  to Advise without the interop until recently
  5. A RCW seems to be the culprit doing a bad release

This led me back to have another look at how the Advise handshake is being done.  Something had already looked odd to me in this area as the AddRef/Release calls on the event sink didn’t seem to be properly implement.   I had glanced at this from a potential leak point of view and had ruled that out and after all a leak while bad does not cause a crash.

The code was written based on the assumption that this “COM object” was only every used once and by one client….the event source.  In the past this was true as it is all in-process and it was created, hooked to the event source via Advise, and then later unhooked via Unadvise deleted.

Yes deleted….like C++ memory operator “delete”.  Its ok right?

  1. new
  2. Advise
  3. Unadvise
  4. delete

Since this is all application internal (we own the sink and source) we know there is no one else using the object.  Once the Unadvise is done we know the object is useless and so we can delete it.  Yeah we break a COM rule or two but we know what we are doing, it’s ok right?.  After all I hate COM overhead and annoyances as much as the next guy.  If we can cut a corner and avoid a bit of the junk COM puts us through why not.

Why not….because rules while annoying and unneeded at times are there for a reason.  I can’t believe I’m writing this.  My 16 year old  rebellious self is very ashamed.  In all seriousness, while I sometimes dislike rules, I always like consistency.  Even if something is wrong I like them all to be wrong the same….makes fixing them once you  realize the error of your ways easier.

The assumption of there only being one user of the particular object was completely valid in the past…..but not once it is marshaled across a .Net interop layer.  There are middle men now that need it….middle men known as RCW.

Sadly the Unadvise call does unhook the event sink from source but it does not unhook the managed RCW that is wrapping the sink.  And we deleted the object right out from under him.

Once understood the fix is easy.  Implement a proper AddRef/Release as so:


ULONG STDMETHODCALLTYPE MyClass::AddRef()
{
   InterlockedIncrement(_refCount);
   return _refCount;
}

ULONG STDMETHODCALLTYPE MyClass::Release()
{
   InterlockedDecrement(_refCount);
   if(0 == _refCount)
   {
      delete this;
   }
   return _refCount;
}

And then change the calls which did the direct “delete” to simply call Release().

Who says knowing is 1/2 the battle.  Its 98% of the battle.

After determining what this issue was, I was able to search Google and actually find an in depth explanation.  Rather ironic that I had to know the solution in order to craft a decent enough query to find a good write up:)  If you want more information here it is:

http://blogs.msdn.com/carlos/archive/2009/03/10/a-clr-com-interop-issue.aspx

Self-induced WPF bootcamp (aka A 12 step program for newbies)

Posted on January 23rd, 2010 in .NET, Architecture | No Comments »

The code discussed in this article can be downloaded here as a VS2010 project.

In this article I want to help WPF newbies by providing a set of links which helped me tremendously in ramping up from know-nothing-newbie to code-producing-xaml-mister. In addition I will share some performance test results to show what WPF seems to do well and what it does not do so well.

My ORM project has reached a stage where I’m ready to eat a bit of my own dog food. I’m ready to build an application atop of it and see where it works well, where it needs some tuning, and where the wheels just totally fall off. My app will need a UI and so I’m taking this opportunity to ramp up on another technology which I have not previously had a good vehicle for exploring….enter stage left WPF.

After roughly 2 weeks of evenings chasing one online WPF-how-to after another I can say this is by far the most impenetrable programming concept/toolset/framework I have ever set out to master. I don’t want to sound mightier than thou, but I’m a smart guy and it usually doesn’t take me all that long to get my head around a topic once I a) decide I want to and b) have a good vehicle for exploring it. In this case I have both and set out thinking that 2 days in I should have my first basic demo UI starting to take shape. Boy was I wrong. Really wrong.

WPF is a paradigm killer. Every thing you know about UI controls, event wiring, UI-to-Data glue code, etc had better be left at the door and your mind ready to be expanded.

I’m not here to teach you anything really, I’m still too much a learner myself at the moment. I think my 2 day ramp up estimate has been adjusted to 2 months. But I am here to walk you through the links that finally got me pointed in the right direction. Following these articles in roughly this order set me straight. I’m not WPF fluent yet, but my head is starting to think in the new paradigm and thus far I can say it is a very fun place to be. My only deep concern at the moment is about performance. All of the data binding, loose property linking, and configured event wiring quickly lead you to wonder if all the development time benefits of WPF will be paid for by a runtime performance hit. I will conclude this article with some cursory analysis of one performance test.

The following are the links that finally “made it all click” along with a brief explanation of each.

Guided Tour Horse Race

http://www.codeproject.com/KB/WPF/GuidedTourWPF_1.aspx

Josh Smith has put together a very good 5 part walk through to get a newbie started.  Prior to finding this thorough tutorial I was just not getting it.  I would look at one small example after another and understand them at the micro level but there was just no perspective of how it was meant to all glue together.  John’s horse race is the perfect place to start.

Model View ViewModel MVVM

http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090051

Not until writing this article and reviewing these links did I realize that Josh also wrote the excellent MVVM MSDN article that helped me.  The horse race demo is a great starter for the core WPF knowledge but it doesn’t go to far into helping you think about laying out a real application architecture.  In my particular case I have the model nailed down quite well as part of my ORM work.  I left the horse race feeling better about the view using WPF.  I had ideas on how to glue the two together but it was fuzzy.  I actually come from a MVC background back before MVC was cool like it is today with all the new wiz-bang ASP.NET MVC.  So naturally I was thinking of using a MVC style of separation.  After reading this MVVM article by Josh I can see why this approach is leaned toward by the the WPF community as it is a very good fit.  I suspect that I will still lean a little toward MVC by coding the command objects which actually change the model separate from the view-models, but we will see.  The bottom line here is that this article was a great help is seeing a proper and clean way to provide the glue between the model and the view.  It was particularly eye opening to see how there really isn’t must need at all for putting “code” in the view itself.    With a little desire and some discipline you can achieve a view that is all XAML and no code.  Very tempting stuff.

Routed Events and Commands

http://msdn.microsoft.com/en-us/magazine/cc785480.aspx

Brian Noyes’ MSDN write up explaining event writing and commands in WPF is also a must read.  As with other WPF constructs they have taken something you understood to be a square and remade it as a circle.  It is all ok once you realize what is going on but seeing events sources and handlers that are not really “connected” in the traditional sense is odd at first.

With those 3 articles and their supporting code I was able to begin the process of learning the think like WPF.  I think I like it.  Another couple of weeks and I’ll know…..I’ll keep you posted.

So with the rather long initial ramp up now behind me and a realization that there is still much to learn I wanted to make sure that I really wanted to invest.  This WPF adventure is part of a larger effort on my part to put together a solid set of technologies that work well together for building enterprise apps.  I want to produce a proven and repeatable approach.  I can tell at this point that WPF is pleasing at development and design time…..but what about at run time?  Can it perform?  Can it scale?  To begin the process of answering this I put together a small test app.  This is by no means an exhaustive test, I have simply picked one type of UI scenario that I have use for and who’s performance is of the up most importance.

I have created a UI which places 64 button on screen in an 8×8 grid and then changes all 64 buttons quickly.  In the real scenario these would be click-able buttons used on a touch screen device.  In the test, the view-model simply has a timer to generate a simulated event.  This is an overly simplistic test but my approach at this time is simply that a simple test better fly far faster than needed in a real application else we have a problem.

Before we can talk about the test and the results, we need to cover 2 more articles that were useful in putting the test together.  It seems that even this simple test led me to run into some real world problems.

The test app uses an ItemControl which is bound to a collection in the view-model named AvaliableKeys.  AvaliableKeys is defined in the view-model as an ObservableCollection<Key> where Key is a model class that defines the properties of the onscreen keys such as their color.


<ItemsControl DataContext="{Binding Source={StaticResource KeyGridVM_rk}}"
 ItemsSource="{Binding Path=AvaliableKeys}"
 Height="727"
 Width="714"
 Background="AliceBlue">

Once the application is running, the contents of the Available keys collection is manipulated in the view-model and thus the on screen representation of those “keys” are also manipulated since the ItemControl is bound to that collection.

Seems like a good first test right? Put a generous number of things on screen, bind the view to them, then manipulate them in a loop. How fast can the UI keep up? You here a “but” coming don’t you….yeah it wasn’t that easy.

I used a timer and a callback to simulate the data being altered by a worker thread. This is after all closer to how I would expect a real app to work. While WPF supports this just fine for scaler properties it doesn’t support this for collections. As soon as my timer kicked off I got an exception thrown:

“NotSupportedException – This type of CollectionView does not support changes to its SourceCollection from a thread different from the Dispatcher thread.”

Ugh. Threads and UIs and message pumps and other muck.

It seems that WPF does not support the contents of a collection to which it is bound changing in another thread. The CollectionChanged event is fired and WPF catches it and then can’t deal with changing the UI bits and bobs in this non-UI thread.

Brilliant….I need a Guinness.

So I went to bed that night disgusted at how well it all sounds on paper and how quickly a simple test app shows the rough and smelly bits. But the next day I couldn’t resist looking for a solution. I found Bea….she seems cool….she tells the truth….I like Bea.  She knows and explains the issue very well and even seems genuinely saddened that it is there.

The problem

http://bea.stollnitz.com/blog/index.php?s=thread+dispatch

Now I like Bea but I was not as fond of her solution.  Perhaps in time I’ll find out there is merit in it that I’m missing now.  Bea approached the problem by creating a collection this is aware of the UI thread and forces all inserts/removals to delegate to that thread.  Yes this solves the issue but I just can’t make myself feel good about forcing all my collections in the model to carry around this UI baggage.

For a solution that fits my needs better I turned to Karl Hulme. 

The Solution

http://karlhulme.wordpress.com/2007/03/04/synchronizedobservablecollection-and-bindablecollection

Karl’s approach to the problem was to create a collection wrapper that catches all the onchanged events and proxies these events over to the UI thread.  This feels very good to me.  It allows the model to be the model with no baggage.   It is a wrapper class that does not copy the collection contents and so it should perform well and allow for future updates in the model to be made known to the UI.  It can be fit relatively easily into the view-model layer.

So with this little hiccup both understood and resolved we can get back to the test.  How does it run?

Out of the gate it’s rough, but I expected as much.  Every 250 milliseconds the test clears the collection and repopulates it.  So this means we get 4 screen refreshes of the key grid per second.  I have chosen to start here as I wanting to simulate a touch screen POS system and I think giving the user 4 responsive touch/actions per second is a real world benchmark.

The application does keep up this pace but with considerable flicker, not at all acceptable at this stage.  Here is the actual code doing this refresh.  Note that the constructor for Key() assigns it a random color.


protected void RePopulateKeys(int keyCount)
{
  lock (this)
  {
     keyList.Clear();

    while (keyCount-- > 0)
    {
       Key k = new Key();
       k.Id = keyCount;
       keyList.Add(k);
    }
  }
}

After seeing that this test could make the 4 times a second rate with flicker I took off the guvnor and let it run all out. In 60 seconds this test was able to refresh the screen 300 times or 5 times per second.   This is fast enough but just barely and we don’t have any real logic using resources yet.

As a second test I changed the view-model such that once the collection was populated it did not change again.  Rather the keys in that collection were changed in place.  This is done by a simple loop that just goes through the 64 keys and randomly changes their colors.  This test looks much better.  The flicker is completely gone and it runs fast.  In 60 seconds this test refreshes the screen 4000 times or 66 times per second.  All very smooth with no flicker.

protected void RandomizeKeysInplace()
{
 lock (this)
 {
  foreach (Key k in keyList)
  {
   k.RandomizeContents();
  }
 }
}

As a third test I changed the logic to move the existing keys around in the collection.  32 move operations each having a remove and an add such that all 64 keys are moved.  While this test also runs rather well the results are interesting when compared to the second test.  In 60 seconds this test refreshes 400 times.  This is a factor of 10 worse that test 2.

protected void RandomizeKeyPlacement()
{
 lock (this)
 {
    int moveto;

    // 32 is 1/2 the list since each is a move we cover 2 at a time
    for (int i = 0; i < 32; i++ )
    {
      moveto = i + 32;
      if(moveto>=64)
      {
        moveto = moveto-64;
      }
      keyList.Move(i, moveto);
   }
  }
 }

So it would seem that WPF is far far far more capable of performing under a load of property changes than it is under a load of collection changed.  Not overly unexpected but these collection operation were not very heavy.  I suspect it is a combination of handling the collection change coupled with the fact that it also had to remove and re-add the windows elements in this case where as in test 2 all the windows where left alone and only their properties changed.

I realize that this test is very small in terms of exercising WPF features and that it is asking WPF to do something that is not its core target.  But it is a direction I need it to perform in and the results are interesting.  I will likely add to this test in the future as I begin to rely on other WPF features.  The most immediate is that of binding/rebinding a command to each button in the loop.

It has been a mind altering experience to peel back to first few layers of mysteries that surround WPF and I hope the details here of my journey can be of use to you.

The little things

Posted on January 16th, 2010 in .NET, Architecture | No Comments »

Been a while since an update.  Busy coding on interesting things makes the blog go stale.

I’m working on a code generated ORM to manage an arbitrary domain model in what started as a bit of a laughable idea.  I had been following various forums on ORMs and was rather taken back by the volume of traffic on the boards in general as well as the amount of that traffic that showed developers having runtime issues with them.  In particular one issue jumped out at me and that was start-up performance.  Most if not all of the ORMs out there are configuration driven in some fashion and this configuration takes time to load.  I began to think that the task of providing a clean bridge from DB to In Memory Objects and back to DB again shouldn’t be “all that hard”.  Now I also have other thoughts and motivations for this project, but a primary driver I have to admit was the rather self serving “can I do this more simply”.

Three or so weeks in it is progressing well,  though I have to say it isn’t trivial.  My hats off to those producing production quality ORMs.

A basis for my approach to the problem is to store the “master copy” of the domain in a modeling tool and then use that model to simply generate the code needed to manage the domain at run time.  There are 2 primary benefits to this approach which I appreciate.  The first is that it avoids the “start up” cost issues as there is little to no configuration to load and process.  The second is that the resulting code base while large is very simple, readable, debug-able, and maintainable there by allowing even entry level developers to work with the runtime/code-base quite easily.  I often find resistance among developers to using large generic frameworks  and I think the primary source of this resistance is based in the general inability to diagnose issues when the system breaks down.  While the system I’m working on is a frameworks of sorts, it is solving the general problem of ORM in a very specific way by generating the classes and stored procedures needed to manage a specific domain model.  I’m more than willing to admit that at this point in time the verdict is out as to whether or not this is a scalable approach to the problem, but if I’m successful I do believe the end result will be very positive for developers that use it on a day to day basis.

So as I progress on this project I’m starting to actually eat my own dog food as I’m starting another endeavor on which I will use this Modeling/CodeGen/ORM technique (must find a name for it:) as a large portion of the code base.  In doing so I’m now spending a fair bit of time in the debugger looking at entities in my new domain model.  For the first time I’m looking at these not as the artifacts of my ORM tool set but as the business meaningful entities that they are.  I’m now a user of the system as apposed to the framework author.  It is amazing how quickly this changes and improves your view of a system.

I immediately noted that I did not at all like how a domain entity displayed in the debugger.  It would show me the datatype name.  But there are 2 serious issues with this.  The first is that the domain model design worked very hard to follow a POCO approach. It did this using interfaces and clean POCO classes as the root of each entity type.  Then additional plumbing was added on in a layering of subclasses which the author/developer/user of the domain should have no need or desire to see.  While a developer can not and perhaps should not be entirely divorced from a knowledge of what is supporting his entities from underneath, they should also not have it put in their face.  The second issue and the one that initially led me to looking at this problem is that the most important fundamental fact about an entity was hard to see in the debugger…….its key.  The very definition of an entity in the DDD world is that of having an immutable key which uniquely identifies the entity.  The user/developer needs to have this up front and center in the debugger.

So how can we solve this?  Please don’t say .ToString().  Yes the debugger will use a ToString() method but that just isn’t the right solution.  What if in the future there is a legitimate reason to use ToString() for another purpose and the resulting text is not debugger friendly.

There has the be a proper solution to this…..and there is.  Enter stage right…DebuggerDisplay Attribute.

Using this attribute you can specify a format string and property replacements to be used by the debugger.

So here is a snippet example of the deepest inner most class for a domain entity named “City” which the outside world sees only as the interface ICity.

internal class PersistableCity : AnotherBaseClassForCity
{
public string Name{…}
public string StateName{…}

}

This was displaying in the debugger as simply {PersistableCity}

However a simple change of the class definition to this:

[DebuggerDisplay("City Name={Name} State={StateName}")]
internal class PersistableCity : AnotherBaseClassForCity
{

}

And now it displays as {City Name=”Atlanta” State=”Georgia”}

This makes me very happy, it is a little thing but it is important.

Deep in thought on the depths of DDD and the Anti-Framework

Posted on December 29th, 2009 in Architecture | No Comments »

I’ve been continuing my quest to use the Entity Framework’s EDMX file (the conceptual portion of it) as a foundation for a DDD code gen platform.  I had to switch to VS 2010 to get around some limitations in 2008 but I have yet to hit any major stumbling blocks in the 2010 version.

I do hope to get something written up on the progress, though at this point I’m not sure when.  The project has gone from simple prototype to complex prototype and sort of taken on a life of its own.  But more on that later.

In the mean time I have read 2  worth while articles on subjects very close to what I’m driving towards.  I find these interesting not only for what they have to offer in similarity with my current project but also because each has some direction/goals that are in direct opposition to my goals.  It is always good to see what others are thinking and ponder on why your own thoughts are either similar or different.

The first is from Oleg Sych found here where he discusses using some of the EF underpinnings but bypassing the visual modeling tool.   Instead he favors using the SqlServer database diagram tool.  His main reason is that the EF tool simply can’t handle large real world models (50+ tables).  I too have had a lingering concern on this, though my goals are to use the EDMX modeler/file as a proving ground to an approach and then look to allow the source model to come from other sources.  In particular I want to look at some UML tools…but I digress.

The interesting thing for me is how he shifted toward a pure data model modeler and away from a domain model modeler.  The EF tool set does a fence sitting job in between these and I am specifically looking to work in the domain modeling space.  I want to treat the domain as the first class citizen and the relational data model as second class.  Or perhaps looking at it in a less controversial way I want to provide a means for application developers to treat the domain as first class and database developers to treat the relational data model as first class….and I want to provide a means for each to change independently.  I think that final part (change independently) is so very important and thus far I do not feel the ORM tools out there are delivering on this portion of the puzzle.

At any rate I will no doubt be keeping an eye out for when Oleg discusses this topic again in the future.

The second article is found here and is by Mats Helander.  This article is about managing a large set of domain objects and how to best deal with plumbing code for features such as dirty tracking.  This article is not new in its approach but it is very thorough and yet simple to follow.  It takes you through a long re-factoring journey from Obese Domain Model to POCO Domain Model supported by an AOP framework.  Ultimately it is an advertisement for Mats’ AOP framework NAspect (and there is nothing wrong with that).  Whether you care about DDD, AOP, or anything else architectural for that matter I would encourage you give this article a read just to exercise your OO brain cells.

I appreciate the general approach that Mats suggests for dealing with the plumbing in a domain and in many ways it looks very much like what I’m working on.  The difference is that Mats ultimate goal is that of using an AOP framework to provide proxy versions of the classes at runtime based on configuration.  Where as I am looking to code generate these at development time as plain old code and simply compile it all in.  I think one of my motivators in this is touched on in Mats article when he talks about how AOP gets a bad name and developers think it is voodoo.

In general I think frameworks of all sorts tend to get bad reputations from developers.  Frameworks almost always make somethings eaiser and others either harder or just not possible.  Developers quickly forget how it is helping them and focus on where it hinders them.  Couple this with the fact that the framework is often not debuggable by the enduser-developers and the bad reputation is made worse.  By approaching the domain plumbing using simple code generation I think you get sort of an anti-framework.  Yes it is a framework and it provides some specific benefits.  But it is really simple code (be it a lot of it for a large domain), easy to read, compiled by the enduser-developer, and easily debuggable.

In addition to being more appealing to application developers (at least that is my claim) I think this approach will provide some additional benefits as well.  The ORM portion of my project is part of the code generated plumbing under the domain.  Because this layer is defined at development time and not via configuration at runtime, I think it will avoid some of the runtime startup performance issues encountered by configuration heavy solutions.

I look forward to exploring this further as well as reading about similar endeavors.

Eric Evans is depressing…how refreshing.

Posted on December 7th, 2009 in Architecture | No Comments »

I stumbled onto this presentation this week.  Not what I was expecting.

I think he is targeting an audience who works on systems slightly larger than what I typically deal with, but the points still ring a bell very close to home.  And he addresses something non-technical which also comes close to home and that is the simple politics that often cause good software to be labeled otherwise and vice verse.

Worth the 60 minutes and the final 20 do finally make a positive point.

http://www.infoq.com/presentations/design-strategic-eric-evans

EDMX as a Domain Modeling Tool

Posted on December 6th, 2009 in .NET | No Comments »

I’ve been spending some time reviewing  the Entity Data Modeling tool in VS2008.  This tool allows for visually designing a model and then produces an xml file containing the data describing that model.  In particular I have been using it to model a simple but necessarily complex set of objects and then using T4 to produce skeletal POCO objects.

This tool is designed to be the initial point of developer interaction when working with the Entity Framework (EF).  The EF as a whole is designed to be approached more from the data side of modeling and less from the domain side of modeling.  However, this aspect of the EF is based largely in what happens from the EDMX file forward and so I’m giving the modeling tool and EDMX file a look to see how well it can be used as a basis for domain modeling as part of Domain Driven Design (DDD).

I’m not going to pontificate on the differences in these approaches to modeling except to say that I do not believe one supersedes the other.  They are simply similar but different.  With domain modeling desiring to produce a well defined model in the application and data modeling desiring to produce a well defined model in the database.  There are many discussions on this topic online if you are interested,  Here for example. Or here.

So with some of the backdrop now explained let’s move along.  I’m sitting at the end of a several day adventure of exploring EDMX and T4 as a set of tools for modeling a domain and producing C# code for that domain.  I’m doing this for the sole purpose of evaluating these tools and not to produce any particular result/product/software.

I have chosen to use the recording industry as my sample domain and as such have modeled a simple relation involving Songs, Artists, Albums (yes I’m old enough to remember these), Recordings, and Playlists.  Here is a screen shot of my example thus far.

 

RecordingIndustryDomain2 

This isn’t the most exciting or most complete domain model by any means, but it does hit on some specific points.

  1. NamedEntity is both a base type and abstract
  2. There are 1-* and *-* relations.
  3. Recording is not an entity unto itself but rather an aggregate of 3 others.
  4. PlayList::SongCount is readonly.

And then there are some points of interest I still need to add.

  1. An entity with a multi-value key.
  2. A 1-* property where the * side may be one of several types all inheriting from a common base type.
  3. A self referencing entity, i.e. a parent-child relation.
  4. You tell me.  I want to keep this model small and real world…but I want to cover a full set of complexities.  What can you suggest.

I have been generally pleased with EDMX though I will conclude with the issues I have been faced with.  I have been less pleased with T4 though I am still giving it a fair try and perhaps it will be discussed in a later post.  Though there is a lot of good information out there to help make T4 more usable.  The two main sources I have drawn from are Oleg Sych and Tangible Engineering.

Overall I have been pleased with EDMX.  The editor is integrated with VS and its free for VS users.  The xml schema it produces is generally readable, manageable, and most importantly for me appropriate for input into other tools such as code generators.  It does appear to be a good tool though there are some limitations worth noting.  Some of these are due to immaturity and others are due to my use of the tool as a domain modeler which is not its native intent.  That said, I feel that Microsoft/Visual Studio should look to facilitate DDD and as such they need to include a good modeling tool.  While this could be a separate tool from the EDMX modeler that would seem an odd fit.  If MS is to provide a good domain modeler, the most likely direction would be to grow the EDMX modeler into this role. After all, the EDMX file is broken into three main sections:  Conceptual Model, Storage Model, and Mapping Schema.  The DDD portion of this is the Conceptual Model.  With a few changes/improvements this might well be a great integrated choice for DDD.

So what limitations have I hit with EDMX?

  1. Domain Modeling is too tightly coupled with Data Modeling
  2. No support for Complex Types (Value Objects)
  3. No ability to treat an entity property as part of a key
  4. No support for custom meta data on entities

Domain Modeling is too tightly coupled with Data Modeling. While the tool does allow for modeling the domain without providing any storage model or mapping data, you get many errors produced when compiling.  I assumed this was due to the custom tool associated with the EDMX file being unable to do its typical EF work.  I simply removed the build action and the custom tool association hoping these errors would go away.  They did not.  It is unclear to me who/what exactly is raising these errors.  I see room for improvement here in having the tools more politely cater to those wanting to do strictly domain modeling.  At this point in time this is only an annoyance and is not preventing forward progress on my part.

No support for Complex Types aka Value Objects. The xsd schema supports the definition of Complex Types and allows them to be used as properties of entities, however the tools in VS2008 do not support these.  This is a major lacking as of VS2008 though it looks like VS2010 addresses this.  That said, it is surprising to me that the EDMX tools were released with out this support.  At a minimum I would have hoped a fix for such a lacking would have come in with a VS2008 service pack.  One of the aspects of DDD that I like is that at its core it is simple.  There are a very few building blocks.  So when one of those blocks is missing it is a rather large impediment.  I will be installing VS2010 and looking at the improved support shortly.

No ability to treat an entity property as part of a key. When an entity (Song) has a property which is a reference to another entity (Author is an Artist), that property can not be made part of the key.  In the case of Song/Author this is a problem as Song’s Title is not enough to make it unique.  See this site for some fun discussion of different songs of the same name.  My desire is to simply treat the author as part of the key making the author’s key and the song’s key together the unique identifier of a song.  An inability to do this seems to be a significant limitation.  If we slip out of domain modeling for a moment and over into data modeling we know this would ultimately be persisted with column(s) in the Song table which foreign key to the primary key of the Artist table.  Perhaps your database uses natural keys and this would be the artist name.  Or more likely your database uses surrogate keys and this would be that key.  In either case this reference from Song to the Artist would be part of the Song’s unique identifier.  I would be very interested to see how others have dealt with this limitation as this is by far the largest barrier to progress I have found.

No support for custom meta-data on entities. When using the EDMX file to generate code it would seem useful to be able to provide hints on an entity about the type of code to produce.  Simply custom meta-data tacked onto the entity.  One example I found myself wanting was the ability to indicate if an abstract entity was to be generated as a base class vs an interface.  At this point in time, this is not a barrier to progress for me but does seem like a good addition.

In conclusion, I look forward to continuing to explore using EDMX as a domain modeler.  I’m hopeful that the VS2010 release will move the product forward in a direction that benefits the DDD crowd.

What he said

Posted on November 29th, 2009 in Uncategorized | No Comments »

I very much enjoyed this lengthy write up on unit testing by Justin Etheredge.

http://www.codethinked.com/post/2009/11/05/Ite28099s-Okay-To-Write-Unit-Tests.aspx

I have very much appreciated my experiences with proper unit-testing, thought I admit to not having done as much of it as I would like.

Once you embrace a unit testing framework and put it to use you are implicitly forced to embrace other things…like writing units…like loose coupling…like clean interfaces…etc.

The fact that your code is getting tested is almost a nice side effect, as the real benefit is you are pushed to think like and be a better coder.  The systems you build begin to pick up steam as the units you are forced to build in order to be testable start to be truly reusable and proven.  The end results are so much bigger than just tested code.  That said, Justin’s article certainly points to the types of short comings that stop good coders from embracing the technique and thus those good coders miss the opportunity to become better coders.

I encourage you to challenge yourself to find the proper balance of unit testing in your development.

Ouch…did you hear that slap.

Posted on November 24th, 2009 in Uncategorized | 1 Comment »

Intellectual real estate is such a fickle business.

slap