Temporary data / temporary data in DDD on the write side in CQRS

I am having problems with how I will maintain temporary data / temporary data in DDD and how this will be handled on the write side using CQRS. Ultimately, I would like to find a solution that also goes well with the source of events.

Using, for example, temperature forecasts, temperature changes can also affect the energy demand forecast for a region / location. Assuming that temperature forecasts can go far into the future (based on historical data), loading all forecasts into an aggregate Location, I think, will be impractical without applying any limit to the amount of downloaded data.

What is a good / recommended approach for synchronizing / storing this kind of data that will be used on the write side in CQRS while maintaining the event source?

Are any of my attempts below (option A or B) suitable DDD / CQRS solutions?

Option A:

Allow the temperature to be set independently and subscribe to events using the process manager / saga to then recalculate the demand. This solution will help keep the size of the unit small, but it seems that the overall border may be wrong, since demand depends on temperature and now applies to teams / events.

// OverrideTemperatureForecastCommandHandler.cs 
public void Handle(OverrideTemperatureForecast cmd)
{
    var from = cmd.TemperatureOverrides.Min(t => t.DateTime);
    var to = cmd.TemperatureOverrides.Max(t => t.DateTime);

    TemperatureForecasts forecasts = temperatureForecastRepository.GetByLocation(cmd.LocationId, from, to);

    forecasts.Override(cmd.TemperatureOverrides);

    temperatureForecastRepository.Save(forecasts);
    // raises 
    // TemperatureForecastsOverridden(locationId, overrides)
}

// TemperatureForecastsOverriddenProcessManager.cs 
public void Handle(TemperatureForecastsOverridden @event)
{
    var from = cmd.TemperatureOverrides.Min(t => t.DateTime);
    var to = cmd.TemperatureOverrides.Max(t => t.DateTime);

    // issue a command to recalculate the energy demand now temperature has changed...
    commandBus.Send(new RecalculateEnergyDemand 
       { 
          LocationId = @event.LocationId,
          From = from,
          To = to
       }));
}

// RecalculateEnergyDemandCommandHandler.cs 
public void Handle(RecalculateEnergyDemand cmd)
{
    EnergyDemand demandForecasts = energyDemandForecastRepository.GetByLocation(cmd.LocationId, cmd.From, cmd.To);

    // have to fetch temperature forecasts again...
    TemperatureForecasts temperatureForecasts = temperatureForecastRepository.GetByLocation(cmd.LocationId, cmd.From, cmd.To);

    demandForecasts.AdjustForTemperature(temperatureForecasts);

    energyDemandForecastRepository.Save(demandForecasts);
    // raises 
    // ForecastDemandChanged(locationId, demandforecasts)
}

Option B:

"" . DDD-, , , ( ?). "" .

// OverrideTemperatureForecastCommandHandler.cs 
public void Handle(OverrideTemperatureForecast cmd)
{
    var from = cmd.TemperatureOverrides.Min(t => t.DateTime);
    var to = cmd.TemperatureOverrides.Max(t => t.DateTime);

    // use from/to to limit internally the range of temperature and demand forecasts that get loaded in to the aggregate.
    Location location = locationRepository.Get(cmd.LocationId, from, to);

    location.OverrideTemperatureForecasts(cmd.TemperatureOverrides);

    locationRepository.Save(forecasts);
    // raises 
    // TemperatureForecastsOverridden(locationId, overrides)
    // ForecastDemandChanged(locationId, demandforecasts)
}

A B :

// TemperatureDenormaliser.cs
public void Handle(TemperatureForecastsOverridden @event)
{
    var from = @event.Overrides.Min(t => t.DateTime);
    var to = @event.Overrides.Max(t => t.DateTime);

    var temperatureDTOs = storage.GetByLocation(@event.LocationId, from, to);

    // TODO ... (Add or update)

    storage.Save(temperatureDTOs);
}


// EnergyDemandDenormalizer.cs
public void Handle(ForecastDemandChanged @event)
{
    var from = @event.Overrides.Min(t => t.DateTime);
    var to = @event.Overrides.Max(t => t.DateTime);

    var demandDTOs = storage.GetByLocation(@event.LocationId, from, to);

    // TODO ... (Add or update)

    storage.Save(demandDTOs);
}
+4
1

.

, . ; .

, .

0

All Articles