c# - Most effective way to create a class that has a small variance in derived classes -


i have class contains lot of functionality called record. in system there exist 2 basic types of records have primary key uint, , have primary key of guid.

the record class contains features need except 1 property. record.id allows developer read current primary key correct type (uint or guid).

to achieve had create 3 classes (simplified example):

class record {     ... lots of code ... }  class recordint : record {      public uint id { get; set; } }  class recordguid : record {      public guid id { get; set; } } 

this works ok, introduced problem data models can't create generic record instance. have know if derive classes expecting recordint or recordguid. added template argument models this.

class abstract class model<t> : t : record, new() {    ... lots of generic code uses t ...     // example    public list<t> find(...) {        .. find record ...       return new list<t>();     } } 

this has caused me more problems benefit of having 2 record types. when instantiate model code has know type of record required. if takes place in section should generic, have add template argument.

i found after while there started lot of methods using template arguments. type passed model<recordguid>, , have pass template argument down chain of several method calls befor gets code needs it.

is there more effective pattern i'm doing?

edit:

one reason why model using template argument needs have methods return collections of record. in c# can't cast list<record> list<recordguid> had use template argument return type list<t>.

i decided create new class represent primary keys, , have class control type state id.

this allow me create single class represent record while class support either uint or guid primary keys. means model class no longer required template arguments resulted in other source code dependant upon model not require template arguments.

here primaryvalue class i'm using.

/// <summary> /// used store value of primary key table. /// </summary> public sealed class primaryvalue {     /// <summary>     /// raw value     /// </summary>     private object _value;      /// <summary>     /// required type.     /// </summary>     private type _type;      /// <summary>     /// true if guid type.     /// </summary>     public bool isguid     {                 {             return _type == typeof(guid);         }     }      /// <summary>     /// type if uint type.     /// </summary>     public bool isinteger     {                 {             return _type == typeof(uint);         }     }      /// <summary>     /// true if value empty.     /// </summary>     public bool empty     {                 {             if (_type == typeof(uint))             {                 return (uint)_value == 0;             }             return (guid)_value == guid.empty;         }     }      /// <summary>     /// constructor     /// </summary>     public primaryvalue(type ptype)     {         _type = ptype;         if (ptype == typeof(uint))         {             _value = 0;         }         else if (ptype == typeof(guid))         {             _value = guid.empty;         }         else         {             throw new modelexception("type not supported primaryvalue.");         }     }      /// <summary>     /// uint constructor.     /// </summary>     public primaryvalue(uint pvalue)     {         _value = pvalue;         _type = typeof(uint);     }      /// <summary>     /// guid constructor     /// </summary>     public primaryvalue(guid pvalue)     {         _value = pvalue;         _type = typeof(guid);     }      /// <summary>     /// copy constructor.     /// </summary>     public primaryvalue(primaryvalue pvalue)     {         _value = pvalue._value;         _type = pvalue._type;     }      public void set(primaryvalue pvalue)     {         if (_type == pvalue._type)         {             _value = pvalue._value;             return;         }          throw new modelexception("primaryvalues not of same type.");     }      /// <summary>     /// assigns uint value.     /// </summary>     public void set(uint pvalue)     {         if (_type == typeof(uint))         {             _value = pvalue;             return;         }          throw new modelexception("primaryvalue not uint type.");     }      /// <summary>     /// assigns guid value.     /// </summary>     public void set(guid pvalue)     {         if (_type == typeof(guid))         {             _value = pvalue;             return;         }          throw new modelexception("primaryvalue not guid type.");     }      /// <summary>     /// returns raw value.     /// </summary>     public object get()     {         return _value;     }      /// <summary>     /// gets id uint.     /// </summary>     public uint tointeger()     {         if (_type != typeof(uint))         {             throw new modelexception("primaryvalue not uint type.");         }         return (uint)_value;     }      /// <summary>     /// gets id guid.     /// </summary>     public guid toguid()     {         if (_type != typeof(guid))         {             throw new modelexception("primaryvalue not guid type.");         }         return (guid)_value;     }      /// <summary>     /// checks if 2 ids equal.     /// </summary>     public static bool operator ==(primaryvalue a, primaryvalue b)     {         if (a._value.gettype() == b._value.gettype())         {             return a._value == b._value;         }          throw new modelexception("can not compare primaryvalues of different types.");     }      /// <summary>     /// checks if 2 ids not equal.     /// </summary>     public static bool operator !=(primaryvalue a, primaryvalue b)     {         if (a._value.gettype() == b._value.gettype())         {             return a._value != b._value;         }          throw new modelexception("can not compare primaryvalues of different types.");     }      /// <summary>     /// convertion uint.     /// </summary>     public static implicit operator uint(primaryvalue a)     {         return a.tointeger();     }      /// <summary>     /// convertion guid.     /// </summary>     public static implicit operator guid(primaryvalue a)     {         return a.toguid();     }      /// <summary>     /// convertion string.     /// </summary>     public static implicit operator string(primaryvalue a)     {         return a._value.tostring();     }      /// <summary>     /// convert string.     /// </summary>     public override string tostring()     {         return _value.tostring();     }      /// <summary>     /// hashcode     /// </summary>     public override int gethashcode()     {         return _value.gethashcode();     } } 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -