patterncsharpMinor
Is it a good practice to wrap datatypes in custom classes?
Viewed 0 times
practicedatatypescustomclassesgoodwrap
Problem
So in my current application, I was thinking to make a extended class for all the datatypes. For
These individual extended classes will expose some special functonality bound to that type. For example, this is how i think my
Similarly, I will provide
Is this considered the best practice or it is overhead for my application?
int, it will be something like ExtendedInt, for bool it will be something like ExtendedBool.These individual extended classes will expose some special functonality bound to that type. For example, this is how i think my
ExtendedInt will look like:public interface IExtended
{
bool IsModified { get; set; }
}
Public class ExtendedInt: IExtended
{
private bool m_IsModified;
private int m_MaxValue = int.MaxValue ;
private int m_MinValue = int.MinValue;
private int m_Value=int.MinValue;
private int m_MaxValue = int.MaxValue ;
private int m_MinValue = int.MinValue;
public bool IsModified
{
get { return m_IsModified; }
set { m_IsModified = value; }
}
public int Value
{
get { return m_Value; }
set
{
// Only change the value if it is different.
if (m_Value != value)
{
// Only set the value if it is in the valid range, if any max/min exists.
if ((m_MaxValue > -1) && (value > m_MaxValue))
{
throw new ArgumentException("Value is above maximum value permitted.");
}
else if ((m_MinValue > -1) && (value < m_MinValue))
{
throw new ArgumentException("Value is below minimum value permitted.");
}
else
{
m_Value = value;
this.IsChanged = true;
if (m_Parent != null) m_Parent.DirtyState();
}
}
}
}Similarly, I will provide
ExtendedBool, ExtendedDouble, ExtendedDate, ExtendedString, etc..Is this considered the best practice or it is overhead for my application?
Solution
Too much overhead - nullable types and extension methods seem to give you what you need. The checking you do on your setters will never be executed because an int will never be less than
Only create wrappers over types where you're adding intrinsic business (or technical) value that your consumers will benefit from. I'm afraid the
int.MinValue or greater than int.MaxValue to begin with.Only create wrappers over types where you're adding intrinsic business (or technical) value that your consumers will benefit from. I'm afraid the
ExtendedInt example you provide doesn't seem to meet that criteria over int? myInt;Context
StackExchange Code Review Q#8788, answer score: 5
Revisions (0)
No revisions yet.