Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You're entering the world of <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" rel="nofollow">Aspect Oriented Programming</a> here. You could knock together this kind of functionality in 5 minutes using <a href="http://www.sharpcrafters.com/" rel="nofollow">PostSharp</a> - 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.</p> <p>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..</p> <p><strong>EDIT</strong> In response to VoodooChilds request.. Here's a quick example of a unit test class, using NUnit and the excellent FluentAssertions library.</p> <pre><code>[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 = () =&gt; property.GetSetMethod().Invoke(target, new object[] { value }); // ShouldThrow is a handy extension method of the FluentAssetions library... setter.ShouldThrow&lt;InvalidOperationException&gt;(); } } </code></pre> <p>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!<em>ahem</em></p> <p><strong>EDIT 2</strong> 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 :)</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload