Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:20:15+00:00 2026-05-25T19:20:15+00:00

My question is based on this article . Basically a class can implement a

  • 0

My question is based on this article.

Basically a class can implement a Freezable method to make sure that no properties can be changed once the object enters the Frozen state.

I have an interface that follow this design

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

the objective is to make sure that once the Freeze method is called, the IsFrozen property is set to True and the properties of the object cannot be changed anymore.

To simplify, I will be using an abstract base class:

public abstract class BaseFreezableModel : IFreezableModel
{
    public void Freeze()
    {
        _isFrozen = true;
    }
    public bool IsFrozen
    { 
       get {return _isFrozen;}            
    }
    protected ThrowIfFrozen()
    {
       if (IsFrozen)
           throw new Exception("Attempted to change a property of a frozen model");
    }
}

this way I can have a class like

public class MyModel : BaseFreezableModel
{
     private string _myProperty;
     public string MyProperty
     {
        get{return _myProperty;}
        set 
        {
           ThrowIfFrozen();
           _myProperty = value;
        }
     }
 }

This is all nice and simple, but which strategy can I adopt to make sure all properties follow the pattern above? (apart from writing setters and getters)

These are the alternatives I came up with:

  • Find a mechanism to inject a method into each property setter using emit perhaps. But I have no idea how to do it, what potential issues I may encounter (and therefore how long it will take). If someone knows this and can point me in that direction, it would be great.

  • Use some templates during the build process so that the Call to OnCheckFrozen is inserted just before compile time. This has the advantage of being really simple to understand and can work for similar scenarios.

  • Find a framework that can do all of this for me, but it just as an extreme case as I am not allowed to use external framework on this project.

What solutions would you use to accomplish this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:20:16+00:00Added an answer on May 25, 2026 at 7:20 pm

    You’re entering the world of Aspect Oriented Programming here. You could knock together this kind of functionality in 5 minutes using PostSharp – but it seems you’re not allowed to use external frameworks. So then your choice comes down to implementing your own very simple AOP framework, or just biting the bullet and adding checks to every property setter.

    Personally I’d just write checks in ever property setter. This may not be as painful as you expect. You could write a visual studio code snippet to speed up the process.. You could also write a smart unit test class which would, using reflection, scan through all the properties of a frozen object and attempt to set a value – with the test failing if no exception was thrown..

    EDIT In response to VoodooChilds request..
    Here’s a quick example of a unit test class, using NUnit and the excellent FluentAssertions library.

    [TestFixture]
    public class PropertiesThrowWhenFrozenTest
    {
        [TestCase(typeof(Foo))]
        [TestCase(typeof(Bar))]
        [TestCase(typeof(Baz))]
        public void AllPropertiesThrowWhenFrozen(Type type)
        {
            var target = Activator.CreateInstance(type) as IFreezable;
    
            target.Freeze();
    
            foreach(var property in type.GetProperties())
            {
                this.AssertPropertyThrowsWhenChanged(target, property);
            }
        }
    
        private void AssertPropertyThrowsWhenChanged(object target, PropertyInfo property)
        {
            // In the case of reference types, setting the property to null should be sufficient
            // to test the behaviour...
            object value = null;
    
            // In the case of value types, just create a default instance...
            if (property.PropertyType.IsValueType)
                value = Activator.CreateInstance(property.PropertyType);
    
            Action setter = () => property.GetSetMethod().Invoke(target, new object[] { value });
    
            // ShouldThrow is a handy extension method of the FluentAssetions library...
            setter.ShouldThrow<InvalidOperationException>();
        }
    }
    

    This method is using a parameterized unit test to pass in the types being tested, but you could equally encapsulate all of this code into a generic base class (where T : IFreezable) and create extended classes for each type being tested, but some test runners don’t like having tests in base classes.. *ahem*Resharper!ahem

    EDIT 2 and, just for fun, here’s an example of a Gherkin script which could be used to create much more flexible tests for this kind of thing 🙂

    Feature: AllPropertiesThrowWhenFrozen
        In order to make sure I haven't made any oversights in my code
        As a software developer
        I want to be able to assert that all properties of a class throw an exception when the object is frozen
    
    Scenario: Setting the Bar property on the Foo type
      Given I have an instance of the class MyNamespace.MyProject.Foo
        And it is frozen
      When I set the property Bar with a value of 10
      Then a System.InvalidOperationException should be thrown
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Based on this question it appears that the default template for CheckStyle will allow
Ok this is more of a computer science question, than a question based on
Based on this question I don't want to litter my ready stuff waiting for
This question is based on another question of mine (thankfully answered). So if in
Based on the response to this question: Why does C++ have header files and
The same as this question but for java Update Based on the comments and
Being new to test based development, this question has been bugging me. How much
For the purposes of this question, the code base is an ASP.NET website that
This is a purely theoretical question. Given three simple classes: class Base { }
Previously I asked this question: Read quicktime movie from servlet in a webpage? Basically

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.