Comments (7)
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.
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.
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.
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.
Do you like me to implement this enhanced matching or are you planning to create a pull request?
from nein-linq.
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.
I think that works well. Thank you very much!
from nein-linq.
Related Issues (20)
- NotSupportedException when using DbInjectable within a sub query HOT 13
- [Nullsafe Queries] Default values for Nullable<T> causing troubles HOT 3
- NeinLinq injects hiding member in injectable member of declaring type
- .CountAsync() and .FirstAsync() throws exception when using ToInjectable HOT 5
- DynamicQuery.CreatePredicate doesn't work with Enums HOT 4
- Query tags are stripped away HOT 3
- Is it possible to store the result of an expression in a property? HOT 4
- Readonly CachedExpressions HOT 1
- Documentation: What does calling WithLambdaInjection() on DbContext options do? HOT 2
- Support For Entity Framework Core 6 HOT 18
- Doc bug HOT 1
- How about an implicit cast to `CachedExpression<X>` from `Expression<X>`? HOT 5
- Property vs method issue HOT 4
- ToInjectable vs ToEntityInjectable HOT 4
- Is `FirstOrDefaultAsync` supported HOT 1
- Q: would this work with source generators? HOT 2
- [Question/Feature Request] Overwrite member/property mapping HOT 1
- [Help] Translating from LinqKit Invoke + Expand to NeinLinq HOT 1
- Issue with NeinLinq.EntityFramework, Linq.Count() method HOT 6
- Issue with Moq HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nein-linq.