vendredi 20 juillet 2018

Entity lazy methods and Dimetra law

I have entity:

@Entity
@Table(name = "CARDS")
public class Card {

  @ManyToOne
  @JoinColumn(name = "PERSON_ID", referencedColumnName = "ID", nullable = false)
  private Person person;

  @OneToMany(mappedBy = "card")
  private List<CardStatus> cardStatuses;

And I my service I can call next code:

public Card getCardBlaBlaBla(Long id){

   Card card  = cardRepository.findaCard(id);

   Something something = card.getCardStatuses().get(0).getSomething();
}

card.getCardStatuses().get(0).getSomething() - getCardStatuses is LAZY

My questions - Does this challenge violate the Dimetra law?

The Law of Demeter There is a well-known heuristic called the Law of Demeter2 that says a module should not know about the innards of the objects it manipulates. As we saw in the last section, objects hide their data and expose operations. This means that an object should not expose its internal structure through accessors because to do so is to expose, rather than to hide, its internal structure. More precisely, the Law of Demeter says that a method f of a class C should only call the methods of these:

• C
• An object created by f
• An object passed as an argument to f
• An object held in an instance variable of C

The method should not invoke methods on objects that are returned by any of the allowed functions. In other words, talk to friends, not to strangers. The following code appears to violate the Law of Demeter (among other things) because it calls the getScratchDir() function on the return value of getOptions() and then calls getAbsolutePath() on the return value of getScratchDir(). final String outputDir = ctxt.getOptions().getScratchDir().getAbsolutePath();

Card - is data structures and not violate the Dimetra law but LAZY methos has logik(make select to DB).

Does this challenge violate the Dimetra law or Not? If yes how can I use it correctly?

I realy have a lot of code like:

entityOblect.getChield().getChield().getSomething();

Aucun commentaire:

Enregistrer un commentaire