如何返回修改多个项目的方法的状态?

发布于 2025-01-10 17:04:59 字数 2121 浏览 0 评论 0原文

我有一个可以更新许多项目的方法。由于并发性,某些项目可以正确更新,另一个项目无法更新,因为用户发送了不正确的数据,而另一些项目则无法更新,因为另一个进程可能删除用户想要更新的项目。

所以我在想如何向图书馆的使用者提供哪些项目可以更新、哪些项目有错误以及哪些项目因为被删除而无法更新的信息。

我正在考虑类似的方法,但我觉得它有点臭代码。

public List<TypeItems> UpdateItems(IEnumerable<MyType> paramItems)
{
    List<MyTyme> myLstCorrectItems = new List<MyType>();
    List<Exception> myLstExceptions = new List<Exception>();

    foreach (MyTime item in paramItems)
    {
        try
        {
            Item.Update(newValue);
            myLstCorrectItems.Add(item);
        }
        catch (System.Exception)
        {
            Exception myException = new Exception("ERROR " + item.ID + ex.Message);
            myLstExceptions.Add(myException);
        }
    }


    if(myLstExceptions.Count == 0)
    {
        return myLstCorrectItems;
    }



    foreach(MyType iterator in myLstCorrectItems)
    {
        Exception myException = new Exception("OK " + iterator.ID);
        myLstExceptions.Add(myException);
    }


    throw new AggregateException("ERRORS", myLstExceptions);
}

代码的想法是,如果没有异常的项目,则返回所有更新的项目的列表。当消费者收到列表时,可以比较某个项目是否不在列表中,则表明该项目已被删除,并可以向用户发出警告。如果该项目存在,则它会因更新而具有新值。

如果至少有一项存在错误,我会创建一个聚合异常,在其中添加所有异常。另外,我为正确的商品创建了一个“假期待”,这样消费者就会收到所有处理过的商品。因此,如果某个项目不在一致门异常中,它可以知道该项目已从另一个进程中删除。

也许使用自定义异常会更好,以避免解析错误的字符串,看看它是 OK 还是 ERROR,但这不是我的疑问。

我怀疑是否要知道总体想法是否好,是否使用“假期望”来包含正确的项目。

但如果有更好或替代的方式来通知每个项目的最终结果,我会很高兴能够了解它们。

谢谢。

编辑:解决方案 1:返回正确和不正确的项目:

public (List<MyType> CorrectItems, List<MyType> IncorrectItems) UpdateItems(IEnumerable<MyType> paramItems)
{
    List<MyTyme> myLstCorrectItems = new List<MyType>();
    List<Mytype> myLstIncorrectItems = new List<Mytype>();

    foreach (MyTime item in paramItems)
    {
        try
        {
            Item.Update(newValue);
            myLstCorrectItems.Add(item);
        }
        catch (System.Exception)
        {
            myLstIncorrectItems.Add(item);
        }
    }


    return (myLstCorrectItems, myLstIncorrectItems);
}

I have a method that update many items. Because of concurrency, some items could be update correctly, another can't update because user send incorrect data and anothers can't be update because another process could delete the item that user wants to update.

So I was thinking how give information to the consumer of the library which items could be updated, which items has errors and which items couldn't be update because they are deleted.

I was thinking a method something like that, but I have the feel that it is a bit smelly code.

public List<TypeItems> UpdateItems(IEnumerable<MyType> paramItems)
{
    List<MyTyme> myLstCorrectItems = new List<MyType>();
    List<Exception> myLstExceptions = new List<Exception>();

    foreach (MyTime item in paramItems)
    {
        try
        {
            Item.Update(newValue);
            myLstCorrectItems.Add(item);
        }
        catch (System.Exception)
        {
            Exception myException = new Exception("ERROR " + item.ID + ex.Message);
            myLstExceptions.Add(myException);
        }
    }


    if(myLstExceptions.Count == 0)
    {
        return myLstCorrectItems;
    }



    foreach(MyType iterator in myLstCorrectItems)
    {
        Exception myException = new Exception("OK " + iterator.ID);
        myLstExceptions.Add(myException);
    }


    throw new AggregateException("ERRORS", myLstExceptions);
}

The idea of the code is that if there is no items with exceptions, return the list of all the items that are updated. When the consumer receive the list, it can compare if some item is not in the list, it means it was deleted and can warning to the user. If the item exists, it has the new values because of the update.

If there is at least one item with errors, I create an aggregate exception in which I add all the exceptions. Also, I create a "fake expcetion" for the correct items, so the consumer would receive all the processed items. So if some item is not in the agreegate exception, it can know that was deleted from another process.

Perhaps it would be better to use a custom exception, to avoid to parse the string of the error, to see if it is OK or ERROR, but this is not my doubt.

My doubt it is to know if it is the general idea is good or not, to use a "fake expcetion" to include the correct items.

But if there is a better or alternative ways to notify the final result of each item, I would be good to can know them.

Thanks.

EDIT: Solution 1: return correct and incorrect items:

public (List<MyType> CorrectItems, List<MyType> IncorrectItems) UpdateItems(IEnumerable<MyType> paramItems)
{
    List<MyTyme> myLstCorrectItems = new List<MyType>();
    List<Mytype> myLstIncorrectItems = new List<Mytype>();

    foreach (MyTime item in paramItems)
    {
        try
        {
            Item.Update(newValue);
            myLstCorrectItems.Add(item);
        }
        catch (System.Exception)
        {
            myLstIncorrectItems.Add(item);
        }
    }


    return (myLstCorrectItems, myLstIncorrectItems);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我最亲爱的 2025-01-17 17:04:59

创建这些假异常来标记正确的项目是一个坏主意。这非常令人困惑,我当然不会想到会这样。另一个解决方案是为结果创建您自己的类型:

public UpdateItemsResult UpdateItems(IEnumerable<MyType> paramItems)
{
   // ...
}

您的类型可能如下所示:

public class UpdateItemsResult
{
   public List<int> IdsOfUpdatedItems {get;set;}

   public List<int> IdsOfFailedItems {get;set;}
}

第一个列表包含更新起作用的项目的 ID。第二个列表包含发生错误的项目 ID。当然,您可以根据需要调整类型。

It is a bad idea, to create these fake exceptions to mark the correct items. This is very confusing and I certainly would not expect it. Another solution is to create your own type for the result:

public UpdateItemsResult UpdateItems(IEnumerable<MyType> paramItems)
{
   // ...
}

Your type might look like this:

public class UpdateItemsResult
{
   public List<int> IdsOfUpdatedItems {get;set;}

   public List<int> IdsOfFailedItems {get;set;}
}

The first list contains the ids of the items, at which the update worked. The second list contains the ids of items, at which some error occurred. Of couse, you can adapt the type to your needs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文