Skip to content
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

Generic Modal Parameters[Feature Request] #448

Open
thepigeonfighter opened this issue Sep 23, 2022 · 2 comments
Open

Generic Modal Parameters[Feature Request] #448

thepigeonfighter opened this issue Sep 23, 2022 · 2 comments
Labels
Feature Request Request to add a new feature Triage Issue needs to be triaged

Comments

@thepigeonfighter
Copy link

thepigeonfighter commented Sep 23, 2022

Is your feature request related to a problem? Please describe.
I've have accidentally passed parameters that don't match Component Parameter Types into the ModalParameters object because there are no Type constraints.

Describe the solution you'd like
Create a generic Modal Parameters class like:

public class ModalParameters<TModal> : ModalParameters
{
    public void AddParameter<TProperty>(Expression<Func<TModal, TProperty>> propertyExpression, TProperty value)
    {
        if (propertyExpression is null)
        {
            throw new ArgumentNullException(nameof(propertyExpression));
        }
        if (value is null)
        {
            throw new ArgumentNullException(nameof(value));
        }
        LambdaExpression lambda = propertyExpression;
        var memberExpression = lambda.Body is UnaryExpression expression
            ? (MemberExpression)expression.Operand
            : (MemberExpression)lambda.Body;
        
        var propInfo = (PropertyInfo)memberExpression.Member;

        Add(propInfo.Name, value);
    }
}

As a note it is AddParameter and not Add to maintain backwards compatibility and to get the complier type safety. I originally had the method as an overload of Add but then you wouldn't get the complier error thus defeating the purpose.

Usage

 var modalParam = new ModalParameters<TModal>();
 modalParam.AddParameter(x => x.Foo, _foo);
@thepigeonfighter thepigeonfighter added Feature Request Request to add a new feature Triage Issue needs to be triaged labels Sep 23, 2022
@SSchulze1989
Copy link

This would be a really nice addition and helps a lot to clean up the code.
It helps especially in the case of passing EventHandlers or Functions to the modal, for example if i want an async submit action to be performend before closing the modal:

var parameters = new ModalParameters()
    .Add(nameof(ModalComponent.OnSubmit), new Func<ModelType, CancellationToken, Task<bool>>(
        (model, cancellation) => OnSubmitAction(model, cancellation));

with the generic version the code simplifies to:

var parameters = new ModalParameters<ModalComponent>()
    .Add(x => x.OnSubmit, new((model, cancellation) => OnSubmitAction(model, cancellation));

And the latter is also typesafe.

This saves a lot of typing (especially when doing so in the dreadful razor editor ...)
Since I am using a lot of modals for async updating data via REST API and i always want to wait for a response before closing I do encounter this problem often in my application.

@thepigeonfighter i did implement the same thing on my end and did not have any issues with the Method being named Add() as well. Since the first parameter is an expression and not a string the compiler can pick the correct overload.

@BenMakesGames
Copy link
Contributor

BenMakesGames commented Apr 26, 2023

we've written a basically-identical extension for our project at work. however: I wouldn't want such a method to throw an ArgumentNullException for null parameter values. there are many times we have optional (nullable) parameters in our modals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature Request Request to add a new feature Triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

3 participants