对于数据库键由多列组成的对象,最合适的设计是什么?

The*_*att 3 .net c# oop design-patterns

假设我的数据库中有一个由以下列组成的表,其中3个唯一标识该行:

CREATE TABLE [dbo].[Lines]
(
    [Attr1] [nvarchar](10) NOT NULL,
    [Attr2] [nvarchar](10) NOT NULL,
    [Attr3] [nvarchar](10) NOT NULL,
    PRIMARY KEY (Attr1, Attr2, Attr3)
)
Run Code Online (Sandbox Code Playgroud)

现在,我的应用程序中有一个对象代表其中一行.它上面有三个属性,对应于数据库中的三个Attr列.

public class Line
{
   public Line(string attr1, string attr2, string attr3) 
   {
        this.Attr1 = attr1;
        this.Attr2 = attr2;
        this.Attr3 = attr3;
   }

   public Attr1 {get; private set;}
   public Attr2 {get; private set;}
   public Attr3 {get; private set;}
}
Run Code Online (Sandbox Code Playgroud)

应用程序中有第二个对象存储这些行对象的集合.

这是一个问题:在参考此集合中的单个行时(从调用者的角度来看),最合适的设计是什么?调用者是否应该负责跟踪他正在更改的行的索引,然后只使用该索引直接修改集合中的行?或者......如果对象上有方法可以说明以下内容:

public GetLine(string attr1, string attr2, string attr3)
{
     // return the line from the collection
}

public UpdateLine(Line line)
{
     // update the line in the collection
}
Run Code Online (Sandbox Code Playgroud)

我们正在讨论我们的团队,因为我们中的一些人认为使用集合中的内部索引来引用一行是更有意义的,而其他人认为当我们已经唯一的时候没有必要引入另一个内部密钥根据三个属性识别一条线.

思考?

Eri*_* J. 5

您的对象模型应该设计为对对象使用者有意义.它不应该在最大程度上与数据模型联系在一起.

听起来对象消费者更直观地考虑三个属性.如果没有相反的性能问题,我会让对象使用者使用这些属性而不关心数据存储的内部工作(即不要求他们知道或关心内部索引).