Generic Base Class/Retaining Previous Version of An Object
I have the following base class:
using System;
using System.Collections.Generic;
using System.Reflection;
namespace CharlesLaboratory
{
public abstract class EntityPreviousState<TEntity>
{
private TEntity _previousVersion;
[NonComparable(true)]
public TEntity PreviousVersion
{
get
{
return _previousVersion;
}
set
{
_previousVersion = value;
}
}
private bool _deleted;
[NonComparable(true)]
public bool Deleted
{
get
{
return _deleted;
}
set
{
_deleted = value;
}
}
/// <summary>
/// This method returns a set of key/value pairs that contain the names and values of any properties not implementing
/// the NonComparable attribute that are different from the previous version of the type that
/// was built when it was instantiated.
/// </summary>
/// <returns>Dictionary<string,string></returns>
public Dictionary<string, string> ChangedProperties()
{
Dictionary<string, string> propertyList = new Dictionary<string, string>();
Type thisType = this.GetType();
PropertyInfo[] thisProperties = thisType.GetProperties();
Type prevType = PreviousVersion.GetType();
PropertyInfo[] prevProperties = prevType.GetProperties();
foreach (PropertyInfo thisProp in thisProperties)
{
foreach (PropertyInfo prevProp in prevProperties)
{
if (prevProp.Name == thisProp.Name)
{
if (!NonComparableProperty(prevProp))
{
if (prevProp.CanRead == true || thisProp.CanRead == true)
{
object oPrevValue = prevProp.GetValue(this, null);
object oThisValue = thisProp.GetValue(PreviousVersion, null);
if (!oPrevValue.Equals(oThisValue))
propertyList.Add(prevProp.Name, oPrevValue.ToString());
}
}
}
}
}
return propertyList;
}
private bool NonComparableProperty(PropertyInfo prevProp)
{
object[] attributesArray = prevProp.GetCustomAttributes(false);
foreach (object attribute in attributesArray)
{
NonComparableAttribute nca = (NonComparableAttribute)attribute;
if (nca != null)
{
return true;
}
}
return false;
}
/// <summary>
/// This abstract method will be implemented in all subclasses so that copy behavior specific to the type
/// can be provided.
/// </summary>
/// <returns></returns>
protected abstract TEntity CreateCopy();
}
}
Its purpose is to allow any subclasses to retain a previous version of themselves, and allow for reporting of changes, without having to hardcode property names. This class inherits it:
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Security.Permissions;
namespace CharlesLaboratory
{
public class BusinessEntityOne : EntityPreviousState<BusinessEntityOne>
{
public BusinessEntityOne()
{
}
public BusinessEntityOne(string name, Int32 code)
{
_name = name;
_code = code;
PreviousVersion = CreateCopy();
}
private string _name;
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
private System.Int32 _code;
public System.Int32 Code
{
get
{
return _code;
}
set
{
_code = value;
}
}
protected override BusinessEntityOne CreateCopy()
{
BusinessEntityOne newCopy = new BusinessEntityOne();
newCopy.Name = this._name;
newCopy.Code = this._code;
return newCopy;
}
}
}
(I haven't provided the code for the NonComparable attribute; you can infer what it does based on the code)
I use this in one solution, it works great.
In my actual development solution, my unit tests bomb, giving me a VerificationException.
I have reached my wit's end. Help, anyone?

