Reputation: 1625
We're currently developing an application (C#, .Net 4.0) which requires the handling of various assets. In order to keep track of an asset's state, we developed an "AssetState" class, which returns the various states an asset can be in:
/// <summary>
/// Represents the states an asset can be in.
/// </summary>
public class AssetState
{
/// <summary>
/// Initializes a new instance of the <see cref="AssetState"/> class.
/// </summary>
public AssetState()
{
}
#region Properties
/// <summary>
/// Gets a normal asset state.
/// </summary>
public static AssetState None
{
get
{
return new AssetState();
}
}
/// <summary>
/// Gets a dirty asset state.
/// </summary>
public static AssetState Dirty
{
get
{
return new AssetState();
}
}
(etc...)
#endregion Properties
#region Methods
/// <summary>
/// Overloaded operator used to combine two states into a new one.
/// </summary>
/// <param name="leftOperandState">The left operand in the equation.</param>
/// <param name="rightOperandState">The right operand in the equation.</param>
/// <returns>A new asset state, which is the AND combination of both operands, in the form of a list of states.</returns>
public static List<AssetState> operator &(AssetState leftOperandState, AssetState rightOperandState)
{
if (leftOperandState == None && rightOperandState != None)
{
return new List<AssetState> { rightOperandState };
}
if (leftOperandState != None && rightOperandState == None)
{
return new List<AssetState> { leftOperandState };
}
if (leftOperandState == None && rightOperandState == None)
{
return new List<AssetState> { leftOperandState };
}
return new List<AssetState> { leftOperandState, rightOperandState };
}
/// <summary>
/// Overloaded operator used to combine two states into a new one.
/// </summary>
/// <param name="leftOperandStates">The left operand in the equation.</param>
/// <param name="rightOperandState">The right operand in the equation.</param>
/// <returns>A new asset state, which is the AND combination of both operands, in the form of a list of states.</returns>
public static List<AssetState> operator &(List<AssetState> leftOperandStates, AssetState rightOperandState)
{
var newAssetState = new List<AssetState>();
newAssetState.AddRange(leftOperandStates);
newAssetState.Add(rightOperandState);
return newAssetState;
}
#endregion Methods
}
An "Asset" class will contain a list of AssetStates. As such, an item could be flagged as "Dirty" and "CheckedOut", for example. When we need to determine an asset's state, we simply iterate through that list and determine if a particular state (or set of states) are present.
In the Asset Class:
/// <summary>
/// Method which determines if the asset is in a particular state.
/// </summary>
public bool IsInState(AssetState assetState)
{
return States.Contains(assetState);
}
/// <summary>
/// Method which determines if the asset is in a particular combination of states.
/// </summary>
public bool IsInStates(IEnumerable<AssetState> assetStates)
{
if (assetStates == null)
{
throw new ArgumentNullException("assetStates");
}
// Determine if this asset is in all the states requested.
return assetStates.All(assetState => assetState != null && this.IsInState(assetState));
}
Is there a better way to approach this problem ? Are there any major pitfalls in the system we developed, which we overlooked ? (while keeping in mind that the code here isn't final, but a rough draft).
Upvotes: 0
Views: 326
Reputation: 113352
For such a small number of possible states to combine, use an enum
rather than a class.
[Flags]
enum AssetState
{
None = 0,
Dirty = 1,
CheckedOut = 2,
RequiresAudit = 4
}
//Create new state for checked out and requires audit
AssetState state = AssetState.CheckedOut | AssetState.RequiresAudit;
//Set dirty without changing rest of state:
state |= AssetState.Dirty;
//Check if is "dirty":
bool isDirty = (state & AssetState.Dirty) != AssetState.None;
//Check if is "dirty" alternative method:
bool isDirty = state.HasFlag(AssetState.Dirty);
You could also add values for common combinations to the enum definition, such as CheckedOutAndDity = 3
Upvotes: 3
Reputation: 3985
It looks like you would be better off using an enum with a Flags attribute. Take a look here.
With the Flags attribute you will be able to use & and | operators. Here is an example:
// make a dirty & checked-out state
AssetState state = AssetState.Dirty | AssetState.CheckedOut;
// check if state contains Dirty
if ((state & AssetState.Dirty) != 0)
{
// handle the dirty state
}
Be careful what values you assign to your enum. They should be 1, 2, 4, 8, 16, etc. otherwise you won't be able to combine them properly using logical operators.
Upvotes: 5