-
Notifications
You must be signed in to change notification settings - Fork 176
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[C# Port] Refactor the action handling system #212
Comments
The current action delegate is quite rigid and has a few flaws. Look at the default actions and how the handler functions are implemented here: Lines 10 to 12 in bfe4412
The flaws are:
The delegate type for the
|
@singhk97 @Stevenic , I have another idea to use parameter attribute to support dynamic argument. This idea comes from Azure Functions Binding that Azure Functions support dynamic argument by different parameter attributes, such as: For example, we provide parameter attributes like public class MyInstance
{
[Action("FullAction")]
public void MyFullActionHandler(
[TurnContext] TurnContext context,
[TurnState(typeof(MyTurnState))] MyTurnState state,
[ActionData(typeof(MyData))] MyData data,
[ActionName] string actionName)
{
return;
}
[Action("PartialAction")]
public void MyPartialActionHandler([ActionName] string actionName, [ActionData(typeof(MyData))] MyData data)
{
return;
}
[Action("DoNothing")]
public void DoNothingActionHandler()
{
return;
}
} By the way, Generic Attributes is just proposed, so for now we may need to pass type to the attribute. |
Adding @vule-msft to the discussion. |
It's what SK used to do. It's no longer what it does. It now supports any number of arguments of any relevant type. |
Thanks for this reminder. It's great to know there's such use pattern in SK. |
Expression.Compile is using reflection emit under the covers to generate custom IL. It's thus much more expensive on first use, in exchange for having invocation performance be much faster. If you're going to be executing the call a lot and the overhead of those calls matter, it can be a worthwhile tradeoff. The difference is measured in nanoseconds, though, so you really do need to be making many, many calls to the resulting delegate for the startup costs to be worth it. Also, recent versions of .NET optimize MethodInfo.Invoke further. In .NET 7, the implementation can actually use reflection emit under the covers to achieve a similar optimization. See https://devblogs.microsoft.com/dotnet/performance_improvements_in_net_7/#reflection for more details. |
This is really helpful. Thanks a lot! |
@stephentoub , the sample and doc you provided are really helpful, and I've implemented new attribute-based handler. Discussed with @singhk97 and one of the good suggestions is to provide an analyzer to check the parameter type of the attribute. I went through SK repo but did not find any similar part. In addition, the only similar analyzer I found is from several years ago. Is there any suggested getting-started or potential risk for providing analyzer to check attribute-binding parameter type? Thanks again! |
Refactor action handling system to support dynamic action import with necessary parameters, resolves #212 - Action method can use parameter attributes `ActionTurnContext`, `ActionTurnState`, `ActionEntities`, `ActionName` to get inputs. - Support `void` / `bool` / `Task` / `Task<bool>` / `ValueTask` / `ValueTask<bool>` return types. - Have runtime type checker. For example, ``` csharp [Action("action1")] public void MyAction( [ActionTurnContext] ITurnContext context, [ActionTurnState] TState state, [ActionEntities] MyDataType entities, [ActionName] string actionName) { ... } ``` This PR may have conflict with the turn state one. I'll merge and resolve conflicts after #232 merged. For code analyzer, created #237 to track to not block the action system itself or sample work.
There are tons of analyzers implemented in https://github.com/dotnet/roslyn-analyzers... you should be able to find some good fodder there. The risk is simply that you're writing code that will run as part of compilation, e.g. if you have a bug and it's really slow, that will significantly slow down compile times. |
No description provided.
The text was updated successfully, but these errors were encountered: