Convert design structure in C #

I need to make conversions between units, each block consists of two parts, its name and integer, the integer part can be positive or negative. I need my Unit class to inject dependencies and be loosely coupled. For example, if I need to add something in the future, I do not want to change other classes that use this class.

In my unit class, there should also be a Convert() method for converting between units. I looked through these links:

But they seem loosely coupled.

Please let me know the recommended design template for this problem.

+4
source share
1 answer

I would create a custom attribute for methods that define a unit that a method can convert to and from. Then your converter function can extract the device from the converted elements and use reflection to find these classes with conversion methods that convert to / from these units.

You need the basic unit of measurement that you use to convert to / from methods. For instance. select the metric, and then all the transforms are converted to / from the metric to / from the second block. Then, to go from block A to block B, you convert from A to metric, then convert to B from metric, using the A and B converters selected using reflection.

Of course, since you donโ€™t provide too many details on your request, itโ€™s hard to say what exactly you are trying to do is โ€œguessโ€ the design. :)

+2
source

Source: https://habr.com/ru/post/1446070/


All Articles