Properties and Fields filtering

 

Starting from version 3.0 database supports IgnoreProperty rule. It allows filtering out some unwanted properties during tests and in production code.

 

Properties are given as lambdas: ()=>myClassInstance.MyPropertyToBeIgnored. Then all checks for the type and property names are done by the compiler.

 

  

DB db = new DB("server=(local);password=;");

 

TestTypeDefsOnly test = null;

 

// Create all rules to be saved before calling CreateDatabase.

db.TypeRules

   .IgnoreProperty(() => test.IntProperty)

   .IgnoreProperty(() => test.DecimalProperty)

   .IgnoreProperty(() => test.BoolProperty)

   .IgnoreProperty(() => test.BadProperty)

   .IgnoreProperty(() => test.FloatProperty)

   .IgnoreProperty(() => test.StringProperty)

   .IgnoreProperty(() => test.DateProperty)

   .IgnoreProperty(() => 1 == 0); // This a bad code but should not throw exceptions.

 

db.CreateDatabase("IgnorePropertyPersistence");

 

// Here you can add rules for this session only, they won't be stored.

db.TypeRules.IgnoreType(typeof(IgnoredTestType));

 

The test class is straightforward:

 

internal sealed class TestTypeDefsOnly

{

   // These properties must be ignored, you can't read them anyway.

   public int IntProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

   public DateTime DateProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

   public decimal DecimalProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

   public bool BoolProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

   public Stack<List<DateTime>> BadProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

   public float FloatProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

   public string StringProperty { get { Assert.Fail(); throw new NotImplementedException(); } }

 

   // These are properties good to save and read.

   public string Name { get; set; }

   public int Age { get; set; }

}