使用事务是否可以消除使用辅助 dataContext 来尝试 sql 插入/更新/删除的需要?

发布于 2024-08-04 13:40:29 字数 804 浏览 2 评论 0原文

因此,我通过 Linq-To-Sql 将 IT/开发人员票证列表存储在数据库中。保存尝试仅在用户请求时进行,或者如果他们说关闭时保存。我担心的是,用户更改了 2 张或更多门票,但中间没有保存。

由于某种原因,如果数据库拒绝对一项进行更改,则不会向我提供有关哪一项或哪张票证上的哪个字段出现问题的太多信息,因此我可以将其与向用户提供指示符联系起来。现在我无法保存他们编辑的记录上的好数据,因为一项更改被困在队列中,现在 SubmitChanges 不再起作用。

我构建了一个缓冲系统,其中每个票证都包装在另一个类中,以便将更改保存到缓冲区而不是直接保存到 linq to sql 对象,其中对于每个票证更改:

  • 我可以创建一个新的 dataContext 实例
  • 尝试保存更改 然后,每一行
  • 都会向用户报告失败的每一行。

我猜这段代码有很大的味道,或者至少是丑陋的。

我的同事刚刚建议我尝试交易。我不想拆解我为测试事务方法而构建的东西。

  • 事务是否会正确重置对某个项目的所有更改,或 SaveChanges 尝试保存的所有项目?之后我希望 hasChanges 为空,并且 SaveChanges 不执行任何操作。
  • 有没有更好的方法在 linq-to-sql 中一次提交单独的行更改?
  • 我是否在 SaveChanges 异常中遗漏了一些真正可以帮助我了解哪一行以及该行上的哪个字段出现问题的内容?

也许我不应该允许(因为 linq-to-sql 或者现实世界不需要在不决定保存或不保存的情况下对多个单元进行更改的能力)用户留下票证,直到他们决定是否想要是否保存更改?

So I have a list of IT/developer tickets stored in a database via Linq-To-Sql. Save attempts are only done at the user request, or if they say save on close. My concern was that a user makes a change to 2 or more tickets without saving in-between.

For some reason if the database rejects the change to one, doesn't give me very much information about which item, or which field on which ticket has the problem so I can tie it back to giving an indicator to the user. Now I can't save the good data on the records they edited, because one change is stuck in the queue, and now SubmitChanges no longer functions.

I built a buffering system where every ticket gets wrapped in another class so that changes are saved to a buffer instead of directly to the linq to sql object, where for each ticket changed:

  • I could create a new dataContext instance
  • attempt to save the changes for an individual row
  • then report back to the user each one that failed.

The code I'm guessing has quite a smell, or at least is ugly.

My co-worker just suggested I try transactions. I'd rather not do tear-down of what I've built to test a transactional approach.

  • Will transactions correctly reset all the changes to an item, or all items that SaveChanges would attempt to save? afterwards I would expect hasChanges to be empty, and SaveChanges to do nothing.
  • Is there a better way to submit individual row changes at a time in linq-to-sql?
  • Am I missing something in SaveChanges exceptions that would really help me know which row, and which field on that row is having a problem?

Perhaps I shouldn't allow (because of linq-to-sql or the real world doesn't need the ability to make changes on multiple units without deciding to save or not) the user to leave the ticket until they have decide if they want to save changes to it or not?

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

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

发布评论

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

评论(1

世界和平 2024-08-11 13:40:29

我已经推出了自己的缓冲类,这有点混乱,但肯定可以一次保存一条记录。

这是围绕 linq 实体的业务对象的父类。

public class BufferedLinqChange
{
    LqGpsDataContext _dataContext;

    internal BufferedLinqChange(LqGpsDataContext dataContext)
    {
        _dataContext = dataContext;
    }

    protected void SetBufferedProperty<T>(string key,Action linqAction
        ,bool linqEqualsValue,Action bufferAction)
    {
        if (linqEqualsValue)
        {
            if (Changes.ContainsKey(key))
                Changes.Remove(key);
        }
        else
        Changes.InsertOrUpdate(key, linqAction); bufferAction();
    }

    protected Dictionary<String, Action> Changes = new Dictionary<string, Action>();

    public int ChangeCount { get { return Changes != null ? Changes.Count : 0; } }
    public bool hasChanges { get { return Changes != null ? Changes.Count > 0 : false; } }

    public void SubmitChanges()
    {
        _dataContext.SubmitChanges();
        if (ChangeCount > 0)
        {
            Changes.ForEach((item) => item.Value.Invoke());
            _dataContext.SubmitChanges();
        }
    }
    public void CancelChanges()
    {
        if (Changes != null)
            Changes.Clear();
    }
}

以下是其中一项属性的示例:

#region assetTag


    String _AssetTag;
    public const String STR_assetTag = "assetTag";
    public String assetTag
    {
        get { return (Changes.ContainsKey(STR_assetTag) ? _AssetTag : Asset.assetTag); }
        set
        {
            SetBufferedProperty<String>(STR_assetTag
                , () => Asset.assetTag = value, Asset.assetTag == value, () => _AssetTag = value);
        }
    }
    #endregion

I have rolled my own buffering class, that's a little messy but works for sure saving one record at a time.

This is the parent class for my business objects that wrap around a linq entity.

public class BufferedLinqChange
{
    LqGpsDataContext _dataContext;

    internal BufferedLinqChange(LqGpsDataContext dataContext)
    {
        _dataContext = dataContext;
    }

    protected void SetBufferedProperty<T>(string key,Action linqAction
        ,bool linqEqualsValue,Action bufferAction)
    {
        if (linqEqualsValue)
        {
            if (Changes.ContainsKey(key))
                Changes.Remove(key);
        }
        else
        Changes.InsertOrUpdate(key, linqAction); bufferAction();
    }

    protected Dictionary<String, Action> Changes = new Dictionary<string, Action>();

    public int ChangeCount { get { return Changes != null ? Changes.Count : 0; } }
    public bool hasChanges { get { return Changes != null ? Changes.Count > 0 : false; } }

    public void SubmitChanges()
    {
        _dataContext.SubmitChanges();
        if (ChangeCount > 0)
        {
            Changes.ForEach((item) => item.Value.Invoke());
            _dataContext.SubmitChanges();
        }
    }
    public void CancelChanges()
    {
        if (Changes != null)
            Changes.Clear();
    }
}

Here's a sample of one of the properties:

#region assetTag


    String _AssetTag;
    public const String STR_assetTag = "assetTag";
    public String assetTag
    {
        get { return (Changes.ContainsKey(STR_assetTag) ? _AssetTag : Asset.assetTag); }
        set
        {
            SetBufferedProperty<String>(STR_assetTag
                , () => Asset.assetTag = value, Asset.assetTag == value, () => _AssetTag = value);
        }
    }
    #endregion
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文