简体   繁体   中英

Mapping of enum between layers

I have a service and an api to this service. The service has a enum with let's say 20 values. But my API only has 10. What I do is that I map my 20 service-enums to my 10 api-enums. But let's say that someone alters the enum of the service? Someone adds 10 values so we now have 30 enums but forgets/doesn't know how to alter the mapping. Then we have 10 unmapped enums that never reaches the client. Is it possible to design a mapping between two enums so if any of them changes then we for example get a compile-time error that says that there's enum values that are not mapped? Or am I thinking very wrong here? Maybe a unit-test is the way to make sure that all values in an enum is mapped to another?

Edit:

The service enum would have enums of a more technical character while the api-enums would only have values that should be more logical?

Service-enums:

  • JPA_ERROR("description")
  • DATABASE_DOWN
  • SOMETHING_INVALID
  • SOMETHING_EXPIRED
  • SERVICE_UNAVAILABLE

API-enums:

  • SOMETHING_INVALID
  • SOMETHING_EXPIRED
  • SERVICE_UNAVAILABLE

My mapping today is just a function that takes a service-enum as an argument and has a basic switch-statement on it. In the default-statement I return null and this is what troubles me. I don't like returning null but I also don't really know what kind of behaviour the default-statement should have. That's why I started thinking about if I could "force" the developer to always make sure the mapping is correct.

function ApiEnum mapServiceEnumToApiEnum(ServiceEnum serviceEnum){
    switch(serviceEnum){
        case JPA_ERROR:
             return ApiEnum.SOMETHING_INVALID;
        case DATABASE_DOWN:
             return ApiEnum.SOMETHING_INVALID;
        default:
            return null; //Don't want this to happen...
    }    

It's an abstract question -- I don't know how you have the service enums mapped to your enums, so it's hard to say what makes the most sense. But the approaches you mention are reasonable.

Unmapped Enum Exception

You could certainly check if an enum is unmapped and then throw an exception. If you want the request to fail in the case of an unmapped enum, that's probably the right way to go.

Logged Unmapped Enum

If you don't want the request to fail, you just want to notice when there are unmapped enums, you could log it. Of course, this assumes that you actually look at the logs with enough frequency that you would detect this log message. If not, logging is often just more noise.

Test Mapping

Without knowing more about the architecture, it's hard to say if any test that could check the service enum values against your enum values is really a unit test. If you're dealing with a remote service, it'd be more of an integration test, and the kind of integration test that could fail for other reasons -- say if the test machine loses its connection to the remote service. Still, this might be an approach you'd notice more readily than a log message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM