-
Notifications
You must be signed in to change notification settings - Fork 57
Seed from Production Domain Driven Design
If you are using DDD-styled entity classes (e.g. with private setters and possibly private fields) then you can still use the Seed from Production, but you do need to be more careful on the serializing/deserializing of the JSON files.
The obvious issue is how can Newtonsoft.Json, which is what I use, has to be able to set the properties/fields in the deserialize phase. What is not so obvious (and took me two days to work out!) is Newtonsoft.Json MUST have a way to set all the properties even when it is serializing the classes.
There are many ways to do this (allowing writes to private setters, marking properties with [JsonProperty] or marking a constructor with [JsonConstructor]). This library has a number of extension methods in the class SeedJsonHelpers which use the "allowing writes to private setters" method. These work for my test data, but you may need to try other ways if this doesn't work for your case.
It turns out if you haven't given Newtonsoft.Json a valid way to set a property, then when you serialize the data will either give you an error message which doesn't make any sense (I got a "circular reference exception"), or it serializes the data incorrectly (I got a duplicate entry). Just be aware of this
For a more detailed description of the bug I found in the Newtonsoft.Json go to issue 2049 on Newtonsoft.Json GitHub repo.
- Testing against a PostgreSQL db
- Changes in EfCore.TestSupport 5
- Testing with production data
- Using an in-memory database (old)