jeudi 28 septembre 2017

Which pattern apply for versioning dto

I'm working witch different XML messages on my Grails application. The XML messages have this structure:

<tag>
    <head>
        <param></param>
        <param></param>
    </head>
    <a1xx>
        <param></param>
        <param></param>
        <param></param>
    <a1xx>
</tag>

where xx are diferent type of messages such as 01,02,03,04,etc...

Now I have to work with two different versions of this messages, so I will have the same structure but with different params, and I build this:

class XmlMessageFactory {
   A1XXMessageDto createMessage (MessageType type, MessageVersion version, Params params) {
       A1XXMessageDto a1xxMessage
       if(version==1){
           switch(type) {
           case type.1:
               a1xxMessage = new A102MessageDtoV1(params)
           case type.X:
               a1xxMessage = new A10XMessageDtoV1(params)        
           ....
           }
       } else {
           switch(type) {
           case type.1:
               a1xxMessage = new A102MessageDtoV2(params)
           case type.X:
               a1xxMessage = new A10XMessageDtoV2(params)        
           ....
           }
   }
}

A1XXMessageDto

class A1XXMessageDto {

    HeaderDto header
    BodyDto body

    public A1XXMessageDto(Params params) {
    }
}

A1XXMessageDtoV1/V2 (build the header for the version since the header is common on all the messages with same version)

class A1XXMessageDtoV2 extends A1XXMessageDto {   
    public A1XXMessageDtoV2(Params params) {
        super(params)
        header = new HeaderDtoV1/V2(paramas)
    }
}

HeaderDto

class HeaderDto {

    String commonParam1
    String commonParam2
    public HeaderDto(Params params) {
        commonParam1 = param.commonParam1
        commonParam2 = param.commonParam2
    }

}

HeaderDtoV1/V2 (specific header for each version with specific params)

class HeaderDtoV17V2 extends HeaderDto {
    String commonParamV2
    public HeaderDtoV2(Params params) {
        super(switchingProcess, message)
        commonParamV2 = param.commonParamV2  
    }

}

BodyDtoV1/V2 (specific body for each version with specific params)

class BodyDtoV1/V2 extends BodyDto {
    String extrainfo
    BodyDtoV2(Params params) {
        super(params)
        extrainfo = ""
    }
}

A101BodyDtoV1/V2 (specific data for each message and version)

class A101BodyDtoV2 extends BodyDtoV1/V2 {
    String specificParam1
    String specificParam2

    public A102BodyDtoV2(Params params) {
          commonParam1 = params.specificParam1
          commonParam2 = params.specificParam2
    }

}

A101MessageDtoV1/V2 (this class build the body for each message and version)

class A102MessageDtoV2 extends A1XXMessageDtoV2 {

    public A102MessageDtoV2(Params params) {
        super(params)
        body = new A102BodyDtoV2(params)
    }
}

And I have more classes like this last, for all other messages types.

But I think should be a best way to implement this, and get the posibility to extend it in the future.

So, Which pattern fit best with this structure?

Thanks!

Aucun commentaire:

Enregistrer un commentaire