C# - 试图重构功能编程块
我试图重构此代码,其中是否存在对象是否在缓存中存在差异。如果不在缓存中,则消耗外部API并将结果返回到lambda表达式变量。如果是,请使用缓存变量。
我的问题是,我认为代码块是两次编写的,唯一的区别是变量。
var cacheHit = _cache.TryGetValue("ClientUserId", out string cachedUser);
if (!cacheHit)
{
await _userM.GetUserAsync(
dbData.CreateEmployeeID.ToString(),
cancellationToken)
.MapOnFailure(failure => new AppF(failure.Message, failure.Exception))
.DoAsync(async user =>
{
_cache.Set("ClientUserId", user.ClientUserId, cacheExpiration);
var opsRequest = new OperationStatusRequest();
var results = await _monitoringService.LogHttpRequestResponseAsync(
operationStatusRequest,
JsonSerializer.Serialize(cRequest),
async () =>
await _client.DetailsAsync(user.ClientUserId, cancellationToken) //from API call
.DoAsync(async _ =>
{
//code here
}
).DoOnFailureAsync(async _ =>
{
//code here
});
}); //user
}
else
{
//don't call GetUserAsync if user is already cached, proceed with the operation
var opsRequest = new OperationStatusRequest();
var results = await _monitoringService.LogHttpRequestResponseAsync(
operationStatusRequest,
JsonSerializer.Serialize(cRequest),
async () =>
await _client.DetailsAsync(cachedUser, cancellationToken) //cachedUser
.DoAsync(async _ =>
{
//code here
}
).DoOnFailureAsync(async _ =>
{
//code here
});
}
我已经对主要区别发表了评论,但是IF中的代码几乎遵循同一操作。
有没有办法在不重新编写整个实现的情况下编写IF-ELSE块?
I am trying to refactor this code wherein there is a difference whether an object is in a cache or not. If it's not in cache, then consume an external API and return result to a lambda expression variable. If it is, use the cached variable.
My issue is that I think the code block is written twice with the only difference is the variable.
var cacheHit = _cache.TryGetValue("ClientUserId", out string cachedUser);
if (!cacheHit)
{
await _userM.GetUserAsync(
dbData.CreateEmployeeID.ToString(),
cancellationToken)
.MapOnFailure(failure => new AppF(failure.Message, failure.Exception))
.DoAsync(async user =>
{
_cache.Set("ClientUserId", user.ClientUserId, cacheExpiration);
var opsRequest = new OperationStatusRequest();
var results = await _monitoringService.LogHttpRequestResponseAsync(
operationStatusRequest,
JsonSerializer.Serialize(cRequest),
async () =>
await _client.DetailsAsync(user.ClientUserId, cancellationToken) //from API call
.DoAsync(async _ =>
{
//code here
}
).DoOnFailureAsync(async _ =>
{
//code here
});
}); //user
}
else
{
//don't call GetUserAsync if user is already cached, proceed with the operation
var opsRequest = new OperationStatusRequest();
var results = await _monitoringService.LogHttpRequestResponseAsync(
operationStatusRequest,
JsonSerializer.Serialize(cRequest),
async () =>
await _client.DetailsAsync(cachedUser, cancellationToken) //cachedUser
.DoAsync(async _ =>
{
//code here
}
).DoOnFailureAsync(async _ =>
{
//code here
});
}
I have placed a comment on the main difference but the code in if and else block pretty much follows the same operation.
Is there a way that I can write the if-else block without re-writing the whole implementation twice?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论