In our codebase we are using enums for many states of our objects. For
example we have a TransactionStatus enum:
public enum TransactionStatus { Entered, Approved, Posted, Executed,
Canceled, ...}
which is used in our Transaction object to depict the state that the
current transaction is in:
public class Transaction { public TransactionStatus Status { get; set; }
... }
This field is mapped to a database using the underlying int datatype and
the table has a foreign key to a transaction_status code table.
CREATE TABLE trans_action_status_c (
code int IDENTITY(1,1),
name char(15),
description char(255),
sys_name char(15)
)
CREATE TABLE trans_action (--unfortunately it has always been named this
...
trans_action_status_c int not null,
...
)
where trans_action_status_c.code 1:M trans_action.trans_action_status_c
We have a test class which ensures that all the values in the database
table and in the enum are the same (loads every enum via reflection and
compares them to their related table in the db).
This enum is used anywhere in code where we want to deal with the state
of the transaction object. However when we need to display the enum to
the user, we need to display either the name column or description
column from the database (depending on the enum in question, in this
case we use the name in most places and the description in a report or
two). To solve this problem I created a CodeTable class:
public class CodeTable<T> {
public T Code { get; set; }
public string Name { get; set; }
public string Description { get; set; }
...
}
And I have created an infrastructure enabling me to get any set of code
tables based on the enum type or any individual enum name/description by
passing the enum value in to a method. The problem I have with it is
that I think it smells somewhat in that the set of code table instances
for any particular enum is simply:
List<CodeTable<EnumName>> (except that it is returned as an IEnumerable
because I never need it as a list anywhere)
generated by the List<> copy constructor given the static
IEnumerable<CodeTable<EnumName>> object that is loaded lazily on the
first time I access the database. What I really don't like is how I am
getting the individual enum instance name:
public static string GetName<T>(this T value) { //cannot use constraints
to constrain to an enum type
var instance =
GetCodeTable<T>().Where(o=>o.Code==value).FirstOrDefault();
if(instance==null) throw new InvalidApplicationSetupException("Code
table not correctly set up: " + typeof(T).Name);
return instance.Name;
}
I realize that caring for the type of collection I am using here is
entirely pointless from any kind of optimization perspective (these are
small collections), yet it bothers me that List<> is the type I am using
for this task. As I see it there are several reasonably important facts
about these collections:
1. I only ever index them based on the enum (using the where method as
an indexer)
2. They are always sorted by enum value in increasing order (so the
where could do a binary search or even be O(1) in some type of hashing
implementation)
3. The collections are immutable, only ever changing when first created
via a copy constructor based on the IEnumerable set provided by NH (this
is being done in such a way that each thread will always have its own
copy generated based on the original list so that there is no
possibility of any threading issues anywhere).
4. There is no duplicates
5. for a given enum value, a codetable instance *must* exist
6. All CodeTable instances happen to never get changed anywhere (they
are treated as being immutable by the application, theoretically if I
knew that the collection was thread safe I could get by with having only
one cached/static copy of it, the fact that the pre-copy enumerable
collection is static doesn't matter at all, it could just as easily be
cached, it was made static simply because it never changes).
The generic list datatype doesn't constrain me to any of these facts. It
seems to me that there must be a much better datatype for storing this
information. Does anybody know what type would be best to use for this?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"nhusers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/nhusers?hl=en
-~----------~----~----~----~------~----~------~--~---