Zināšanu bāze

Izstrādātāja ikdiena, darbs ar Enum

Labdien,

Šoreiz iemūžināšu vienu koda gabalu, kas var būt visai noderīgs Web izstrādātājiem. Lieta tāda, ka ikdienā bieži nododam argumentus izmantojot URL. Piemēram: “list.aspx?status=Enabled”.

Lapas apstrādes kodā parasti var atrasts ko līdzīgu uz šo te piemēru:

var status = Request["status"] ?? "Disabled";
switch(status)
{
    case "Enabled":
        // Code;
        break;

    case "Disabled":
        // Code
        break;
}

Tāds īsts PHP variants. Ir visai liela varbūtība pieļaut kļūdu. Vai nav daudz OOP draudzīgāks variants?

Šinī gadījumā var labi noderēt Enum tips. Izveidojam pārskaitījumu ar nosaukumu Status.

public enum Status
{
    Disabled,
    Enabled
}

Pārveidojam jau iepriekš apskatīto kodu, lai pārveidotu tekstu par konkrētāku Enum tipu.

var status = (Status)Enum.Parse(
    typeof(Status), Request["status"] ?? "0");

switch(status)
{
    case Status.Enabled:
        // Code;
        break;

    case Status.Disabled:
        // Code
        break;
}

Tagad kods ir daudz kontrolējamāks, pārbaudes veicam ar pārskaitījuma vērtībām. Jāatzīmē, ka abos gadījumos, kodā vel būtu jāveic dažas papildus pārbaudes. Kas tur tāds, principā jau nav lielas atšķirības abās realizācijās. Daļēji tā arī ir. Bet gadījumā ar pārskaitījumiem, mēs šo te funkcionalitāte varam padarīt par universālāku izmantojot Generic iespējas. Izveidosim jaunu palīg klasi.

public static class Enum<T>
{
    public static T Parse(string value)
    {
        Type t = typeof (T);

        if (!t.IsEnum)
        {
            throw new ArgumentException(
                String.Format("{0} is not an Enum", t.Name));
        }

        string s = value ?? "";

        if (Enum.IsDefined(t, s))
        {
            return (T) Enum.Parse(t, s);
        }

        return default(T);
    }
}

Tagad mums pavērās iespēja izmantot šādu te funkcionalitāti visur, kur mēs apstrādājam argumentus. Vēlreiz pārveidojam mūsu piemēru.

var status =
    Enum<Status>.Parse(Request["status"]);

switch(status)
{
    case Status.Enabled:
        // Code;
        break;

    case Status.Disabled:
        // Code
        break;
}

Tagad kods ir maksimāli drošs lietošana. Protams pēc vajadzības klasi Enum<T> vel var papildināt ar funkcionalitāti, bet tas jau pēc vajadzības. Galvenais mans uzdevums novirzīt domāšanu pareizā virzienā:)

Cerams noderēs!


Posted Mar 10 2010, 07:11 PM by andrejs.mamontovs

Comments

google+ circles wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-24-2012 10:18

W1SUK2 Thanks so much for the blog post.Really thank you! Keep writing.

microblogging submission wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 07-01-2012 6:26

CT4arm I really like and appreciate your blog. Fantastic.

amazing services wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 08-08-2012 15:28

LZBzAZ I really liked your article post.Really looking forward to read more.

crork wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 09-09-2012 13:27

gGYFmi Really appreciate you sharing this post.Really looking forward to read more. Want more.

bookmarking submission wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 09-24-2012 15:08

t3Lmvt Thanks-a-mundo for the blog post.Much thanks again. Fantastic.

cheap bookmarking service wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 10-07-2012 13:01

2jcFrL Really appreciate you sharing this article post.Really thank you! Much obliged.

bookmarking service wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 10-26-2012 1:59

rij7rZ Very neat article.Really looking forward to read more. Really Cool.

bookmarking service wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 11-09-2012 15:14

V0Xx40 Great post.Much thanks again. Cool.

Link Building Service wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 12-07-2012 23:38

krAPpS I value the post.Really thank you! Will read on...

destockchine wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 01-09-2013 0:24

It's the best you do not considering who you are, but nevertheless , considering what individual My organization is individuals in the morning together with you.

destockchine www.destockchinefr.fr/.../serie-a-pas-cher

archeage gold wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 02-27-2013 12:48

Undeniably believe that which you said. Your favorite reason seemed to be on the internet the easiest thing to be aware of. I say to you, I definitely get irked while people consider worries that they just do not know about. You managed to hit the nail upon the top and defined out the whole thing without having side effect , people can take a signal. Will probably be back to get more. Thanks archeage gold www.cheap-archeage-gold.com

destock jeans wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-04-2013 6:03

Fancy will be the primary rational together with n acceptable solution in real your life. destock jeans http://www.g77.fr/

Code Promo Spartoo wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-14-2013 16:56

I'm keen on happened caused by you, but nonetheless , caused by who actually Now i am as i here's with you. Code Promo Spartoo http://www.c88.fr/

bookmarks wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-15-2013 13:55

eSX0ZZ I loved your blog.Thanks Again. Really Cool.

casquette swagg wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-16-2013 22:11

An accurate close friend will be people who overlooks your current setbacks and therefore tolerates your current positive results. casquette swagg http://www.b77.fr/

casquette snapback wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-16-2013 23:29

Your uncle definitely isn't a fellow worker, but yet a fellow worker will almost always be a real uncle. casquette snapback http://www.a77.fr/

destock sport et mode wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-17-2013 20:51

Add‘testosterone levels attempt overtime, the most efficient points arrive when you finally very least are expecting those to. destock sport et mode www.ruenike.com/vetement-femme-c-16.html

buy social bookmarks wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 03-23-2013 20:51

oNWsFd Major thankies for the blog.Really thank you! Fantastic.

comedy channel wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 04-06-2013 21:28

Thanks for the blog post.Really looking forward to read more. Cool.

veste redskins wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 04-06-2013 21:54

I prefer you do not simply because of what you do, except simply because of of which I'm just presents 'm on hand. veste redskins http://www.niketnfrancais.com/

maillot de bain femme wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 04-15-2013 9:17

Have a passion for is normally fallible at birth and labor, yet it builds more potent as we grow old when it is the right way provided. maillot de bain femme http://www.nikerequin2013.com/

christian louboutin outlet store wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 06-03-2013 5:33

You Could Try These Out

gucci outlet online wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 06-04-2013 2:43

Have A Peek At These Guys

Gucci Handtaschen Online wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 06-06-2013 21:14

You Can Find Out More

bottega veneta online shop wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 06-14-2013 6:05

Check These Guys Out

michael kors wrote re: Izstrādātāja ikdiena, darbs ar Enum
on 06-16-2013 8:42

thank you for share!

Add a Comment

(obligāts)  
(brīvizvēles)
(obligāts)  
Remember Me?