C
C#•11mo ago
fasadin

Chaining async methods. Method not found

I want to have something like this
var foo = await new UserBuilder(_client)
.CreateUser()
.VerifyUser()
.Build();
var foo = await new UserBuilder(_client)
.CreateUser()
.VerifyUser()
.Build();
but this gives me an error Cannot resolve symbol 'VerifyUser' it's because CreateUser returns Task<T> if I write like this
var foo = await new UserBuilder(_client)
.CreateUser();
foo = await asd.VerifyUser();
var bar = await asd.Build();
var foo = await new UserBuilder(_client)
.CreateUser();
foo = await asd.VerifyUser();
var bar = await asd.Build();
or like this
var asd = await (await (await new UserBuilder(_grupieClient)
.CreateUser())
.VerifyUser())
.Build();
var asd = await (await (await new UserBuilder(_grupieClient)
.CreateUser())
.VerifyUser())
.Build();
then it works as expected but second and third solution it's ugly. How to achieve desired syntax?
15 Replies
fasadin
fasadinOP•11mo ago
its similiar problem to this https://stackoverflow.com/questions/25302178/using-async-tasks-with-the-builder-pattern but having Then it's not a solution for me 😦
Angius
Angius•11mo ago
The second solution is preferable
fasadin
fasadinOP•11mo ago
not for me, it’s ugly. If I have 10 more methods that I can chain then I will assign foo multiple times just to call next method
Angius
Angius•11mo ago
The third solution, then Every async method needs to be awaited to get the actual data from it that isn't wrapped in a Task Period However you want to chain it is up to you, but chain it you must Because Task<Foo> will not have the methods of Foo
fasadin
fasadinOP•11mo ago
yes I understand, but there has to be workaround. You wrote yourself that he has no methods for it. So I can write extension method for Task<Foo> 😉
Angius
Angius•11mo ago
That method would still have to await inside And thus, would need to be async And thus, would need to be awaited itself
fasadin
fasadinOP•11mo ago
yes, but caller would does not see it and it would be as first example
Angius
Angius•11mo ago
Yes the caller would see Or... huh Not sure tbh I was never into going against the grain of the languages I use, so no idea tbh Try if, if writing 2783645 additional largely useless methods would fulfill your aesthetic needs lmao
fasadin
fasadinOP•11mo ago
lol, so you defending your lack of knowledge with some bullshit that I am doing something against language 😄 next time if you don’t know something. Don’t respond, as you are clearly not capable of helping if it’s beyond your skill level Probably you didn’t even used templates if you are writing nonsense about writing millions of methods. Sad 😞
Angius
Angius•11mo ago
No, I'm defending the use of a simple additional variable instead of writing at least 4 lines of an extension method
var a = await Foo();
var b = await a.Bar();
var a = await Foo();
var b = await a.Bar();
instead of
var b = await Foo().Buzz();

public static class TaskExtensions
{
public static Task<Bar> Buzz(this Task<Foo> task)
{
var foo = await task;
return await foo.Bar();
}
}
var b = await Foo().Buzz();

public static class TaskExtensions
{
public static Task<Bar> Buzz(this Task<Foo> task)
{
var foo = await task;
return await foo.Bar();
}
}
One requires an additional variable — the horror! The other requires a whole another class and a method Assuming it works It's not beyond my skill level, far from it, I just try to not thing of how to write the most useless and verbose code possible
fasadin
fasadinOP•11mo ago
- in your example a and b have different types, in my example the have the same type - what if you have 10 other methods? Then you are going to have 10 other variables? Is it still readable and nice? Can it be solve with one method? (I think it can) even if that would mean to create 10-20 extra variables that have no purpose? oh, interesting so creating N extra variables is not vervose and useless code, but one extra method for chaining is? 😄
Angius
Angius•11mo ago
Eh, go ahead with your plan then
fasadin
fasadinOP•11mo ago
ah, so when we expand example to be more difficult that does not fit your point, then you no longer have answer, so only you can say "whatever" and leave.
Angius
Angius•11mo ago
Yeah, nah, I simply don't care enough to continue arguing
fasadin
fasadinOP•11mo ago
for me it looks like you are not good with helping. If anyone challenges your advice (because in this case it's opinion, and not a real argument) then you are "offended" and leave what ever it is btw check Language.Ext nuget package, maybe that will help you a bit widen your horizons for anyone that would be interested I've endup with something like this
var profile = await new UserBuilder(_client)
.CreateUser()
.Then(userBuilder => userBuilder.VerifyUser())
.Then(userBuilder => userBuilder.Build())
.Then(user => Utils.HttpUtil.AuthenticateHttpClient(_client, user.Token))
.Then(client => new ProfileBuilder(client).ChangePreferredLanguage(language))
.Then(profileBuilder => profileBuilder.Build());
var profile = await new UserBuilder(_client)
.CreateUser()
.Then(userBuilder => userBuilder.VerifyUser())
.Then(userBuilder => userBuilder.Build())
.Then(user => Utils.HttpUtil.AuthenticateHttpClient(_client, user.Token))
.Then(client => new ProfileBuilder(client).ChangePreferredLanguage(language))
.Then(profileBuilder => profileBuilder.Build());
it's not ideal, but it's ok
Want results from more Discord servers?
Add your server