How to handle status integers from database in object oriented code?

Consider a database table of Items that have a status flag represented by an integer. A few of the status might be:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0 - Past Storage;
1 - Current Inventory;
5 - Scrap;
6 - Rework;
15 - Processing;
0 - Past Storage; 1 - Current Inventory; 5 - Scrap; 6 - Rework; 15 - Processing;
0 - Past Storage;
1 - Current Inventory;
5 - Scrap;
6 - Rework;
15 - Processing;

Now, I would like to avoid passing and querying for ‘magic numbers’ in my code, and in the past I have used a Dictionary to accomplish this, but this approach seems less elegant than what I hope to accomplish.

How are these types of status flags retrieved from a database handled in object oriented code? Is it with enums, and if so, how? Or is it better to create a separate table with the status flag as the primary key?

Where I work this is a common situation. What we do here is use enums AND a separate table with the status flag as the primary key. In our experience, things have been a lot easier when the primary key was not an identity field. The good thing about doing it this way is that the c# compiler has a list of valid values (the enum) and the DBAs and whoever else has to work with the data (report writers) also has a list of valid values (the table). The downside, of course, is that any additions or modifications have to be done in both places.

3

Usually integers can be represented by enums in code, as that will make it more readable for code blocks that use the integer.

Example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> public enum StatusFlag
{
PastStorage = 0,
CurrentInventory = 1,
Scrap = 5,
Rework = 6,
Processing = 15,
Unknown = 99 //Optional
}
</code>
<code> public enum StatusFlag { PastStorage = 0, CurrentInventory = 1, Scrap = 5, Rework = 6, Processing = 15, Unknown = 99 //Optional } </code>
        public enum StatusFlag
        {
            PastStorage = 0,
            CurrentInventory = 1,
            Scrap = 5,
            Rework = 6,
            Processing = 15,
            Unknown = 99 //Optional
        }

I included an optional unknown, but you may not need it. Also, this is not really necessary, you could use integers as is with something like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>if (statusflag == 15) //processing
</code>
<code>if (statusflag == 15) //processing </code>
if (statusflag == 15) //processing

But this introduces some magic number like scenarios.

Although not mandatory, there should be a table in the database that has the integer as the PK and probably a description field.

Any reference tables should have FK to the status flag lookup table. That way, one cannot insert a status flag that is not in the table.

If this is not there, there is a potential for any integer to be inserted, so there is less referential integrity to the data. The downside of this is that every new integer will have to be added to the DB and code. If you leave it as an INT with no PK/FK relationships, you can add integers ad hoc. Code changes may need to be done if you wish to incorprate the new INT with newer logic.

I tend to advocate the enum/PK-FK style as making additions should be relatively straight forward. This also ensure data integrity which is a good thing.

7

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật