Introduction
I'm using Dagger 2 for a project that's modeled using the principles of DDD.
I'd like to use the Builder Pattern to create complex entities and the way I'd normally get an instance of the builder was by using a static factory method: ComplexEntity.builder()
.
Aggregate roots will get strongly typed IDs, i.e. ComplexEntityID
, that require a value generator for new instances. Right now I have a method for that in my module: @Provides ComplexEntityID provideComplexEntityID(IdValueGenerator generator) { return new ComplexEntityID(generator.nextId(); }
.
ComplexEntityBuilder
needs an instance (or provider) of ComplexEntityID
in order to create a ComplexEntity
. But static injection is recommended against by the people from Guice and Dagger (among others) for good reasons.
Question
How to create instances of ComplexEntity
using a builder, without using static injection? In other words, how to get an instance of the builder?
Sticking to ComplexEntity.builder()
would be nice because it's a common convention, but I'd think that the only way to make an instance available in a static method without using the new ()
keyword is by using static injection.
Another approach I could think of is to also create ComplexEntityFactory
and put the builder()
method there. But it seems a bit strange to use a factory and a builder together:
- Inject a factory in the class where you need it
complexEntityFactory.builder().value1(...) .value2(...) .build();
What would be the recommended approach in this case?
Edit: If it turns out to a dedicated factory after all, tt would be nice if this factory could still be generated using AutoFactory or something similar
Aucun commentaire:
Enregistrer un commentaire