This is just a little writeup of how we handle and store Enums. Maybe this will help other devs. We save our Enums in the Database, and long story short there is an UML, an example to create the DB Tables with example entries. Short explanation after the code.
CREATE TABLE EnumItemCategories (
Id bigint NOT NULL,
CategoryName nvarchar(255) NOT NULL,
TextEn nvarchar(255) NOT NULL,
TextDe nvarchar(255),
PRIMARY KEY (Id));
CREATE TABLE EnumItems (
Id bigint NOT NULL,
EnumItemCategoryId bigint NOT NULL,
EnumName nvarchar(255) NOT NULL,
Active bit,
TextEn nvarchar(255) NOT NULL,
TextDe nvarchar(255),
PRIMARY KEY (Id),
FOREIGN KEY (EnumItemCategoryId) REFERENCES EnumItemCategories(Id));
INSERT INTO EnumItemCategories (Id, CategoryName, TextEn, TextDe) VALUES (1, 'Animals', 'Animals', 'Tiere'), (2, 'Cities', 'Cities', 'Städte')
INSERT INTO EnumItems (Id, EnumItemCategoryId, EnumName, TextEn, TextDe) VALUES (1001, 1, 'Cat', 'Cat', 'Katze'), (1002, 1, 'Dog', 'Dog', 'Hund')
INSERT INTO EnumItems (Id, EnumItemCategoryId, EnumName, TextEn, TextDe) VALUES (2001, 2, 'Bern', 'Bern', 'Bern'), (2002, 2, 'Zurich', 'Zurich', 'Zürich')
SELECT * FROM EnumItems
SELECT * FROM EnumItemCategories
Pro
- Dynamically add new Enums
- Enums come with displaynames
- Update Typos or Translations on the fly
- Database Tables can Reference the EnumItems as ForeignKeys
Con
- Foreignkey constraint is not perfect -> Wrong EnumItem can be referenced (You will want to validate values in your backend)
The Active bool is just an information. You will need 2 methods in your frontend, one that loads all enums and one that filters just the active enums. This is because you want to display already saved values even if the enum is now inactive to be selected. But dropdowns should only display active enums.
Tags: Enum
Archives
- November 2023
- December 2022
- November 2022
- February 2022
- November 2021
- October 2021
- September 2021
- July 2021
- April 2021
- March 2021
- February 2021
- January 2021
- September 2020
- July 2020
- April 2020
- March 2020
- February 2020
- December 2019
- November 2019
- October 2019
- August 2019
- June 2019
- February 2019
- December 2018
- November 2018
- October 2018
- September 2018
- August 2018
- July 2018
- May 2018
- March 2018
- February 2018
- December 2017
- November 2017
- September 2017
- July 2017
- June 2017
- April 2017
- February 2017
- January 2017
- October 2016
- September 2016
- July 2016
- May 2016
- April 2016
- March 2016
- August 2015
- July 2015
- May 2015
- April 2015
- March 2015
- February 2015
- January 2015
- October 2014
- April 2014
- March 2014
- February 2014