A happy demon

How to Encapsulate your Core Domain and while still keeping EF Happy

Alexander Wichmann Carlsen
5 min readSep 12, 2024

--

My stories are free for everyone, so if you aren’t a member you can read it here.

Jimmy Bogaard wrote about something very similar here, where he refactors towards a stateobject. Vaughn Vernon also wrote about it here.
However the blogpost only briefly mentions how (de/re)hydration happens in a repository, but nothing about how it actually works — So this post is about how I like to do it.

StateObject

The state object in this context, is a simple pattern.
It merely serves as model Entity Framework is going to work with.

In the end our goal is to stay out of the way of Entity Framework and make it super simple to map state objects in and out of the database — Vaughn Vernon

This generally means 3 things

  1. No one is going to work on these classes outside of Tests and EF.
  2. Due to 1., we can keep it really simple (properties with public getters/setters).
  3. We now have a place to hold non-domain, but data-entry level information (Like CreationDate or similar if we really want to)

In all regards, the state object abstracts the database away from our domain-model, in a way thats…

--

--

Alexander Wichmann Carlsen

I am a developer, Full-Stack enthusiast, Vue aficionado, Azure ninja, Microservice builder.