2014년 12월 27일 토요일

Async java driver fluent syntax changes

The older version with artefact 'async-driver' was using a syntax like (using groovy code here) :

collection
.find(...)
        
.updateOne(....)
        
.register({ AcknowledgedWriteResult result, MongoException e2 ->
} as SingleResultCallback)




which was great, as it allowed to structure complex queries by using the fluent builder pattern. However, the async-driver artefact no longer seems to be maintained...

As of artefact 'driver-async' this seems to have changes to:


collection.updateOne(...,
.....,
{ AcknowledgedWriteResult result, MongoException e2 ->
} as SingleResultCallback
)

Which in my opinion makes it a lot more difficult to write clean looking code. Any chance to have a real fluent api back?



Have you had a look at this async driver?

It takes a different approach from having the fluent API tied to the collection object. Instead the fluent builder is attached to the immutable operation. e,g.,

   collection.find( Find.builder().where(...).sort(...).limit(...) );

I think this has a couple advantages:
  1. You can build a single instance of an operation you plan to reuse. This is not helpful in all cases but I have seen many cases where it can help keep the intent of code much clearer and creates less garbage for the JVM.
  2. There are no surprises as to when the driver is going to the server for an operation.  If you call a non-getter method on the collection, you are going to the server. I have seen lots of people iterating over a cursor and calling .count() in the middle of the loop thinking it is giving a count from the iteration and wondering why it is so slow.


I actually had a look at that driver in the past, but found that the licence did not fit the project I am working on. Looks like the licence has changed recently. Not sure if I want to switch to an entirely different driver right now.


댓글 없음:

댓글 쓰기