You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
But I am finding it hard to combine evertything using Bind without running into type inference issues. What I have now is:
var result = Validator.Validate<Command, CommandValidator>(command)
.Map(x => UnitResult.Success<Command>());
if (result.IsFailure)
return HandleError(result.Error);
var itemExists = await itemRepository
.GetSingleByName(command.Name, cancellationToken)
.Bind(x => x.Match(
Some: _ => UnitResult.Failure<Error>(DatabaseErrorHelper.Conflict($"Item '{command.Name}' already exists")),
None: UnitResult.Success<Error>
));
if (itemExists.IsFailure)
return HandleError(itemExists.Error);
var newItem = await itemRepository
.Create(new Item(command.Name), cancellationToken)
.Bind(id => itemRepository.SaveChanges(cancellationToken).Map(_ => id));
return newItem.IsSuccess
? Created(newItem.Value)
: HandleError(newItem.Error);
Validator.Validate<Command, CommandValidator>(command) returns a Result<T, Error[]> . So when I bind it, it always returns the T, in this case, Command. IBy mapping it to a UnitResult i thought I could pass it down to the itemRepository stuff, to make it all part of a single chain (Validate, check for existance, Handle any error) before proceeding with Create and Save.
In "langExt" you could simply do "pure" to just pass the object further down the call chain, but I am having trouble understanding how I am supposed to handle a result in a case like this?
var result = Validator.Validate<Command, CommandValidator>(command)
.Map(x => UnitResult.Success<Command>())
.Bind(result => result.Success => continue, result.Failure: Abort and handle error);
Dividing it into three "steps" (Validation, check DB and then save) works, but, I would like to handle it more like a signle call chain, if possible.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to achive seomtehing similiar to what I had using LanguageExt:
But I am finding it hard to combine evertything using Bind without running into type inference issues. What I have now is:
Validator.Validate<Command, CommandValidator>(command)returns aResult<T, Error[]>. So when I bind it, it always returns theT, in this case,Command. IBy mapping it to a UnitResult i thought I could pass it down to theitemRepositorystuff, to make it all part of a single chain (Validate, check for existance, Handle any error) before proceeding with Create and Save.In "langExt" you could simply do "pure" to just pass the object further down the call chain, but I am having trouble understanding how I am supposed to handle a result in a case like this?
Dividing it into three "steps" (Validation, check DB and then save) works, but, I would like to handle it more like a signle call chain, if possible.
Any help would be appreciated
Beta Was this translation helpful? Give feedback.
All reactions