There are a few situations in the project I am working on where I need to "enumerate" a few of my persistent classes and I was wondering how other people were tackling this problem.
So for example, let's say we have a persistent class ProductType. Then let's say there are 2 types (and that is unlikely to change), TypeA and TypeB.
So in the style of an enumerator, we want to be able to reference ProductType.TypeA and ProductType.TypeB in our domain model. For example, we might want:
if( order.Contains( ProductType.TypeA ) )
{
// Some logic here...
}
Let's now say that ProductType has a Name property whose value we want to draw from the database. So an enumerator won't do because we won't have access to the Name property.
One approach is to use public static readonly fields on the ProductType class:
public static readonly ProductType TypeA = new ProductType( 1, "Type A" );
public static readonly ProductType TypeB = new ProductType( 2, "Type B" );
The problem with this approach is that we have the Name property embedded in the code as well as in the database. This problem grows substantially as the number of properties grows and associations are added to other persistent classes.
Is there a "best practice" approach to handling this situation?
|