代码之家  ›  专栏  ›  技术社区  ›  Tj Kellie

对象删除或保存要使用哪种类型的方法?

  •  3
  • Tj Kellie  · 技术社区  · 16 年前

    我见过很多对象返回一个int或其他返回值,“专家”对此有什么看法?

    i、 e.这:

    public void Delete()
    {
       if (this.objectID == null || this.objectID == -1) { throw new exNotDbObject() }
       //Do dB operations
       this.objectID = null;
       this.TimeRetrievedFromDb = null;
    
    }
    

    与此相比:

    public int Delete()
    {
       if (this.objectID == null || this.objectID == -1) { throw new exNotDbObject() }
       int retVal = dataLayer.DeleteObj(this.objectID);
       return retVal;
    }
    
    1 回复  |  直到 16 年前
        1
  •  1
  •   kemiller2002    16 年前

    int Delete() 我自己然后我就可以知道删除了多少条记录(如果有的话),除非出现问题,否则程序可以按预期继续。

    void Delete() 如果没有删除任何记录(或多个记录),则抛出一个异常,但我从不喜欢这种方法,因为它总是假设我知道删除的预期结果。