HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Supporting Enum in EF 5 with .Net 4.0

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withenumnetsupporting

Problem

I'm currently working through Getting Started with EF5 using MVC 4 in Visual Studio 2010 (.NET 4.0) to learn about how to use ASP.NET and Entity Framework. During the beginning of the tutorial I ran into an issue with enum support when creating a database with Code First Migrations. For reasons unknown I have to work in this environment and create my own support for enums so I decided to make the property int instead then I would convert it to Enum when I want to display it like so:

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq;
using System.Linq.Expressions;

namespace ContosoUniversity.Extensions
{
    public static class Extensions
    {
        public static String DisplayAsEnum(this HtmlHelper cHtml,
                                                           Expression> cExpression,
                                                           Type cType,
                                                           String strDisplay = "Unknown")
        {
            try
            {
                if (cType.IsEnum)
                {
                    object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

                    if (cResult != null)
                    {
                        strDisplay = Enum.ToObject(cType, cResult).ToString();
                    }
                }
            }
            catch (Exception cExc)
            {
                strDisplay = cExc.ToString();
            }

            return (strDisplay);
        }
    }
}


In my razor cshtml file I display it to the webpage with this call:

@Html.DisplayAsEnum(modelItem => item.Grade, typeof(Grade), "No Grade")


I have a feeling that this is not the "professional" or "right" way of handling the lack of enum support so I was wondering if there is anyway to improve this function or is there a better way of handling enumerations in EF 5 with .Net 4.0 support?

I was shown a Stack Overflow

Solution

Ideally you won't want to change the strDisplay, so we should probably take it out of the parameter list and make it a constant inside the method and then just return in each of the 3 circumstances.

var display = "Unknown";

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return display;


this seems weird to me that we are using the variable (whatever you choose to name it) only once, I would probably in this circumstance (since we aren't allowing it to be changed) just return the string Unknown like this

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return "Unknown";

Code Snippets

var display = "Unknown";

try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return display;
try
{
    if (cType.IsEnum)
    {
        object cResult = cExpression.Compile().DynamicInvoke(cHtml.ViewData.Model);

        if (cResult != null)
        {
            return Enum.ToObject(cType, cResult).ToString();
        }
    }
}
catch (Exception cExc)
{
    return cExc.ToString();
}

return "Unknown";

Context

StackExchange Code Review Q#64902, answer score: 5

Revisions (0)

No revisions yet.