Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a lazy loader #8

Closed
LeonardoFerreiraa opened this issue Mar 2, 2019 · 1 comment
Closed

Provide a lazy loader #8

LeonardoFerreiraa opened this issue Mar 2, 2019 · 1 comment

Comments

@LeonardoFerreiraa
Copy link
Owner

When using example and getDefault call another factory, can cause duplicated data.

Example:

@Data
public class Phone {
    private String number;
    private Contact contact;
}

@Data
public class Contact {
    private String name;
}

@Component
public class ContactFactory extends JBacon<Contact> {
    
    @Autowired
    private ContactRepository contactRepository;

    @Override
    public Contact getDefault() {
        Contact contact = new Contact();
        contact.setName("teste");
        return contact;
    }

    @Override
    public Contact getEmpty() {
        return new Contact();
    }

    @Override
    public void persist(Contact contact) {
        contactRepository.save(contact);
    }
}

@Component
public class PhoneFactory extends JBacon<Contact> {
    
    @Autowired
    private PhoneRepository phoneRepository;

    @Autowired
    private ContactFactory contactFactory;

    @Override
    public Phone getDefault() {
        Phone phone = new Phone();
        phone.setNumber("16 99999-9999");
        phone.setContact(contactFactory.create());

        return phone;
    }

    @Override
    public Phone getEmpty() {
        return new Phone();
    }

    @Override
    public void persist(Phone phone) {
        phoneRepository.save(phone);
    }
}

In tests:

Contact contact = contactFactory.create(empty -> {
    empty.setName("Leonardo");
});
phoneFactory.create(5, empty -> {
    empty.setContact(contact);
});

6 Contacts will be persisted in database.

@LeonardoFerreiraa
Copy link
Owner Author

#11

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant