In my database I have tables that define types for example
Table: Publication Types
ID | Type ---------- 1 | Article 2 | Abstract 3 | Book ....
Which is related through the ID key to a publication tables which has the field TypeID.
I then create a PublicationTable data table my .NET application which I want to filter based on the publication type. For example the following function gives me the number of publications for a specific author and publication type.
Public Function countPublications(ByVal authorID As Integer, _ ByVal publicationType As Integer) As Integer Dim authPubs As New PublicationsDataSet.tblPublicationsDataTable authPubs = Me.getAuthorsPublications(authorID) Dim dv As New DataView(authPubs) dv.RowFilter = "status='published' AND type='" + _ publicationType.ToString + "'" Return dv.Count End Function
To call this function to get a count of articles by an author of a specific type, I could
call the function with two integers
countPublications(authorID, 1)
setup an enum so that I can write
countPublications(authorID, pubType.Article)
or
somehow use the publication type table to filter the publication data set but I haven't got my head around how to do this.
What other approaches should I consider.
Thanks
Enum types are more convenient for coding. For infrequent releases, or if you often have new/deleted/changed values, use a database table. For static sets of values, or if you release code all the time, use an enum.
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.
An ENUM is a string object with a value chosen from a list of permitted values that are enumerated explicitly in the column specification at table creation time.
By default, when an enum is a part of an entity, JPA maps its values into numbers using the ordinal() method. What it means is that without customizations JPA stores enum value as numbers. These numbers are associated with the order in which you define values in the enum.
if publication types are essentially static, enums are fine
there is arguably little difference between embedding
inner join lookuptable lt on lt.id = (int)myenum.sometype
in a query and adding
inner join lookuptable lt on lt.name = "somehardcodeddescription"
they're both embedded constants, the former just has a well-defined type behind it
alternately you could use
inner join lookuptable lt on lt.name = myenum.sometype.ToString
i prefer the former
if, on the other hand, new lookup types may be added after the code is deployed, then an enum will quickly become outdated;
but if there is core set of static enum values that the code needs and the rest don't matter then the former solution is still fine
as usual, "it depends" ;-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With