Repository navigation
Dispatch inherited operation methods in generated data portal code - #5048
rockfordlhotka merged 4 commits into
Conversation
The generated IDataPortalOperationMapping/IDataPortalOperationNamedMapping code only covered operation methods the class declares, so operations on a base class (a project base class or CSLA's own, such as BusinessBase.Child_Create) threw DataPortalOperationNotSupportedException from generated code before the data portal fell back to reflection. Concrete classes now include the accessible, non-private operation methods they inherit. Selection follows reflection-based dispatch: a method closer to the class wins (scored as injected parameters less inheritance depth), overrides without the attribute are called virtually at the override's level, and an inherited method whose name the class reuses is called through base. Methods that could bind to the wrong overload, or that reflection could resolve differently, are still left to reflection. Extension methods and the CSLADP002/CSLADP009 analyzers still cover only declared methods. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Track whether a method is inherited separately from its dispatch depth, so overrides without the attribute (such as Child_Create) get no extension methods and no misplaced diagnostics. - Report CSLADP002/CSLADP009 for collisions between a declared and an inherited method, at the declared method, so a cross-level tie that reflection reports as ambiguous is visible at build time. - Leave methods whose signature uses a private or protected nested type out of the generated interface and dispatch; they made the generated code fail to compile (CS0051). Applies to declared methods as well. - Require exact criteria types only between methods at the same level; guard a less specific declared method against criteria that match a method further up the chain instead, so it keeps handling other calls. - Add inherited methods once per type in a separate pipeline step rather than for every operation method. - Dispatch a lone params object[] operation method (the editable lists' Child_UpdateAsync) with the whole criteria array, as reflection does, when no other method of its kind exists. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
🟡 Changes recommended
Generated dispatch can select the wrong operation because it ignores unresolved candidates, excludes competing private signatures, omits injection-based scoring, and fails to recognize derived operation attributes.
4 open issues
What changed in this PR
Updates CSLA .NET’s data portal generator to dispatch supported inherited operation methods directly, reducing reflection fallbacks and debugger interruptions described in #5046.
Changes:
- Adds inherited-method discovery and dispatch, including overrides,
basecalls, and loneparams object[]operations. - Updates collision diagnostics and fallback handling while keeping generated extensions limited to declared operations.
- Adds regression tests, snapshots, documentation, and a release note.
| File | Description |
|---|---|
Source/tests/csla.netcore.test/DataPortal/InheritedOperationDispatchTests.cs |
Tests inherited root and child operations without fallback exceptions. |
Source/tests/csla.netcore.test/DataPortal/DataPortalExtensionsTests.cs |
Resolves interface members against inherited methods. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.SingleFetchWithCriteria#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child-create dispatch. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.SingleCreateNoParams#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child-create dispatch. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.RunLocalAndValueTask#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside asynchronous operations. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.RootAndChildAttributes#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Includes the inherited member while preserving declared-method precedence. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.PrivateNestedCriteria#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records fallback for private criteria types. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.PartialClassAcrossFiles#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch to a split partial class. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.ParamsArrayAndKeywordNames#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records whole-array criteria dispatch and inherited child creation. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.OperationWithInjectParam#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside injected parameters. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.OperationWithInjectAllowNull#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside nullable injection. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.NestedClass#TestApp.Outer.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch within a nested class. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.MultipleOverloads#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child creation alongside overloads. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.KeyedInject#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside keyed injection. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InjectBeforeCriteria#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside mixed parameter ordering. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedSameShapeAcrossLevels#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records guards for overlapping inherited criteria. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedProtectedNestedCriteria#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records fallback for protected criteria types. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedParamsArrayUpdateChild#TestApp.LineItems.DataPortalOperations.g.verified.cs |
Records inherited list-update array dispatch. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedParamsArrayUpdateChild#TestApp.LineItem.DataPortalOperations.g.verified.cs |
Records inherited child creation for list items. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOverriddenWithoutAttribute#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records virtual dispatch to unattributed overrides. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOverloadHiddenByIntermediate#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records fallback for intermediate-class hiding. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOverloadHiddenByDerived#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records explicit base calls. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOperationsFromGenericBase#TestApp.Customer.DataPortalOperations.g.verified.cs |
Records dispatch from a constructed generic base. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOperationsFromGenericBase#TestApp.AppBase_1.DataPortalOperations.g.verified.cs |
Records the abstract base’s declared-operation interface. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOperationNameScoring#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Records cross-level winners and ambiguous fallbacks. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InheritedOperationNameScoring.verified.txt |
Records inherited-operation collision diagnostics. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.InCriteriaAndNullableInject#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside existing parameter cases. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.FileScopedNamespace#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch for file-scoped source. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.ExistingNamedMapping#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited criteria dispatch beside a custom named mapping. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.DuplicateOperationName#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child creation alongside duplicate names. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.DerivedFromBaseWithOperations#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child creation to the derived mapping. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.DeleteWithCriteria#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child-create dispatch. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.CompleteCrud#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside root operations. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.ChildOperations#TestApp.LineItem.DataPortalOperations.g.verified.cs |
Includes the inherited member while retaining declared child creation. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.AsyncMethod#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside asynchronous fetch. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.ArrayNullableGenericCriteria#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited dispatch alongside complex criteria types. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.AmbiguousOverloads#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child creation alongside overload guards. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/Snapshots/DataPortalOperationsGeneratorTests.AmbiguousOperationName#TestApp.PersonEdit.DataPortalOperations.g.verified.cs |
Adds inherited child creation alongside ambiguous names. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalOperations/DataPortalOperationsGeneratorTests.cs |
Adds inheritance, visibility, scoring, and array scenarios. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalExtensions/Snapshots/DataPortalExtensionsGeneratorTests.InheritedOperations#TestApp.PersonEdit.DataPortalExtensions.g.verified.cs |
Records extensions for declared operations only. |
Source/tests/Csla.Generator.AutoImplementProperties.CSharp.Tests/DataPortalExtensions/DataPortalExtensionsGeneratorTests.cs |
Tests exclusion of inherited operations and overrides. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/TrackingNames.cs |
Names the inherited-method pipeline stage. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/OperationDiscovery.cs |
Discovers inherited methods and evaluates callable signatures. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/Models/OperationTypeModel.cs |
Tracks operation kinds with unresolved candidates. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/Models/OperationMethodModel.cs |
Adds inheritance, call-form, array, and visibility metadata. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/IncrementalDataPortalOperationsGenerator.cs |
Adds inherited discovery after per-type grouping. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/DataPortalOperationsBuilder.cs |
Emits inherited calls, selection guards, and array dispatch. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalOperations/DataPortalOperationsAnalyzer.cs |
Reports inherited collisions at declared methods. |
Source/Csla.Generators/cs/AutoImplementProperties/Csla.Generator.AutoImplementProperties.CSharp/DataPortalExtensions/IncrementalDataPortalExtensionsGenerator.cs |
Excludes inherited operations from extensions. |
releasenotes.md |
Documents the inherited-dispatch fix. |
docs/data-portal-operations-generator.md |
Explains inheritance behavior and fallback cases. |
Review effort: Balanced
| var all = model.Methods.Where(m => m.Kind == kind).ToList(); | ||
| var ofKind = dispatchable.Where(m => m.Kind == kind).ToList(); |
There was a problem hiding this comment.
Good catch, fixed in 50aa106.
Filtering the unresolved kinds out entirely would have dropped every generated call of that kind. Instead, inherited methods the generated code can't call now stay in the model with CanCall = false. That covers methods hidden by an intermediate member, methods that aren't accessible, and methods whose types the interface can't name. Because they stay in the model, operation-name selection and criteria dispatch still score them. In your example, the hidden Fetch(int) and Load(int) share Fetch__Int32 and tie, so neither is dispatched and reflection reports the ambiguity. InheritedOverloadHiddenByIntermediate now covers this.
Two cases still can't be modeled: generic operation methods, and declared methods marked with a subclass of an operation attribute. For those kinds UnresolvedKinds now skips both criteria and named dispatch, and the interface members stay in place for static references.
| // When several methods at one level take the same number of | ||
| // criteria, require exact runtime types so a less specific overload | ||
| // cannot capture the call. | ||
| var requireExactTypes = ofKind.Count(m => m.CriteriaCount == method.CriteriaCount && m.InheritanceDepth == method.InheritanceDepth) > 1; |
There was a problem hiding this comment.
Agreed, fixed in 50aa106. Criteria dispatch now weighs a method against every other method of the kind with the same criteria count, including methods the generated code can't call. Those include methods with private or protected nested types, ref/out and params methods, and named-collision losers.
With Fetch(object) and Fetch(PrivateCriteria), the generated Fetch(object) branch now has !(criteria[0] is global::TestApp.PersonEdit.Criteria), so a Criteria value falls through to reflection, which picks the exact overload. This is in the PrivateNestedCriteria snapshot.
| var guards = requireExactTypes || HasExactCriteriaTypes(method) | ||
| ? [] | ||
| : sameShape.Where(m => m.InheritanceDepth > method.InheritanceDepth && !HasSameCriteriaTypes(m, method)).ToList(); |
There was a problem hiding this comment.
Right, the closer-method rule ignored injected parameters. Fixed in 50aa106 by replacing both the closer-method rule and the same-level exact-type rule with the scoring that ServiceProviderMethodCaller uses: exact type 3, assignable 2, object 1, minus inheritance depth, plus injected parameters.
For each other method of the same kind and criteria count, the generator compares the lowest score this method can get with the highest score the other can get.
- The other can't reach it: no guard is added.
- The other could tie or win unless the criteria match this method exactly: the branch gets
!(matches other && !exact). - The other could tie or win regardless: the branch gets
!(matches other). If the other covers every value this method accepts, the method is left out of criteria dispatch. - The generated code can't name the other method's types: the method falls back to requiring exact types, or to reflection.
Your example is the new InheritedInjectionScoring test. Fetch(int) (2) loses to Load(object, 4 × [Inject]) (3), so criteria dispatch calls Load for an int, as reflection does.
Named dispatch is unaffected. The client computes the operation name from the method reflection selects (TryGetProviderMethodInfoFor then ComputeOperationName in DataPortal<T>), so a name always identifies reflection's choice.
To keep the guards from adding noise, parameter models now record a type category and the types each one is assignable to. Guards are left out between types no value can match both, such as int and a class, or int[] and List<int>. Variant generic types and reference-type arrays are treated as possibly overlapping.
| if (IsOperationAttribute(attribute.AttributeClass)) | ||
| { | ||
| var name = attribute.AttributeClass!.Name; | ||
| var kind = name.Substring(0, name.Length - "Attribute".Length); |
There was a problem hiding this comment.
Confirmed: the operation attributes aren't sealed. Fixed in 50aa106.
- Inherited methods:
GetOperationKindwalks the attribute's base types, so an inherited method marked with a subclass such asInitializeChildAttribute : CreateChildAttributeis discovered asCreateChild. It beats the more distantBusinessBase.Child_Create, as it does under reflection (InheritedDerivedOperationAttribute). - Declared methods: these are found by
ForAttributeWithMetadataNameon the exact attribute name, so a declared method with a subclassed attribute isn't modeled. Its kind is now marked unresolved, and all calls of that kind are left to reflection, which finds it (DeclaredDerivedOperationAttribute). Before this change the generated code would have called the inheritedChild_Createinstead.
Addresses PR review: - Inherited methods the generated code cannot call (hidden by an intermediate member, inaccessible, or with types the interface cannot name) stay in the model, marked CanCall = false, so they compete when generated dispatch picks a method, instead of only marking the kind unresolved. Kinds that remain unresolved (generic methods, and declared methods marked with a subclass of an operation attribute) get no generated dispatch at all. - Criteria dispatch compares, for every other method of the kind with the same criteria count, the lowest score the method can have with the highest score the other can have, using reflection's scoring (exact 3, assignable 2, object 1, minus inheritance depth, plus injected parameters). The method is guarded against criteria that match the other whenever the other could tie or win, or left to reflection when the generated code cannot test the other's types. This replaces the same-level exact-type rule and the closer-method rule, and accounts for injected parameters across levels. - Parameter models record a type category and assignable types, so guards between types that no value can match both (int vs a class, int[] vs List<int>) are omitted. Variant generic types and reference arrays are treated as possibly overlapping. - A subclass of an operation attribute counts as that attribute for inherited methods, as GetCustomAttributes<T>(true) does at runtime. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary
IDataPortalOperationMapping/IDataPortalOperationNamedMappingcode. Before, an operation on a base class (a project base likeAppBase<T>, or CSLA's ownBusinessBase.Child_Createand the lists'Child_UpdateAsync) made the generated code throwDataPortalOperationNotSupportedException. The data portal caught it and fell back to reflection, but the debugger broke on it.ServiceProviderMethodCallerchooses them:base..object1, minus inheritance depth, plus injected parameters. For every other method of the same kind and criteria count, it compares this method's lowest possible score with the other's highest. When the other could tie or win, the method is guarded against criteria that match the other, so those calls are left to reflection. This includes methods the generated code cannot call: hidden, inaccessible, or with types the interface cannot name. Guards are skipped between types that no value can match both.params object[]operation method is dispatched with the whole criteria array, as reflection does, when it is the only method of its kind. This covers child lists that declare their own operations.InheritedOperationMethods).BusinessBase.Child_Create. InChildOperationsthe class declares its own[CreateChild], which wins. TheParamsArrayAndKeywordNamessnapshot now dispatches theparams object[]method.GeneratedInterfaceMemberNames_MatchRuntimeOperationNamesnow finds each interface member's method at the nearest declaring type, since the interface can name inherited methods.data-portal-operations-generator.mdand a release note.Test plan
base.callparams object[]updateInheritedOperationDispatchTests(csla.netcore.test) counts first-chanceDataPortalOperationNotSupportedExceptions during create, fetch, and insert/update saves of a root with a child list that declares its own operation. It passes with the fix and fails on main.csla.test.slnx,TestCategory!=SkipOnCIServer)Closes #5046
🤖 Generated with Claude Code