Sample (this isn't exactly what I have in real life, but it's the same
concept - so forgive me if the logic doesn't sound right) - sorry if
this gets a little long-winded:
Suppose I have a class called Account that represents a record in our
Account database table - the primary key is AccountId, and there are
50 accounts - these accounts do not change. There's a table named
Vehicle, and a corresponding Vehicle class, with an AccountId column/
property. There's a one-to-many relationship, so each account can
have many vehicles, and that number can change daily.
Suppose I have a table called Transaction - each transaction belongs
to a particular account (so it has an AccountId foreign key column).
I have a daily process that runs through all of the new transactions,
and needs to know information about the corresponding account, and
also the count of vehicles belonging to that account. During this
particular process, it is safe to assume that the vehicle count per
account will not change (it only changes once per day, and not at the
same time as the transaction processing). So I have to loop through
each transaction, look up the corresponding account, and get the count
of vehicles. Here are some possibilities.
1) For each transaction, make a database call to the Account table to
look up the information, and a second call to the Vehicle table to
retrieve a count of vehicles per account.
2) For each transaction, make a single database call to the Account
table with a join to the Vehicle table to retrieve the count at the
same time, and store the results in a custom object.
3) Actually make a column on the Account table with the count, and use
triggers or other techniques to keep this column updated.
4) Make two Dictionaries - one for <AccountId, Account> and one for
<AccountId, VehicleCount> - pre-populate these two dictionaries and
use both of them when I need the information for each transaction.
5) Make a custom AccountContainer that contains an Account object and
an integer representing the count, then pre-populate a Dictionary of
<AccountId, AccountContainer>.
6) Make a custom AccountEx object that derives from Account, and
contains the integer representing the count, then pre-populate a
Dictionary of <AccountId, AccountEx>.
7) Make a Trictionary of <AccountId, Account, VehicleCount>.
My goal is to make the process as fast as possible, so I don't want to
make database calls with every transaction, especially not to get the
count of the Vehicle table - that table is so large that a count by Id
takes a long time (nearly a second). I would prefer not to even call
the Account table with each transaction, since even though it's a
small table, it's still faster to query the in-memory dictionary (the
result-set of accounts is so small that it's no big deal to store in
memory. And I really don't want to add the "count" column to the
account table - I prefer not to have data about data inside real data.
Any of these solutions will work - #5 or #6 would be the best
performance, but that means I'd need to define a new Type each time I
wanted to use this technique. The Trictionary's goal would be to
remove this need by having that generic type under the sheets,
allowing me to focus on the work. A code sample:
Trictionary<int, Account, int> myTrictionary = new Trictionary<int,
Account, int>();
/* This would do the work one time of retrieving both the full
accounts and the vehicle counts from the database. It's ok if this
takes a long time - a one-time load at the beginning of the process is
no big deal, as long as each individual transaction doesn't take a
long time - there are only 50 accounts, but thousands of transactions
daily. */
foreach (Account account in GetAccounts())
{
myTrictionary.Set(account.AccountId, account, GetVehicleCount
(account.AccountId));
}
foreach (Transaction t in GetNewTransactions())
{
int accountId = t.AccountId;
Account account;
int vehicleCount;
myTrictionary.Get(accountId, out account, out vehicleCount);
// I now have the account and vehicle count for this particular
transaction, retrieved from memory.
}
This is only one specific usage - I'm sure there are plenty of other
situations where returning two objects from a Dictionary can be
useful.
Thanks
Joe
On Dec 18, 12:17 am, "Charles A. Lopez" <
charlesalo...@gmail.com>
wrote:
>
charlesalo...@gmail.com