So stellen Sie sicherJava

Java-Forum
Anonymous
 So stellen Sie sicher

Post by Anonymous »

Ich möchte meine Zuordnungsprozesse standardisieren, um einem bestimmten Schema zu folgen. Daher habe ich einige generische Schnittstellen eingeführt, die dazu beitragen, den Aufwand beim Schreiben neuer Mapper für neue Typen zu verringern. Hier ist die Schnittstelle für Typen: < /p>

Code: Select all

import lombok.NonNull;

public interface MappableCyclic
{
void beforeMapping(@NonNull IN in, @NonNull ReferenceCycleTracking context);
void  afterMapping(@NonNull IN in, @NonNull ReferenceCycleTracking context);
}
< /code>
ReferenceCycleTracking
ist eine typische Implementierung für einen Mapstruct -Kontext, um die unendliche Rekursion beim Abbau von Objekten mit zyklischen Abhängigkeiten zu vermeiden. Darüber hinaus gibt es eine generische Super -Schnittstelle für Mapper: < /p>

Code: Select all

public interface MappableCyclicMapper
{
@NonNull OUT map(@NonNull IN in, @NonNull @Context ReferenceCycleTracking context);

@BeforeMapping default void beforeMapping(
@NonNull                IN                     in,
@NonNull @MappingTarget OUT                    out,
@NonNull @Context       ReferenceCycleTracking context)
{
out.beforeMapping(out, in, context);
}

@AfterMapping default void afterMapping(
@NonNull                IN               in,
@NonNull @MappingTarget OUT              out,
@NonNull @Context ReferenceCycleTracking context)
{
out.afterMapping(out, in, context);
}

@NonNull Class outType();
@NonNull OUT        create(IN in);

/**
* object factory should be called by mapstruct during generated {@link #map(MappableCyclic, ReferenceCycleTracking)}
* implementation
*/
@ObjectFactory default @NonNull OUT lookupOrCreate(@NonNull IN in, @NonNull ReferenceCycleTracking context)
{
OUT out = context.get(in, outType());
if (out == null)
{
out = create(in);
context.put(in, out);
}
return out;
}
}
< /code>
And finally here is the mapper interface for two of my mappable types:
@Mapper interface Map_TaskGroup_EntityDTO_EntityJPA extends MappableCyclicMapper
{
Map_TaskGroup_EntityDTO_EntityJPA INSTANCE = Mappers.getMapper(Map_TaskGroup_EntityDTO_EntityJPA.class);

@NonNull TaskGroupEntityJPA map(@NonNull TaskGroupEntityDTO input, @NonNull @Context ReferenceCycleTracking context);

@Override default @NonNull Class outType() { return TaskGroupEntityJPA.class; }

@Override default @NonNull TaskGroupEntityJPA create(TaskGroupEntityDTO in) { return new TaskGroupEntityJPA(in.name()); }

@ObjectFactory
@Override default @NonNull TaskGroupEntityJPA lookupOrCreate(
@NonNull TaskGroupEntityDTO taskGroupEntityDTO, @NonNull ReferenceCycleTracking context)
{
return MappableCyclicMapper.super.lookupOrCreate(taskGroupEntityDTO, context);
}
}
< /code>
I expected that mapstruct would use the @ObjectFactory
Methode in der Super -Schnittstelle bereitgestellt, aber ich habe nicht versucht, das Mapstruktur mit einer zusätzlichen Objektfabrikmethode in der obigen Sub -Schnittstelle zu unterstützen. Die Implementierung für diese Schnittstelle, die durch Mapstruct erzeugt wird, ignoriert jedoch meine Objektfabrikmethoden: < /p>
@Override
public TaskGroupEntityJPA map(TaskGroupEntityDTO input, ReferenceCycleTracking context) {
TaskGroupEntityJPA target = context.get( input, TaskGroupEntityJPA.class );
if ( target != null ) {
return target;
}

if ( input == null ) {
return null;
}

String name = null;

TaskGroupEntityJPA taskGroupEntityJPA = new TaskGroupEntityJPA( name );

context.put( input, taskGroupEntityJPA );
beforeMapping( input, taskGroupEntityJPA, context );

afterMapping( input, taskGroupEntityJPA, context );

return taskGroupEntityJPA;
}
< /code>
How can I make sure that my object factory methods are not ignored any longer?

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post