Giter Club home page Giter Club logo

Comments (7)

axelheer avatar axelheer commented on May 27, 2024

An interesting idea. Great!

Maybe we can solve it without introducing another attribute? The current mechanism works like that:

// a method with the signature
public static TResult Method(TArg[0], ..., TArg[n-1])
// is being matched with
public static Expression<Func<TArg[0], ..., TArg[n-1], TResult>> Method()

If we improve the matching in the following way:

// a method with the signature
public static TResult Method(TArg[0], ..., TArg[n-1])
// is tried being matched with
public static Expression<Func<TArg[0], ..., TArg[n-1], TResult>> Method()
// then
public static Expression<Func<TArg[0], ..., TArg[n-2], TResult>> Method(TArg[n-1])
// then
public static Expression<Func<TArg[0], ..., TArg[n-3], TResult>> Method(TArg[n-2], TArg[n-1])
// ...

Thus, we match string MethodA(this Whatever w, int number) with Expression<Func<Whatever, string>> MethodA(int number) and also FlavourMethod(this Whatever w, Flavour f, bool sprinkles) with Expression<Func<Whatever, string>> FlavourMethod(Flavour f, bool sprinkles). And since we start with all the arguments within the expression, we don't break current behavior.

Would that help? What do you think?

from nein-linq.

BenJenkinson avatar BenJenkinson commented on May 27, 2024

I think that would work very well!

In your example, (assuming I understood correctly) you seem to be taking the parameters off the end of the expression, rather than the beginning, is that intentional?

string Method(this Whatever w, int a, int b, int c)

// ---------------------------------------------------------

// Matching last-args-first (your example?)
Expression<Func<Whatever, int, int, int, string>> Method()
Expression<Func<Whatever, int, int, string>> Method(c)
Expression<Func<Whatever, int, string>> Method(b, c)
Expression<Func<Whatever, string>> Method(a, b, c)

// ---------------------------------------------------------

// Matching first-args-first
Expression<Func<Whatever, int, int, int, string>> Method()
Expression<Func<Whatever, int, int, string>> Method(a)
Expression<Func<Whatever, int, string>> Method(a, b)
Expression<Func<Whatever, string>> Method(a, b, c)

Do you think first-args-first is more intuitive?

from nein-linq.

axelheer avatar axelheer commented on May 27, 2024

Yep, that was intentional.

We cannot really take off the first parameter, if we're using extension methods (which is usually the case for me). Strictly speaking the first-args-first approach would look like that:

Expression<Func<Whatever, int, int, int, string>> Method()
Expression<Func<int, int, int, string>> Method(w)
Expression<Func<int, int, string>> Method(w, a)
Expression<Func<int, string>> Method(w, a, b)
Expression<Func<string>> Method(w, a, b, c)

Thus, since taking w off doesn't make sense, we would start with the second parameter for extension methods, but maybe the first for ordinary methods... The last-args-first approach isn't very intuitive at first sight, but at least it's consistent (hopefully).

If I may use your previous example with another parameter:

var yep = true;
var foo = (from d in data.ToInjectable()
           from w in d.Whatever
           select d.FlavourMethod(w, Flavour.Chocolate, yep)).ToList();

Then the first parameters (d and w accordingly) are "real query parameters" and the last parameters (Flavour.Chocolate and yep) are "non query parameters". Looking at it from this angle, it's quite considerable, i think.

from nein-linq.

BenJenkinson avatar BenJenkinson commented on May 27, 2024

Yep, I wrote that all out wrong and missed the extension method param. Oops.

I agree, last-args-first would be more consistent; especially with extension methods and non-extension methods.

from nein-linq.

axelheer avatar axelheer commented on May 27, 2024

Do you like me to implement this enhanced matching or are you planning to create a pull request?

from nein-linq.

axelheer avatar axelheer commented on May 27, 2024

Had a similar problem, but finally solved it another way (7a0bfba):

Non static methods are supported now too, which means the method can decide which expression to return based on local class data, which should be even more flexible. Furthermore no first-last-whatever magic is necessary.

public class Flavorizer
{
    private readonly Flavour flavor;

    public class Flavorizer(Flavour flavor)
    {
        this.flavor = flavor;
    }

    [InjectLambda]
    public string Something(Whatever whatever)
    {
        return ...
    }

    public Expression<Func<Whatever, string>> Something()
    {
        switch (flavor)
        {
            case Flavour.Vanilla:
                return ...
            case Flavour.Chocolate:
                return ...
            default:
                return ...
        }
    }
}

// ------------------------

var flavorizer = new Flavorizer(Flavour.Chocolate);
var foo = data.ToInjectable()
    .Select(d => flavorizer.Something(d))
    .ToList();

What do you think?

from nein-linq.

BenJenkinson avatar BenJenkinson commented on May 27, 2024

I think that works well. Thank you very much!

from nein-linq.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.