Removed the requirement for having the default constructor in the aggregation process and projections #1447
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Overview
Currently, Marten forces in many places
new ()
constructor. This is not a huge issue, but a bit painful and sometimes too big restriction. For most of the guides of how to construct proper aggregates (and classes in general), it stated that they should only expose what's needed. Usually, it's good to not expose the public default constructor but use the dedicated factory method.Especially for the event-sourced aggregates, it's important, as the state should be rebuilt from the events.
Details of changed
Introduced new object factory called (surprise!) New. This solution is taken from https://stackoverflow.com/a/16162475/10966454.
See also more benchmarks and discussion about object creation from lambda (with expression caching):
The solution tries to find the default public constructor, if it's not able then it's searching for non-public and this is also not found then it creates an uninitialized object (see https://docs.microsoft.com/en-us/dotnet/api/system.runtime.serialization.formatterservices.getuninitializedobject?view=netframework-4.8).
Besides that: