Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive programming in Domain Design

I'm used to develop software creating "domain entities" -- these entities "depends" only on other entities inside the domain.

Let's say I have this interface

package domain;

import domain.beans.City;

public interface CitiesRepository {  
  City get(String cityName);          
}

As you can see, the City I am returning is again a domain object. Implementations of this CitiesRepository can be found outside the domain, and can rely upon a database, an http client, a cached decorator etc.

I am now working with a reactive framework -- vert.x -- and I am trying to understand how I can keep working using such model. I don't want vert.x specific answer but only to understand if there is any pattern/best practice to understand how to achieve this.

In the reactive-programming there is almost never a return value but always some callback/handler that will consume the event after it happened. Should I rewrite my interfaces to be "reactive"?

package domain;

import domain.beans.City;

public interface CitiesRepository {  
  void get(String cityName, DomainHandler<City> cityHandler);
}

Just providing this example caused me some some spaghetti-headache when thinking to the implementations where I have to deal with the Handler of the "reactive framework" to "fill" my domain handler.

Should I stop thinking in this kind of design when working with reactive model? Should I prefer an Observable/Promise approach?

Any hint would be really appreciated

like image 562
Carlo Bertuccini Avatar asked Jun 15 '26 11:06

Carlo Bertuccini


1 Answers

In the reactive systems I've been involved with there has been an event handler that would then use the repository:

public void SomeEventHandler : IHandle<SomeEvent> {
public SomeEventHandler(CityRepository repo) {}
}

You would then use your existing repository inside the handler code:

public void When(SomeEvent event) {
    var city = _cityRepository.Get(event.CityName);
// do something with city
}

In the CompositionRoot of the application, the handler would be registered to handle the event through whatever messaging bus / reactive stream etc. will be receiving / producing the event.

So I wouldn't look to make the repository reactive, rather add in an event handler to use it.

like image 136
tomliversidge Avatar answered Jun 18 '26 01:06

tomliversidge



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!