Skip to content

Commit a9ba1de

Browse files
vanroguclaude
andcommitted
Make build() return the context type from newBuilder()
The builder now remembers the context type passed to newBuilder() and build() returns C directly, eliminating the redundant build(Class) overload. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 8721acc commit a9ba1de

File tree

9 files changed

+24
-18
lines changed

9 files changed

+24
-18
lines changed

sliceworkz-eventmodeling-api/src/main/java/org/sliceworkz/eventmodeling/boundedcontext/BoundedContext.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static <C extends BoundedContext<?,?,?>> BoundedContextBuilder<C> newBuil
5454
". Ensure it extends EventTypes with concrete type arguments.");
5555
}
5656
BoundedContextBuilder<C> result = ServiceLoader.load(BoundedContextBuilder.class).findFirst().get();
57+
result.contextType(contextType);
5758
result.eventTypes((Class<?>) typeArgs[0], (Class<?>) typeArgs[1], (Class<?>) typeArgs[2]);
5859
return result;
5960
}

sliceworkz-eventmodeling-api/src/main/java/org/sliceworkz/eventmodeling/boundedcontext/BoundedContextBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ public interface BoundedContextBuilder<C extends BoundedContext<?,?,?>> {
3535

3636
BoundedContextBuilder<C> name(String name);
3737

38+
BoundedContextBuilder<C> contextType(Class<C> contextType);
39+
3840
BoundedContextBuilder<C> eventTypes(
3941
Class<?> domainEventRootType,
4042
Class<?> inboundEventRootType,
@@ -107,8 +109,6 @@ BoundedContextBuilder<C> historicalEventTypes(
107109
*/
108110
<T> T port(Class<T> portType, String qualification);
109111

110-
<T> T build( );
111-
112-
<T> T build(Class<T> returnType);
112+
C build();
113113

114114
}

sliceworkz-eventmodeling-benchmark/src/main/java/org/sliceworkz/eventmodeling/benchmark/BenchmarkApplication.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public static void main ( String[] args ) throws InterruptedException {
107107
.features()
108108
.rootPackage(BenchmarkApplication.class.getPackage())
109109
.done()
110-
.build(OrderProcessing.class);
110+
.build();
111111

112112
bc.start();
113113

sliceworkz-eventmodeling-examples/src/main/java/org/sliceworkz/eventmodeling/examples/banking/BankingClosingTheBooksExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public static void main(String[] args) {
100100
.rootPackage(BankingClosingTheBooksExample.class.getPackage())
101101
.done();
102102

103-
ClosingTheBooks bc = builder.build(ClosingTheBooks.class);
103+
ClosingTheBooks bc = builder.build();
104104

105105
bc.start();
106106

sliceworkz-eventmodeling-examples/src/main/java/org/sliceworkz/eventmodeling/examples/banking/BankingExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public static void main ( String[] args ) {
6161
.features()
6262
.rootPackage(BankingExample.class.getPackage())
6363
.done()
64-
.build(Banking.class);
64+
.build();
6565

6666
bc.start();
6767

sliceworkz-eventmodeling-impl/src/main/java/org/sliceworkz/eventmodeling/module/boundedcontext/BoundedContextBuilderImpl.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public class BoundedContextBuilderImpl<C extends BoundedContext<?,?,?>> implemen
8282
private static final String PURPOSE_OUTBOUND = "outbound";
8383
private static final String PURPOSE_OBSERVABILITY = "observability";
8484

85+
private Class<C> contextType;
8586
private String name;
8687

8788
private List<LiveModelSpecificationImpl> liveModelSpecs = new ArrayList<>();
@@ -109,6 +110,12 @@ public class BoundedContextBuilderImpl<C extends BoundedContext<?,?,?>> implemen
109110

110111
private final AdapterRegistry adapterRegistry = new AdapterRegistry();
111112

113+
@Override
114+
public BoundedContextBuilder<C> contextType(Class<C> contextType) {
115+
this.contextType = contextType;
116+
return this;
117+
}
118+
112119
@Override
113120
public BoundedContextBuilder<C> name ( String name ) {
114121
this.name = name;
@@ -260,12 +267,8 @@ public <T> T port(Class<T> portType, String qualification) {
260267
}
261268

262269
@Override
263-
public <T> T build ( ) {
264-
return build((Class<T>) BoundedContext.class);
265-
}
266-
267-
@Override
268-
public <T> T build ( Class<T> returnType ) {
270+
public C build ( ) {
271+
Class<?> returnType = contextType != null ? contextType : BoundedContext.class;
269272

270273
if ( instance == null ) {
271274
throw new IllegalArgumentException("instance not set");
@@ -360,11 +363,11 @@ public <T> T build ( Class<T> returnType ) {
360363
am.setCapabilitiesDelegate(bc);
361364
im.setCapabilitiesDelegate(bc);
362365

363-
T result;
366+
C result;
364367
if ( returnType.isInterface()) {
365368
result = proxy(bc, returnType);
366369
} else {
367-
result = (T)bc;
370+
result = (C)bc;
368371
}
369372
bc.setSelfReference((BoundedContext<?,?,?>) result);
370373
return result;

sliceworkz-eventmodeling-testing/src/main/java/org/sliceworkz/eventmodeling/testing/AbstractBoundedContextTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ void setUp ( ) {
7373
// let subclasses do any needed configuration
7474
configure(builder);
7575

76-
this.boundedContext = builder.build(BoundedContext.class);
76+
this.boundedContext = (BoundedContext<DOMAIN_EVENT_TYPE, INBOUND_EVENT_TYPE, OUTBOUND_EVENT_TYPE>) builder.build();
7777
}
7878

7979
public abstract Class<DOMAIN_EVENT_TYPE> domainEventType ( );

sliceworkz-eventmodeling-tests-inmem/src/test/java/org/sliceworkz/eventmodeling/mock/boundedcontext/AbstractMockDomainTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@
2121

2222
public abstract class AbstractMockDomainTest extends AbstractBoundedContextTest<MockDomainEvent,MockInboundEvent,MockOutboundEvent> {
2323

24+
@SuppressWarnings("unchecked")
2425
protected Mock buildBoundedContext ( BoundedContextBuilder<?> boundedContextBuilder ) {
25-
Mock result = boundedContextBuilder.build(Mock.class);
26+
Mock result = (Mock) boundedContextBuilder.build();
2627
this.boundedContext = result;
2728
this.boundedContext.start();
2829
return result;

sliceworkz-eventmodeling-tests-inmem/src/test/java/org/sliceworkz/eventmodeling/module/historical/HistoricalDomainEventTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
* annotations and {@link Upcast} implementations to transparently transform old events
5656
* to current types when read from the event store.</p>
5757
*/
58+
@SuppressWarnings("unchecked")
5859
public class HistoricalDomainEventTest {
5960

6061
private EventStorage eventStorage;
@@ -104,7 +105,7 @@ void historicalEventsAreUpcastedAndVisibleThroughReadModel() {
104105

105106
builder.readmodel(ItemCountReadModel.class).live();
106107

107-
boundedContext = builder.build();
108+
boundedContext = (BoundedContext<CurrentDomainEvent, InboundEvent, OutboundEvent>) (BoundedContext<?,?,?>) builder.build();
108109
boundedContext.start();
109110

110111
// Step 3: Execute a command that adds a NEW event (using the current schema)
@@ -139,7 +140,7 @@ void historicalEventsAreUpcastedAndVisibleThroughCommand() {
139140

140141
builder.readmodel(ItemCountReadModel.class).live();
141142

142-
boundedContext = builder.build();
143+
boundedContext = (BoundedContext<CurrentDomainEvent, InboundEvent, OutboundEvent>) (BoundedContext<?,?,?>) builder.build();
143144
boundedContext.start();
144145

145146
// Execute a command that raises a new event

0 commit comments

Comments
 (0)