How to create enum file dynamically from the database (or any other Data source)

30 באוקטובר 2012

אין תגובות

Hey.

While we all wait for all the news from 2012 build, I thought about sharing something I did in one of my previews projects.

In this project we had two sources for the same enum, from the database and from a constants in the application. Each time you wanted to add an enum entry, you had to change it in two places, both in the code and the database.

This is, of course an example of bad design and we thought about a solution that will not change the entire application but remove the unnecessary duplication for the enum names and values.

Our solution was to create the a class library dynamically that contains all the enums in the application, so we wouldn’t have to refactor all of the preview code, and this class library will be created upon values based in the database.

And here is the code (of course with a sample enum) :

1.  var appDomain = AppDomain.CurrentDomain;
2.  var assemblyName = new AssemblyName("MyEnumsDllName"); 
3.  var assemblyBuilder = appDomain.DefineDynamicAssembly
    (assemblyName, AssemblyBuilderAccess.RunAndSave);
4.  var moduleBuilder = assemblyBuilder.DefineDynamicModule
    (assemblyName.Name, assemblyName.Name + ".dll");

5.  var enumBuilder = moduleBuilder.DefineEnum
    ("EnumeratedTypes.MyEnum", TypeAttributes.Public, typeof(int));
6.  for (int i = 0; i < 9; i++)
7.  {
8.     enumBuilder.DefineLiteral("EnumEntryName" + (i + 1).ToString(), i);
9.  }
10. enumBuilder.CreateType();

11. assemblyBuilder.Save(assemblyName.Name + ".dll");

Explanation of the code:

2. You give the name of the dynamically created class library.

3. AssemblyBuilderAccess param : you define that the class library you have just created can be saved and executed.

4. Create the class library in memory.

5. Declare a enum in the class library you have just created.

6. Add entries to the just created enum, names and values.

10. Create the enum in memory.

11. Save the file in to the disc.

If you want to create more than 1 enum, just repeat steps 5 until 10, and you can create as much enums as your solution needs. In my sample I didn’t use the database for my source of the enum entry names and values, I used just a memory string for the entry names (“EnumEntryName”) and a increment of int value for the enum values. In our solution we got the names and the values from the database. You can use any data source suitable for your solution.

Now, for any projects that uses those enums, a reference to this newly created class library should be added and this should be done only once. The sweet thing about dynamically created enums is that the IDE (Visual studio 12 in this case) will externalize the changes in the class library as soon as you change it, each time you will add/change/remove entries from the database and run the code I have created above, the content of the class library will change but you will not have to remove/add or change the reference again. The IDE will even show full intellisense for the changed class library for the comport of your developers.

That is it, easy and clean solution for creating enums from any data source.

Enjoy.

הוסף תגובה
facebook linkedin twitter email

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *