Created
December 18, 2019 15:56
Star
You must be signed in to star a gist
Real life example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private readonly PersonRepository personRepository; | |
private readonly ILogger logger; | |
private readonly TwitterService twitterService; | |
public PersonService(ILogger logger) | |
{ | |
this.logger = logger; | |
personRepository = new PersonRepository(); | |
twitterService = new TwitterService(); | |
} | |
private TryAsync<Context> CreateContext(long personId) | |
{ | |
return TryAsync(() => personRepository.GetById(personId)) | |
.Map(person => new Context(person)); | |
} | |
private TryAsync<Context> RegisterTwitter(Context context) | |
{ | |
return TryAsync(() => twitterService.Register(context.Email, context.Name)) | |
.Map(account => context.SetAccount(account)); | |
} | |
private TryAsync<Context> Authenticate(Context context) | |
{ | |
return TryAsync(() => twitterService.Authenticate(context.Email, context.Password)) | |
.Map(token => context.SetToken(token)); | |
} | |
private TryAsync<Context> Tweet(Context context) | |
{ | |
return TryAsync(() => twitterService.Tweet(context.Token, "Hello les cocos")) | |
.Map(tweet => context.SetTweet(tweet)); | |
} | |
private TryAsync<Context> UpdateParty(Context context) | |
{ | |
return TryAsync(async () => | |
{ | |
await personRepository.Update(context.Id, context.AccountId); | |
return context; | |
}); | |
} | |
public async Task<string> Register(long personId) | |
{ | |
string result = string.Empty; | |
await CreateContext(personId) | |
.Bind(RegisterTwitter) | |
.Bind(Authenticate) | |
.Bind(Tweet) | |
.Bind(UpdateParty) | |
.Match( | |
context => | |
{ | |
logger.LogSuccess($"User {personId} registered"); | |
result = context.Url; | |
}, | |
exception => logger.LogFailure($"Unable to register user : {personId} {exception.Message}")); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment