Repository navigation
Description
Currently, the TopicMappingService exclusively maps properties, and requires an empty constructor. It would add flexibility, and especially for records, if constructor parameters could also be mapped.
Challenges
There are two primary challenges with this.
Double Mapping
If a property is mapped via the constructor, how do we prevent it from being double mapped? This is especially true with records, where the constructor parameter might be used to define a property.
Order of Operations
Currently, the object is initialized, immediately added to cache, and then mapped. This would require assembling a collection of mapped values first and assigning them in unison, instead of assigning them as they're evaluated, and only then adding the object to the cache.
Circular References
Extrapolated out, the above means that topic associations might be evaluated prior to the root object(s) being created. This will introduce problems with circular references since the original topic can't be mapped until (constructor) dependencies are mapped.
Considerations
Double Mapping
- Ignore mapping properties if they aren’t an
IListand aren’t set to their default value—except that would likely introduce problems with properties initialized to a specific value. - Require
[DisableMapping]on the property itself to manually prevent this situation. - Track mapped constructor parameters in a configuration object passed down to
MapProperties(). This could even be on theTopicMappingCacheEntry. That would depend on the parameter names matching their target properties.
Circular References
The circular dependency issue could be addressed by limiting what annotations can be applied to parameters. Specifically, this could exclude:
[Include()], so no associations are mapped,[MapToParent], since no parent object will be available.
Activity
JeremyCaney commentedon Mar 21, 2021
Topic associations—including topic references and relationship collections—could both be supported so long as they don’t create a circular reference. One way to handle this would be to track the
Topic.IdandTypethat are being initialized, and then throw an exception if thatTopic.IdandTypeare requested again before the first finishes initialization.This could even be done with the existing
MappedTopicCacheby adding e.g anIsInitializingproperty to theMappedTopicCacheEntry, and then throwing an exceptionTryGetValue()—and possiblyAddOrGet()—attempt to retrieve aMappedTopicCacheEntrywithIsInitializingset totrue. This way, we don’t need to introduce a new way of tracking current mapping state.JeremyCaney commentedon Mar 21, 2021
Relationships introduce some challenges in terms of initializing the collection. If it’s a concrete type with an empty constructor, that’s no problem. If it’s an
IList<>orIEnumerable<>, we can extract the generic parameters and use them to initialize aList<>. The latter is a bit more involved.4 remaining items
Established `ItemConfiguration` based on `PropertyConfiguration`
Establish `IsInitializing` property on `MappedTopicCacheEntry`
Refactored `SetProperty()` around "get" semantics
Introduced new `InitializeCollection()` method
Allow `ITopicMappingService` attributes to be used on parameters
Allow non-empty constructors with `MapAsync<T>()`
Update `TopicMultiMap` to be case insensitive
Introduced test for verifying exception on circular references
Merge branch 'feature/map-constructor-parameters' into develop
JeremyCaney commentedon Mar 23, 2021
To support this, the
TopicMappingServicewas refactored to use a newItemConfiguration(88e06a7) instead of aPropertyConfiguration(88e06a7) so that the helper methods could operate off of eitherParameterInfoorPropertyInfo. Those same methods were updated to use "get" semantics instead of "set" semantics (304525d), thus separating out how they are set to eitherSetPropertyAsync()(304525d) orGetParameterAsync()(142d765). With that, the constructor mapping logic was added to the previously introducedMapAsync(topic, type…)overload (3b34265), which was first introduced as part of the[MapAs()]support (61de3bf). Finally, the code was updated to make this the single source of view model construction in theTopicMappingService(ad34a65). This logic is validated via a pair of unit tests (8b3f14e, 4ac3567) using a newConstructedTopicViewModel(3dcbdd6).As part of this update, I also refactored the
MemberDispatcherto separate the (de)serialization of attribute values to value types into a newAttributeValueConverterlibrary (84a2758), and updated theMappedTopicCacheto have a more focused interface (60ee7d7, 5b3a59c) and to enforce business logic related to instantiating constructors (8f86392, cb41728, b33b60c).[-]Map constructor parameters[/-][+]Feature: Map constructor parameters[/+]Merge branch 'release/5.1.0' into master