|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe Why to Use ClassEnumC# enum is very convenient, but it can only have one One simple example to show why we need a class implemented could be a credit card type. There are 4 common credit card types: Visa, Master, American Express and Discover Card. If you use C# public sealed class CreditCardType {
public static readonly CreditCardType AmericanExpress =
new CreditCardType("American Express");
public static readonly CreditCardType DiscoverCard =
new CreditCardType("Discover Card");
public static readonly CreditCardType Master = new CreditCardType("Master");
public static readonly CreditCardType Visa = new CreditCardType("Visa");
private readonly string name;
private CreditCardType(string name) {
this.name = name;
}
public override string ToString() {
return name;
}
}
The power of this class based implementation is that you can add properties or even functions if needed. Something missing here is the
public class CreditCardType : ClassEnumGeneric<CreditCardType>
{
public readonly static CreditCardType Visa = new CreditCardType("Visa");
public readonly static CreditCardType AmericanExpress = new CreditCardType("American Express");
private CreditCardType(string name):base(name) {}
}
Now this // static parse function
CreditCardType.Parse("Visa"); //returns CreditCardType.Visa
// static Items property that returns a collection that contains
// CredictCardType.Visa and CredictCardType.AmericanExpress
// and other CredictCardTypesif declared.
CreditCardType.Items; //returns a collection that contains
public class CreditCardType : ClassEnumGeneric<CreditCardType> {
public readonly static CreditCardType Visa = new Visa();
public readonly static CreditCardType AmericanExpress = new AmericanExpress();
protected CreditCardType(string name) : base(name) {}
public abstract bool ValidateCCNumber(string ccNumber);
private class Visa : CreditCardType {
public Visa() : base("Visa") {}
public override bool ValidateCCNumber(string ccNumber){
//....do some Visa validation
}
}
private class AmericanExpress : CreditCardType {
public AmericanExpress() : base("American Express") {}
public override bool ValidateCCNumber(string ccNumber){
//....do some AmericanExpress validation
}
}
}
Now comes another major reason why you want to use the public class CreditCardTypeUserType : ClassEnumUserType<CreditCardType>{}
Now you can map your property using the following <property name="CCType" type="YourNameSpace.CreditCardTypeUserType,
YourAssembly" column="CCTypeEnum"/>
How To Use
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||