Technology debt! How to realize multiple enum free conversion simply and efficiently | Develop Paper

Technology debt! How to realize multiple enum free conversion simply and efficiently | Develop Paper

1: Background

1. Tell a story

Some time ago, I was in charge of two business modules of a project with my colleagues. Maybe we lacked communication, which led to a place where we should have defined an enum. As a result, we each customized one, which made us tired of docking these two enums. In order to facilitate understanding, I don’t want to let you see the English spelling of this sprain. Let me take the bank as an example.

This is embarrassing. How can we turn China bankenum into bankenum? In order to seek more, faster, better and less, this article talks about this problem.

2: Looking for solutions

1. Manual matching

In essence, it is to find a mapping relationship between two enums. Human flesh matching is the most simple and crude. The code is as follows:

I can see that this way of writing lacks flexibility. As a programmer, I can’t be satisfied with it. Since I’m looking for mapping, I believe many friends first heard that the word mapping comes from the Entity Framework. People use attribute when dealing with mapping from table to model. Is that right? That’s the inspiration. Can I also use attribute To mark the corresponding relationship between two enums???

2. Use attribute

With this idea, you can customize an attribute. Of course, if you are lazy, you can also use the one that comes with the frameworkDescriptionAttributeThe code is as follows:

Next, you can set the description on bankenum, as shown in the following code:

Then I can get the attribute value through reflection, and then go to chinabankenum to find the corresponding key. Right? For the convenience of understanding, I encapsulate an extension method of enum, and realize the conversion of enum to enum through reflection. The code is as follows:

The code logic is relatively simple. Next, write two examples to test:

3. Some optimization of parse transformation

I don’t know if you find that when you write code, when you convert string or int to enum, the code you write is stinky and long, such as the following:

There are both typeof and strong type conversion, and if the strong conversion fails, exceptions will be thrown. For various reasons, a new tryparse is added to the framework, as shown in the following figure

It looks much better, but I still feel a little uncomfortable. In order to be more pleasant, I decided to encapsulate a tryparse method in enumextension, as shown in the following code:

When calling, you can do this:Var bankenum = "industrial and Commercial Bank of China". Tryparse < chinabankenum > ();It’s more pleasant to the eye.

3: Summary

Ha, this article comes from a pit encountered in the project development. I believe many friends will encounter similar situations. Unfortunately, the function provided by the default enum is too weak. You can expand more practical methods on enum according to your own business, such as obtaining all keys, all values and so on, so as to make your code more neat, clean and powerful!

More high quality dry goods: see my GitHub:dotnetfly

This content was originally published here.

Leave a Reply

%d bloggers like this: