Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mapping between interfaces in mapStruct

Tags:

mapstruct

Hi as of now i'm able to do the mapping between simple classes. Now i have to map between interfaces. Consider i have 2 interfaces where many classes implements these interfaces. Since i didn't get to know how to do, i have mapped to specific type of interface A. Now i need to do reverse mapping, where attributes may be in subclasses, how to do inverse mapping in this case. I tried @inheritinverseconfiguratioin tag it didn't work.
instead of reading descriptions, it will be really helpful if i get some code snippet in answer to understand

like image 312
Sharath K P Avatar asked Nov 25 '25 09:11

Sharath K P


1 Answers

Let's say there are two interfaces

public interface DomainInterface { }
public interface DtoInterface { }

and implementations

public class Domain1Impl implements DomainInterface { }
public class Domain2Impl implements DomainInterface { }
public class Dto1Impl implements DtoInterface { }
public class Dto2Impl implements DtoInterface { }

Mapper example

@Mapper
public interface MyInterfaceMapper {

    default DtoInterface map(DomainInterface domain) {
        if (domain instanceof Domain1Impl) {
            return mapDomain1((Domain1Impl)state);
        }
        else if (domain instanceof Domain2Impl) {
            return mapDomain2((Domain2Impl)state);
        }

    }

    Dto1Impl mapDomain1(Domain1Impl domain);

    Dto2Impl mapDomain2(Domain2Impl domain);

}
like image 137
Roman Golov Avatar answered Nov 28 '25 17:11

Roman Golov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!