Question

Suppose in a manufacturing environment, there are certain stock materials available for use in a product. For example, there are only a few different sizes of copper tube, each having a specific thickness and diameter combination. For the purposes of clean and minimal code as well as the ability to control the available options, is an enum a good fit for this combination of data? I was thinking I could make an enum with a member for each valid stock item (representing metal, physical measurements, etc.), and then provide extension methods on the enum to return the mentioned details.

The stocks available for use may change over time, so maybe an enum is not the best answer. Is there a better way to express the valid combination of related values? I am using c#.

Was it helpful?

Solution

I would probably just make a class TubeParameterCombination with read-only properties Thickness and Diameter, and provide an array or list like

List<TubeParameterCombination> validCombinations;

somewhere in the code. If you want to be absolutely sure no one changes that list afterwards once it got initialized, encapsulate it behind some kind of ValidCombinationProviderclass which allows only read-only access. What you have to decide is where and when you initialize that list.

You can either hardcode the initialization in your program (which means you have to change your program whenever the valid combinations change), read the combinations from a text file (which could be changed by a user ot an administrator), maybe just once when your program starts. Or you read the valid combinations from a database (which makes sense when your program is already using a database for such kind of data, and you want to provide a possibility for controlled changes to that list). Pick whatever suits your needs best.

OTHER TIPS

Enum in C# is meant as list of static enough items to discriminate something. If there is even chance of this list of items changing, especially in production, then using it is not appropriate. If you want specific behavior, you have to code it yourself. It shouldn't be hard to code a class or hierarchy of classes that express what you need.

Licensed under: CC-BY-SA with attribution
scroll top