gpt4 book ai didi

c# - 在 C# 中链接多个相同方法的调用

转载 作者:太空宇宙 更新时间:2023-11-03 12:35:14 26 4
gpt4 key购买 nike

我有一个更新方法,它获取最后一个事件项目并使用新值克隆它并将事件标志设置为 true。我的问题是 update 方法被多个 web api 方法调用,所以最后一个事件项目并不总是相同的。我最终遇到了多个事件项目和数据不一致的问题。所以我正在考虑链接所有调用来解决这个问题,但我不知道从哪里开始。`

[HttpPost]
[Route("Route2")]
[ValidateModel]
public async Task<HttpResponseMessage> Post(string contractReference, [FromBody] Family input)
{
return await CallPartialUpdate(contractReference, p => p.Family = input);
}


[HttpPost]
[Route("Route3")]
[ValidateModel]
public async Task<HttpResponseMessage> Post(string contractReference, [FromBody] Address input)
{
return await CallPartialUpdate(contractReference, p => p.Address = input);
}



private async Task<HttpResponseMessage> CallPartialUpdate(string reference, Item itemToUpdate)
{
try
{
var existingItem = _bContext.RetrieveLastActive(reference);

if (existingItem == null)
return Request.CreateResponse(HttpStatusCode.NotFound);

var newRecord = existingItem.Document.Clone();

newRecord.update(itemToUpdate);
newRecord.active = true;

await _bContext.PutDocumentAsync(newRecord, reference);

return Request.CreateResponse(HttpStatusCode.Created);
}
catch (System.Exception exception)
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exception.Message);
}
}`

基于@Asti 的回答,我创建了一个带有 Rx observable 的单元测试,但我在最后一项中仍然存在数据不一致,所以我如何让它工作以及我如何获得 callPartialUpdate() 的结果谢谢

    [TestFixture]
public class Concurrency
{

[Test]
public async Task Update_Should_Always_Change_Last_Item()
{
var observer = Observable.Timer(TimeSpan.FromMilliseconds(1));
var items = new List<Item>()
{
new Item() { FirstName = "AA", LastName = "BB" , IsActive = true },
new Item() { FirstName = "A", LastName = "A" , IsActive = false },
};
await Task.Run(() =>
{
Parallel.Invoke(async () => await observer.Select(item => Observable.FromAsync(ct => UpdateItem(items, new Item() { FirstName = "AAA" })))
.Concat(),
async () => await observer.Select(item => Observable.FromAsync(ct => UpdateItem(items, new Item() { LastName = "BBB" })))
.Concat());
});
var lastItem = items.Single(w => w.IsActive);
Assert.AreEqual("AAA", lastItem.FirstName);
Assert.AreEqual("BBB", lastItem.LastName);
}


public async Task<bool> UpdateItem(List<Item> items, Item itemToUpdate)
{
return await Task.Run(() => update(items, itemToUpdate));
}

private bool update(List<Item> items, Item itemToUpdate)
{
var lastItem = items.Single(w => w.IsActive == true);
lastItem.IsActive = false;
var newItem = new Item()
{
FirstName = string.IsNullOrEmpty(itemToUpdate.FirstName) ? lastItem.FirstName : itemToUpdate.FirstName,
LastName = string.IsNullOrEmpty(itemToUpdate.LastName) ? lastItem.LastName : itemToUpdate.LastName,
IsActive = true
};

items.Add(newItem);
return true;
}
}

最佳答案

对于更新序列,无论是可枚举的还是可观察的:

        updates
.Select(item => Observable.FromAsync(ct => CallPartialUpdate(item)))
.Concat();

如果你想以基于 Rx 的 API 为目标,那么

  • 让方法调用返回 observable
  • 等待可观察对象而不是任务
  • 观察专用调度程序以部分排序工作单元

关于c# - 在 C# 中链接多个相同方法的调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41323409/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com