groovy - Builder pattern with reactive programming -
i'm attempting use reactive programming (rxgroovy) create objects using builder pattern, property values come database queries. i'm looking first of all, how it, , second of all, thoughts on whether or not idea.
the objects i'm trying create of type:
class foo { final string name; final long[] relatedids; foo(string name, long[] relatedids) { this.name = name; this.relatedids = relatedids; } }
and builder:
class foobuilder { string name; linkedlist<long> relatedids; { relatedids = new linkedlist<>(); } foobuilder name(string name) { this.name = name; return this; } foobuilder relatedid(long id) { this.relatedids.add(id); return this; } foo build() { return new foo(this.name, this.relatedids.toarray()); } }
i have 2 queries, 1 returns names of foo objects, , separate query run once each foo object related ids.
i have method (call queryasobservable
) query , observable emits each row of result set. setting name
this:
observable<foobuilder> buidlerswithnameset = queryasobservable(queryallfoos) .map { return new foobuilder().name(it.name); }
and given foobuilder
, set relatedids this:
final foobuilder builder; queryasobservable(queryrelatedids, builder.name) .subscribe { builder.relatedid(it.id); }
my problem how tie 2 together? how create observable set related ids, shown, each foobuilder
emitted first observable? in end, need of foobuilder
objects, name
, relatedids
properties set.
note joins
i know can related ids , name in 1 query join. thought can use lots of smaller queries running in parallel , uses observable streams work each result result becomes available, instead of waiting results come in , building objects serially. i'm open ideas on why that's or bad idea.
to combine observables use flatmap(). not sure of groovy syntax i'll take shot. starting builderwithnameset
:
observable<foobuilder> completebuilder = builderswithnameset .flatmap { builder -> return queryasobservable(queryrelatedids, builder.name) .map { builder.relatedid(it.id); } }
Comments
Post a Comment