What Pattern will solve this – fetching dependent record from database

I have these classes

class Match
{
  int MatchID,
  int Team1ID, //used to reference Team
  int Team2ID,
  ... other fields
}

Note: Match actually have 2 teams which means 2 TeamID

class Team
{
   int TeamID,
   string TeamName
}

In my view I need to display List<Match> showing the TeamName. So I added another field

class Match
{
  int MatchID,
  int TeamID, //used to reference Team
  ... other fields

  string TeamName;
}

I can now do

Match m = getMatch(id);
m.TeamName = getTeamName(m.TeamId); //get name from database

But for a List<Match>, getTeamName(TeamId) will go to the database to fetch TeamName for each TeamID.

For a page of 10 Matches per page, that could be (10x2Teams)=20 trip to database. Example:

for(1..10)
{
   Match m[i] = getMatch(i);

   //the team are never going to be repeated, 20 teams spread over ten matches 
   m[i].Team1Name = getTeamName(m.Team2Id);
   m[i].Team2Name = getTeamName(m.Team2Id);
}

To avoid this, I had the idea of loading everything once, store it in memory and only lookup the TeamName in memory. This made me have a rethink that what if the records are 5000 or more.

What pattern is used to solve this and how?

First off… this sounds like you are trying to solve a problem you dont know you’ll have. Unless you know for a fact that hitting the database 20 times per page (which is nothing) will be a problem, stick with what you have for now and address it when it actually becomes an issue.

That said… a couple of approaches:

In SQL, you can do something like this:

select name from teams where id in (10, 15,....)

Even if you do it once per match, thats 10 trips to the db, not 20. That just cut it in half.

Alternatively – and this is where abstractions can bite you – rethink what you are doing. Rather than loading objects and querying their team names, make a single query that generates all/most of the information you’d need to list everything on the page you are displaying (at least as far as the match information is concerned). With some nifty SQL, you can probably reduce the whole thing down to a single query that returns 10 rows (one per match).

3

There are several ways to optimize for what you are doing:

  • Query for many, don’t do a select * from teams where id = ?, do a select * from teams where id in (?). Your default method’s implementation receives a List and does the later, the overloaded method for a single id turns the id into a List of size 1 and calls the method that takes the List as parameter. From the way I see it, you are doing the inverse.
  • You might never need ALL of it, you said it yourself, you use pagination, so the max possible is defined by whatever max page size you have.
  • But… if you need to request for that many consider taking a look a the Flyweight pattern, where you can store the data in a random data structure of your choosing, and use a single object to access the data with the same interface as you would with N objects.
  • Use a model, remember that Model in MVC (if you are using MVC at all) doesn’t stand for database, but just data. A model can act as a facade for your most complex queries and avoid you unnecessary pain in the Controllers (see “fat model skinny controller” principle)

1

I think your problem is not to be solved by a specific pattern, but by the the software design itself.

There are many things that can be done, if your are connecting directly to a relational database, you could fetch the data in a single query, using joins, and in code bind the data where you want. You could also do it in two trips, and look up the objects already loaded in memory. Or also use something like NHibernate, it all depends of your software design and architecture.

About the many records problem, where are you to use these data? It could probably be paged or filtered, and you could load it in chunks from the data store.

In situations like this you should look overall your project and see how situations like this were handled, and try to follow the same pattern, hence improving code maintainability

1

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