What is the most suitable construct for an object whose database key consists of several columns?

Suppose I have a table in my database consisting of the following columns, 3 of which uniquely identify a row:

CREATE TABLE [dbo].[Lines]
(
    [Attr1] [nvarchar](10) NOT NULL,
    [Attr2] [nvarchar](10) NOT NULL,
    [Attr3] [nvarchar](10) NOT NULL,
    PRIMARY KEY (Attr1, Attr2, Attr3)
)

Now I have an object in my application that represents one of these lines. It contains three properties that correspond to the three Attr columns in the database.

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;}
}

The application has a second object that stores a collection of these line objects.

:. ( )? , , ? ... , - :

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

public UpdateLine(Line line)
{
     // update the line in the collection
}

, , , , , , , .

?

+3
6

, , , - , API . , , .

, , . , , , Line , , Collection.GetLine(attr1, attr2, attr3).UpdateX(newX) ..

, , Collection.UpdateX(line, newX) (, "line" arg "attr1, attr2, attr2').

-, , ( IMHO), API , , .

+3

, . .

, . , , (.. , ).

+5

, " , " - -. , , (- ), .

OO: Line, GetLine, .

, , , , . , . , , ?

, , ( "originalAttr1", "originalAttr2", "originalAttr3" ). , ( "attr1", "attr2", "attr3" ), , , . Getters Setters attr. "" ( , ), originalAttrX ( ..).

, , , ! , () - .

: , .

+2

( )

"" , , , :

public Line this[string attr1, string attr2, string attr3] {
   get { 
      // code to find the appropriate line...
   }
}

- " " , IMO, .

+1

, , . . , , int , , .

- , . , , , .

0

Line, string, , getter. , (, ), Line.

, gory , , , Line.

0

Source: https://habr.com/ru/post/1718571/


All Articles