📄 dataobject.cs
字号:
using System;
namespace HardwareDistributor.Business
{
public class DataObject
{
#region Fields
protected ObjectState _objectState;
#endregion
#region Constructor(s)
/// <summary>
/// A data object created with a no parameter constructor is a new object.
/// </summary>
public DataObject()
{
_objectState = ObjectState.New;
}
#endregion
#region Properties
/// <summary>
/// State of the object either new, unchanged, modified, or marked for
/// deletion.
/// </summary>
public ObjectState ObjectState
{
get
{
return _objectState;
}
set
{
_objectState = value;
}
}
#endregion
#region Methods
/// <summary>
/// This method is used to modify a primary key.
/// </summary>
/// <param name="currentValue">current value of the property</param>
/// <param name="newValue">new value</param>
/// <param name="propertyName">name of the property</param>
/// <returns>the new value if changed otherwise the old value</returns>
/// <exception cref="System.ApplicationException">
/// This is thrown if the object is in a delete state.
/// </exception>
/// <remarks>
/// This method is used to modify a primary key. This property can only
/// be changed when it is null. After that it cannot be changed. In
/// addition, when it is changed the object is not put in a modified
/// object state since the update is just putting the object into the
/// same state as the database where the primary key was set.
/// </remarks>
protected T ChangeKey<T>(T currentValue, T newValue, string propertyName)
{
T result = currentValue;
CheckObjectState(propertyName); // just in case
// Property must be null currently.
if (currentValue == null)
{
if (newValue != null)
{
// Only modify the value if it is different
result = newValue;
}
}
return result;
}
/// <summary>
/// This is a generic method used to modify values.
/// </summary>
/// <param name="currentValue">current value of the property</param>
/// <param name="newValue">new value</param>
/// <param name="propertyName">name of the property</param>
/// <returns>the new value if changed otherwise the old value</returns>
/// <exception cref="System.ApplicationException">
/// This is thrown if the object is in a delete state.
/// </exception>
/// <remarks>
/// This is a generic method used to modify values. It checks to make
/// sure the object is not in a deleted state. Deleted object
/// properties cannot be changed. If the values are different the new
/// value will be assigned and the object's state is modified.
/// </remarks>
protected T ChangeValue<T>(T currentValue, T newValue, string propertyName)
{
T result = currentValue;
CheckObjectState(propertyName);
// Since the type is not really known make sure currentValue is not
// null before testing for equality
if (currentValue != null)
{
// Only modify the value if it is different
if (!currentValue.Equals(newValue))
{
result = newValue;
ModifyState(ObjectState.Modify);
}
}
else
{
// if currentValue is null and newValue is not then they are
// not equal so change the value.
if (newValue != null)
{
result = newValue;
ModifyState(ObjectState.Modify);
}
}
return result;
}
/// <summary>
/// This method checks to make sure the object is not in a deleted
/// state. Deleted object properties cannot be changed.
/// </summary>
/// <param name="propertyName">Name of the property</param>
/// <exception cref="System.ApplicationException">
/// This is thrown if the object is in a delete state.
/// </exception>
protected void CheckObjectState(string propertyName)
{
if (_objectState== ObjectState.Delete)
{
throw new ApplicationException("Cannot change property, " + propertyName +
", because object is marked for deletion.");
}
}
/// <summary>
/// Marks the object to be deleted.
/// </summary>
public void Delete()
{
ModifyState(ObjectState.Delete);
}
/// <summary>
/// This method is used to ensure the object is in the correct state.
/// When an object is in a new state it can only be marked delete.
/// </summary>
/// <param name="newState">The new object state being requested</param>
protected void ModifyState(ObjectState newState)
{
switch (newState)
{
case ObjectState.Delete :
_objectState= ObjectState.Delete;
break;
case ObjectState.Modify :
if (_objectState== ObjectState.Unchanged)
{
_objectState= ObjectState.Modify;
}
break;
}
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -