Skip to content

Feature: Map constructor parameters #35

Closed

Description

@JeremyCaney

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 IList and 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 the TopicMappingCacheEntry. 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

  1. added
    Area: MappingRelates to one of the `ITopicMappingService` interfaces or implementations.
    Status 0: DiscussionNeeds further evaluation of requirements and prioritization.
    Type: FeatureIntroduces a major area of functionality.
    on Mar 5, 2021
  2. self-assigned this
    on Mar 20, 2021
  3. added this to the OnTopic 5.1.0 milestone on Mar 20, 2021
  4. added and removed
    Status 0: DiscussionNeeds further evaluation of requirements and prioritization.
    on Mar 20, 2021
  5. JeremyCaney commented on Mar 21, 2021

    @JeremyCaney
    MemberAuthor

    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.Id and Type that are being initialized, and then throw an exception if that Topic.Id and Type are requested again before the first finishes initialization.

    This could even be done with the existing MappedTopicCache by adding e.g an IsInitializing property to the MappedTopicCacheEntry, and then throwing an exception TryGetValue()—and possibly AddOrGet()—attempt to retrieve a MappedTopicCacheEntry with IsInitializing set to true. This way, we don’t need to introduce a new way of tracking current mapping state.

    Note: This would require careful coordination from callers to avoid inadvertently tripping this exception, but as an internal class exclusively called by TopicMappingService, that should be easy to manage.

  6. JeremyCaney commented on Mar 21, 2021

    @JeremyCaney
    MemberAuthor

    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<> or IEnumerable<>, we can extract the generic parameters and use them to initialize a List<>. The latter is a bit more involved.

  7. removed this from the OnTopic 5.1.0 milestone on Mar 21, 2021
  8. added
    Status 0: DiscussionNeeds further evaluation of requirements and prioritization.
    and removed
    Status 0: DiscussionNeeds further evaluation of requirements and prioritization.
    on Mar 21, 2021
  9. 4 remaining items

  10. JeremyCaney commented on Mar 23, 2021

    @JeremyCaney
    MemberAuthor

    To support this, the TopicMappingService was refactored to use a new ItemConfiguration (88e06a7) instead of a PropertyConfiguration (88e06a7) so that the helper methods could operate off of either ParameterInfo or PropertyInfo. Those same methods were updated to use "get" semantics instead of "set" semantics (304525d), thus separating out how they are set to either SetPropertyAsync() (304525d) or GetParameterAsync() (142d765). With that, the constructor mapping logic was added to the previously introduced MapAsync(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 the TopicMappingService (ad34a65). This logic is validated via a pair of unit tests (8b3f14e, 4ac3567) using a new ConstructedTopicViewModel (3dcbdd6).

    As part of this update, I also refactored the MemberDispatcher to separate the (de)serialization of attribute values to value types into a new AttributeValueConverter library (84a2758), and updated the MappedTopicCache to have a more focused interface (60ee7d7, 5b3a59c) and to enforce business logic related to instantiating constructors (8f86392, cb41728, b33b60c).

  11. added
    Status 5: CompleteTask is considered complete, and ready for deployment.
    and removed on Mar 23, 2021
  12. changed the title [-]Map constructor parameters[/-] [+]Feature: Map constructor parameters[/+] on Mar 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Area: MappingRelates to one of the `ITopicMappingService` interfaces or implementations.Priority: 2Status 5: CompleteTask is considered complete, and ready for deployment.Type: FeatureIntroduces a major area of functionality.

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions