Skip to content

fodyarchived/Freezable

Repository files navigation

Chat on Gitter NuGet Status

This is an add-in for Fody

Icon

Implements the Freezable pattern

Usage

See also Fody usage.

NuGet installation

Install the Freezable.Fody NuGet package and update the Fody NuGet package:

PM> Install-Package Fody
PM> Install-Package Freezable.Fody

The Install-Package Fody is required since NuGet always defaults to the oldest, and most buggy, version of any dependency.

Add to FodyWeavers.xml

Add <Freezable/> to FodyWeavers.xml

<?xml version="1.0" encoding="utf-8" ?>
<Weavers>
  <Freezable/>
</Weavers>

Add an interface

public interface IFreezable
{
    void Freeze();
}

Add a freezable class

public class Person : IFreezable
{
    bool isFrozen;
    public string Name { get; set; }

    public void Freeze()
    {
        isFrozen = true;
    }
}

What gets compiled

public class Person : IFreezable
{
    volatile bool isFrozen;
    public void Freeze()
    {
        isFrozen = true;
    }

    void CheckIfFrozen()
    {
        if (isFrozen)
        {
            throw new InvalidOperationException("Attempted to modify a frozen instance");
        }
    }

    string name;
    public string Name
    {
        get { return name; }
        set
        {
            CheckIfFrozen();
            name = value;
        }
    }
}

Icon

Icon courtesy of The Noun Project

About

Implements the Freezable pattern as part of your build

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages