Home > Software design >  How can I create an EntityView that contains itself?
How can I create an EntityView that contains itself?

Time:12-30

Basically I would like to do this but I just couldn't find the solution or the keywords.

@EntityView(Location.class)
public interface LocationView {
    @IdMapping
    int getId();

    String getName();
    
    LocationView getParent();
}

CodePudding user response:

Referring to the same view again within a view is not allowed because this circularity makes it necessary that entity views become mutable or that some sort of lazy proxy is introduced.

Think about how you want to model your response as e.g. JSON and the entity view mapping will naturally follow from that, e.g. something like this:

@EntityView(Location.class)
public interface LocationIdView {
    @IdMapping
    int getId();
}
@EntityView(Location.class)
public interface LocationView extends LocationIdView {
    String getName();
    LocationIdView getParent();
}

or

@EntityView(Location.class)
public interface LocationIdView {
    @IdMapping
    int getId();
}
@EntityView(Location.class)
public interface LocationBaseView<T extends LocationIdView> extends LocationIdView {
    String getName();
    T getParent();
}
@EntityView(Location.class)
public interface LocationView extends LocationBaseView<CountryLocationView> {
}

@EntityView(Location.class)
public interface CountryLocationView extends LocationBaseView<ContinentLocationView> {
}

@EntityView(Location.class)
public interface ContinentLocationView extends LocationBaseView<LocationIdView> {
}

  • Related