Initialize object from SqlDataReader

As of now, we have two different styles of object intializations based on SQL row in our codebase:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Config(SqlDataReader sdr) {
for (int i = 0; i < sdr.FieldCount; i++) if (!sdr.IsDBNull(i)) {
switch (sdr.GetName(i)) {
case 'Id': Id = sdr.GetInt32(i); break;
...
}
}
}
</code>
<code>public Config(SqlDataReader sdr) { for (int i = 0; i < sdr.FieldCount; i++) if (!sdr.IsDBNull(i)) { switch (sdr.GetName(i)) { case 'Id': Id = sdr.GetInt32(i); break; ... } } } </code>
public Config(SqlDataReader sdr) {
    for (int i = 0; i < sdr.FieldCount; i++) if (!sdr.IsDBNull(i)) {
        switch (sdr.GetName(i)) {
            case 'Id': Id = sdr.GetInt32(i); break;
            ...
        }
    }
}

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public User(SqlDataReader sdr)
{
UserId = sdr.GetInt32(0);
if (!sdr.IsDBNull(1)) Name = sdr.GetString(1);
...
}
</code>
<code>public User(SqlDataReader sdr) { UserId = sdr.GetInt32(0); if (!sdr.IsDBNull(1)) Name = sdr.GetString(1); ... } </code>
public User(SqlDataReader sdr)
{
    UserId = sdr.GetInt32(0);
    if (!sdr.IsDBNull(1)) Name = sdr.GetString(1);
    ...
}

They are called like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>while(sdr.Read()) users.Add(new User(sdr));
</code>
<code>while(sdr.Read()) users.Add(new User(sdr)); </code>
while(sdr.Read()) users.Add(new User(sdr));

C# coding guide says that style should be consistent, so one of them has to change to the other, if we don’t find a reason to keep them different. So, before we implement the whole project with its 50+ models, we need a design guideline regarding this.

Both designs have their drawbacks, but I don’t know how to make it better:

  • In the first, no one will notice until later in the code if required fields are dropped from the query.
  • The second one will fail whenever the fields in the query are reordered.

Which one should we prefer, or how could we improve them to address the drawbacks? Are they really drawbacks, in terms of code maintenance/reuse?

1

Both options have drawbacks: the first depends on the “magic string” of the column name, the second depends on the “magic number” of the column index. A good suite of automated unittests could cover either usage and help a great deal in preventing breaking changes though…

I would personally favor the first for a number of reasons, but they are probably very personal. (Actually, I would prefer NOT to write this kind of code and use an OR/M but that does not make this question any less valid)

Searchable field names

Field names can be found using Find in Files. ‘Id’ is obviously very generic, but I imagine if you want to remove a column called ‘OrderPaymentTerm’ you could easily look for the usages in the code. Or even better, put the field names in constants that are part of the class that creates the query. That way, field names and usages are right beside each other.

Required field validation is easy

If you want to make sure all the fields are present in the query, simply keep a list of fields that you read from the SqlDataReader and throw an exception if a field is missing.

Column order should not matter

Nobody expects a change in the ordering of columns in a SELECT clause to be a breaking change. And if you use SELECT * FROM, you can’t control the ordering of the columns anyway.

Removing a column is less painful

If you remove the sixth column in a ten-column SELECT, you have to update all the indexes in the second case. Painful, and prone to error.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật