Use enumeration in nhib.pdf

Source: Internet
Author: User

We often have this requirement: map a field into an enumeration.

As a result, nhib.pdf provides this capability. In mapping, the type is not specified, so that NH can infer it by itself. So I got my job today (hello...

Er... Continue. Under the impression, according to the above method, NH will store the int value corresponding to the enumeration in your char or varchar field.

So the customer was upset that they wanted to see a more meaningful character or even a string. Fortunately, NH provides two classes: enumchartype and enumstringtype.

First, it is easier to use enumchartype <t> to view the ing characters. Let's look at the example in the test project of NH. The entity and enumeration definitions are as follows:

Code Public class enumcharbaz
{
Private int32 ID;
Private samplecharenum type;

Public Virtual int32 ID
{
Get {return ID ;}
Set {id = value ;}
}

Public Virtual samplecharenum type
{
Get {return type ;}
Set {type = value ;}
}
}

Public Enum samplecharenum
{
On = 'n ',
Off = 'F ',
Dimmed = 'D'
}

 

HBM is as follows:

Code <Class name = "enumcharbaz" table = "bc_ecfoobarbaz">
<ID name = "ID">
<Generator class = "assigned"/>
</ID>
<Property name = "type" column = "type" type = "nhib.pdf. type. enumchartype '1 [nhib.pdf. test. typestest. samplecharenum, nhib.pdf. test], nhib.pdf "/>
</Class>

 

Let's once again strongly condemn the general expression of NH metamorphosis ~~

Let's take a look at the ing string and use the built-in enumstringtype <t>, which is also very easy. Let's look at the test example in NH.

Code Public class genericenumstringclass
{
Public Virtual int ID
{
Get;
Set;
}

Public Virtual sampleenum enumvalue
{
Get;
Set;
}
}

Public Enum sampleenum
{
On,
Off,
Dimmed
}

 

Code <Class name = "nhibloud. Test. typestest. genericenumstringclass, nhibloud. Test" table = "bc_estr">
<ID name = "ID" column = "ID">
<Generator class = "assigned"/>
</ID>

<Property name = "enumvalue" type = "nhib.pdf. type. enumstringtype '1 [[nhib.pdf. test. typestest. sampleenum, nhib.pdf. test], nhibbench "column =" enumc "/>
</Class>

 

As a result, NH stores your enumerated tostring in the database.

If you want to further customize the string content of the enumeration, You can inherit the enumstringtype. We strongly recommend that you do not inherit the enumstringtype <t> and check his code to see how useless it is.

[Serializable]
Public class enumstringtype <t>: enumstringtype
{
Public enumstringtype (): Base (typeof (t )){}
}

 

Isn't this a useful job...

For ease of use, I implemented such a base class

Code Public class genericenumstringtype <tenum>: enumstringtype where tenum: struct
{
Protected static idictionary <tenum, string> dictionary {Get; private set ;}
Static genericenumstringtype ()
{
If (! Typeof (tenum). isenum)
{
Throw new argumentexception ("tenum is not Enum type", typeof (tenum). fullname );
}
Dictionary = new dictionary <tenum, string> ();
}

Public genericenumstringtype ()
: Base (typeof (tenum ))
{
// Do nothing
}

Public override object getvalue (Object Code)
{
If (0 = dictionary. Count)
{
Return base. getvalue (CODE );
}
VaR @ Enum = (tenum) code;
If (dictionary. containskey (@ Enum ))
{
Return dictionary [@ Enum];
}
Throw new argumentexception ("cannot convert", code. tostring ());
}

Public override object getinstance (Object Code)
{
If (0 = dictionary. Count)
{
Return base. getinstance (CODE );
}
VaR STR = code. tostring ();
If (dictionary. Values. Contains (STR ))
{
Return
Dictionary. Where (pair => pair. value. Equals (STR, stringcomparison. ordinalignorecase). Select (
Pair => pair. Key). First ();
}
Throw new argumentexception ("cannot convert", STR );
}
}

 

Then, the custom enumeration to the string class is simplified as follows:

Code Public class yntypestringtype: genericenumstringtype <yntype>
{
Static yntypestringtype ()
{
Dictionary. Add (yntype. Yes, "Yes! ");
Dictionary. Add (yntype. No, "No! ");
}
}

 

HBM also maps this class and does not need to write generics :)

Okay. It's here today ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.