Using a Behavior to Aggregate Values in a Master-Detail view in M-V-VM
It is very difficult to handle situations in a master-detail view, where the master side displays a value derived from aggregating the detail collection. The Model-View-ViewModel pattern makes this even more challenging, since most means of handling this require changing your model, or using extensive code behind.
Using Behaviors to Allow the ViewModel to Manage View Lifetime in M-V-VM
One common issue people face in the Model-View-ViewModel pattern is how to cleanly deal with the lifetime of the View from within the ViewModel. Keeping all of the logic in the ViewModel for preventing the View from closing can be a daunting task, especially at first.
About Big-O Notation – The good, the bad, and the confusing
Often, prior to using a specific algorithm, it helps to understand information about how the algorithm scales. In order to document this, computer science has borrowed a notation from mathematics called Big-O notation.
Understanding Big-O notation is critical in making good decisions about algorithms or libraries used to implement routines. However, there is often a lot of misunderstanding about what it means, and I frequently see this used as justifications for poor decisions.
Preventing Event-based Memory Leaks – WeakEventManager
Events are a common source of memory leaks in .NET applications.
When an object (the receiver) subscribes to an event on another object (the sender), the sender holds a reference to the receiving object. This is because the standard event mechanism in .NET works based off delegates, which contain two member fields – _target and _methodPtr. In this case, _target acts as a strong reference to the receiver. Later, if the receiver is no longer needed, it must unsubscribe from the event, or the event itself will keep the receiving object rooted, preventing the garbage collector from reclaiming its memory. This can lead to large memory leaks – especially if the receiver itself holds large amounts of memory.
Normally, this isn’t a problem; its easy to just unsubscribe from the event, which prevents the above from occurring. However, there are times when the useful lifetime of a receiver of an event isn’t tied to the sender at all, which can make unsubscribing very difficult. WPF introduced a new class and pattern for handing this situation: the WeakEvent Pattern.
System.WeakReference internals and side-effects
After discussing the uses of WeakReference, I decided to dig into the internal implementation in .NET a bit more. I thought I’d write a short bit about how the WeakReference class works internally, and some side effects of using this vs. a standard strong reference.
Working with the GC instead of against it – System.WeakReference in .NET
While garbage collection isn’t the thing that sets .NET above development in C++ in my mind, it is a major factor. The garbage collector in .NET is a wonderful thing. It makes development much cleaner, safer, and more enjoyable in general. Not only do you get great developer productivity boons, you also have a level of safety that just makes writing memory intensive code more fun.
The GC in the CLR is fantastic – until it isn’t. As much as I love the garbage collector, it also can have some pretty nasty side effects, notably causing memory leaks that are sometimes difficult to trace and fairly painful to correct.
Visual Studio 2010 Beta
Microsoft just released Visual Studio 2010 Beta to the public at large. Now we just have to wait for the final release…
Just keep repeating: “C# is not Java. C# is not C++.”
Vicente Cartas’s latest blog post on Singleton’s in C# (original post in Spanish) illustrates something very important for learning C# well – there are subtle, but very important differences in C# which are very valuable to learn if you’re coming from a Java or C++ background. This is a classic example – the traditional C++ forms of the Singleton pattern are not appropriate in C#.
C# and .NET make life a lot easier, in a lot of ways… but it’s critical to understand the subtle nuances caused by the abstraction layers .NET provides.
Great news on the parallel front for VS 2010
I just saw Daniel Moth’s latest post on new features in Visual Studio 2010, and I got excited and decided that I had to share.
IDisposable Part 5 – Using IDisposable Classes in C#
For part 5 of my series on IDisposable, I want to focus on using IDisposable objects. We’ve seen many of the ways and reasons you might create an IDisposable class. Now, we’ll use the classes we’ve created.