Search code examples
c#immutabilityfreezable

Any "Gotchas" with using Freezable class outside of WPF?


I've been reading up on concurrency, and looking at things from a more "thread safe" point of view. WPF (or actually System.Windows.Freezable and others) has a freezable class, which can give "popsicle immutablity". Has anyone tried using this outside of WPF/Silverlight and would it be better to use this, or roll your own/use someone else's? I know there's a few good ones out there.


Solution

  • You should not use the Freezable type in System.Windows outside the WPF.

    The reason for that is that you create a dependency to WindowBase.dll (or where Freezable is defined). And such a reference should not exist in "model projects" without direct access to the UI.

    However, you can easily code your own Freezable base class.

    I used the following interface in an application where I wanted to create thread-safe objects which required complicated initiation (it were circular references):

    public interface IFreezable
    {
        bool CanFreeze
        {
            get;
        }
        bool IsFrozen
        {
            get;
        }
    
        void Freeze();
    }
    

    Note the CanFreeze property: I decided to use it as I wanted to validate the Freezables before freezing - and not giving the client a chance to do so is not good in my opinion.

    The concept of Freezables is IMO a nice idea which enriches the palette of tools in multi-threaded applications.