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

[API Proposal]: Support for selecting multiple properties in single expression. #2479

Open
voroninp opened this issue Nov 25, 2023 · 7 comments · May be fixed by #2488
Open

[API Proposal]: Support for selecting multiple properties in single expression. #2479

voroninp opened this issue Nov 25, 2023 · 7 comments · May be fixed by #2488
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation enhancement up-for-grabs

Comments

@voroninp
Copy link
Contributor

Background and motivation

If I write assertion this way:

 actualEnv.Should().BeEquivalentTo(env, _ => _.Excluding(_ => new { _.Id, _.Creator, _.LastEditor }));

the exception is thrown:

System.ArgumentException : Expression <new <>f__AnonymousType0`3(Id = _.Id, Creator = _.Creator, LastEditor = _.LastEditor)> cannot be used to select a member. (Parameter 'expression')

So I have to write it slightly more verbose.

 actualEnv.Should().BeEquivalentTo(env, _ => _.Excluding(_ => _.Id).Excluding(_ => _.Creator).Excluding(_ => _.LastEditor));

API Proposal

So, my proposal is to treat expression for anonymous object creation as an enumeration of properties to exclude.
For example EF Core uses this approach for defining composite key.

API Usage

a.Shoul().BeEquivalentTo(b, _ => _.Excluding(b => new { b.Prop1, ...., b.PropN }));

Alternative Designs

No response

Risks

No response

Are you willing to help with a proof-of-concept (as PR in that or a separate repo) first and as pull-request later on?

No

@voroninp voroninp added the api-suggestion Early API idea and discussion, it is NOT ready for implementation label Nov 25, 2023
@IT-VBFK
Copy link
Contributor

IT-VBFK commented Nov 28, 2023

I assume that should be only possible for Excluding(), .For().Exclude() and Including() and nothing else?

Asking, because strictly speaking e.g. subject.Should().BeEquivalentTo(expected, opt => opt.WithStrictOrderingFor(o => new { o.prop1, o.prop2 })); would also be possible (from API perspective, but makes little sense.. at least for me 🙂). That's why I am limiting this to the first element if applicable.

What does the rest think?

@voroninp
Copy link
Contributor Author

but makes little sense

Why do you think so? 🤔 AFAIK this is also a way of defining ordering, at least for some frameworks.

dotnet/efcore#19367

@IT-VBFK
Copy link
Contributor

IT-VBFK commented Nov 29, 2023

Huh.. that's true
Will also add this to a few missing options

@voroninp
Copy link
Contributor Author

voroninp commented Nov 29, 2023

@IT-VBFK , on the other hand, linq throws because anonymous types do not implement IComparable.

var a = new []
{
    new { x = 1, y = -1},
    new { x = -1, y = 1}
};
a[0].GetType().Dump();
a.Order().ToList();

@ITaluone
Copy link
Contributor

The question is: What do we want to achieve here?
IMO: excluding/including as non-verbose as possible. And possibly ordering?

@dennisdoomen
Copy link
Member

Please refrain from creating a PR until we ask for it or we label the issue as api-approved

@IT-VBFK
Copy link
Contributor

IT-VBFK commented Nov 30, 2023

Ok,

I converted it to a draft. But I leave it open. Take it as a sketch 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api-suggestion Early API idea and discussion, it is NOT ready for implementation enhancement up-for-grabs
Projects
None yet
4 participants