score:1
In some sense, C# enums are similar to a object-oriented class hierarchy. If you declare enumeration Choice
with alternatives One
and Two
, it is a bit similar to declaring a class hierarchy with base class Choice
and two derived types (One : Choice
and Two : Choice
).
One difference is that enumerations are not extensible (if you need to add a case, you need to modify the declaration, while if you need to add a case to a class hierarchy, you simply define a new inherited type).
Second difference is that options of an enumeration cannot carry additional data - if you're declaring class hierarchy, you can store other fields in the
One
case (for example).
Without these two "limiations", you could consider using enumerations to represent entities in an application (in fact, functional languages have construct similar to these "more powerful" enumeration and use it for these kind of things).
However, since C# enumerations are very simple, they are mostly used to represent values independent of the specific domain, or values that are needed more generally in programming. For example:
enum TriState { True, False, Undecided }
This could be quite useful type, but it isn't directly related to any specific domain.
score:2
I use enums for database values that should never be created or altered by a user and if there is special logic based on the value. Currencies are not a good example unless you have every currency in your enumeration and accept that you will have to recompile your app should a new currency come along or one is deprecated. OrderStatus might be a good example if you have logic in your system based on the OrderStatus. Regardless, it should be noted that if you do create an enumeration to represent a database value, that the application must be recompiled should a new value in this table need to be created and thus this practice should be used with great caution.
In situations where I'm going to mimic a database value with an enumeration, I use the name of the enumerated value as the primary key of the table and as value passed to the foreign key. I.e., when I save to the database, I'm going to pass myEnumVal.ToString()
. I have a couple of reasons for this. First, it requires that adding a value is more deliberate. If I were to use the numeric value as the primary key, there is an implication that I'm using a surrogate key and that additional values can easily be added. When other developers see a column using a name as the primary key, there is a higher chance that they'll think that the value is not arbitrary. Second, it makes database code significantly easier to read. You get queries like Where OrderStatus = 'Active'
instead of Where OrderStatus = 1
.
Source: stackoverflow.com
Related Articles
- Do you use enums in your data applications and how?
- c# Linq or code to extract groups from a single list of source data
- The data source does not support server-side data paging
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- System.ArgumentException: Complex DataBinding accepts as a data source either an IList or an IListSource
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- What would you choose for your data layer today, Linq or Subsonic?
- Is there any way to create a LINQ query as a variable without having the data source (yet)?
- How does linq actually execute the code to retrieve data from the data source?
- LINQ Source Code Available
- In Memory Data Cache for Performance in .Net Applications
- .NET 4 Code Contracts: "requires unproven: source != null"
- Is there a way to speed up this code that finds data changes in two XML files?
- The given value of type String from the data source cannot be converted to type int of the specified target column
- Merge duplicate data without affecting others in LINQ code
- Use a linq query as microsoft local report Data Source (WinForms)
- At what point is a LINQ data source determined?
- creating Linq to sqlite dbml from DbLinq source code
- read icollection data using LINQ in C# code
- updating data in many-to-many relationship in entity framework in code first existing database
- Linq to sql as object data source - designer problem with partial classes
- How can i copy data table records of different field name based on mapping list evaluating condition on source data table?
- C# LINQ How to get a data source from a db?
- Using LINQ query result for data source for GridControl c#
- how to combine two data source into one?
- How to swap the data source associated with a Linq query?
- Entity Framework Linq - how to get groups that contain all your data
- How to Query Icollections of Entity Framework Code First Data
- ASP.net 4.0 Entity Data Model Mysql Not Treating Mysql Enums Right
- C# SQL/Linq/Entity Framework calculating column totals for multiple columns from large data source
- How do I get values from SelectedItem in ComboBox with Linq and C# 3.5
- The required column 'id' was not present in the results of a `FromSql` operation in EFcore
- Help with writing a linq query
- Create string with Objects grouped by same Property
- linq to sql + stackoverflow exception when querying objects
- system.linq groupby more than one element
- C# LINQ GroupBy error Only one expression can be specified in the select list
- Would this perform better with LINQ?
- xml Append, File.Add... overwrites original data
- How to find all classes that implements a generic abstract class using reflection in C#?
- How to use automapper in orderby clause with dynamic linq?
- Check if two lists are equal with different type
- Sorting an XElement
- Grouping linq to XML multiple hierarchies
- The specified type member "" is not supported in LINQ to Entities.
- user trimstart in entity framework query
- ASP.net c# LINQ for loop
- Linq to Entities & SQL Server 2012: Insert consecutive number by a combination of fields
- OrderBy().ThenBy().ThenBy() not giving expected result on a List of entities
- Mapper does not contain definition for CreateMap C#