rathoud96

rathoud96

Commanded aggregate state is different than projection

I am working with commanded by @slashdotdash. Recently I encountered an issue where my aggregate’s state was different than my projection.
For example the aggregate state is %Aggregate.Example{is_valid: false} and my projection is %Projection.Example{is_valid: true}
Now I am stuck on how to sync them or is there a way the state can be updated?

Thanks

Most Liked

tcoopman

tcoopman

I’m lacking a bit of context to give a good response.

Do you mean your projection in production has a bug so that it’s state is wrong? Did you forget to handle an event? Or something else?

If it’s a bug in your projection, the default way to solve this would be to fix the bug, deploy it and then replay the eventstream.

Last Post!

tcoopman

tcoopman

On my phone, so I’ll do my best to respond.

I’m going to take a step back from commanded and talk about event sourcing in general for a moment.

In event sourcing an aggregate takes a list of events (it’s own history) and a command and it makes a decision. That decision is communicated by returning one or more events.
The aggregate should be pure. Meaning it should make the decision only based on the information in the history of the events and the command. It should do no queries or other side effects to make a decision.

I write should. I mean I strongly suggest doing it that way. It will give you easier to understand code. Easier to test code,…

Looking at your code it looks like you’re doing a query in the aggregate. From a design perspective I would try to extract that.
It’s not clear though where you’re doing the query in the aggregate. Is it when making the decision? So where you handle the command? Or is it in the apply function?

If it’s in the apply function it is definitely wrong. Your apply function MUST be pure.

If it’s in the code making a decision. The only thing I can imply is that at the time of the command the author was not in the DB. I would expect a different event though depending on if the author is in or not, so the projection should follow and have the same information.

Hopefully this makes sense. If not let me know.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement