Bob in his book, as an example, implements the Payroll application. The architecture is built on transaction scripts — for every user action there is a transaction class that works with the database. But here's what I can not understand, for example, take a piece of the application, where for the employee "HorlyClassification" add "TimeCard".

public class HourlyClassification : PaymentClassification { private double hourlyRate; private Hashtable timeCards = new Hashtable(); public HourlyClassification(double rate) { this.hourlyRate = rate; } public double HourlyRate { get { return hourlyRate; } } public TimeCard GetTimeCard(DateTime date) { return timeCards[date] as TimeCard; } public void AddTimeCard(TimeCard card) { timeCards[card.Date] = card; } } 

Here the AddTimeCard (Timecard) method is of interest, because it introduces the next card into the collection, which is stored in memory, and not in the database. A transaction promising to add a TimeCard to the database looks like this:

 public override void Execute() { Employee e = database.GetEmployee(empId); if (e != null) { HourlyClassification hc = e.Classification as HourlyClassification; if (hc != null) hc.AddTimeCard(new TimeCard(date, hours)); } } 

Question: When is the information on the TimeCard card entered into the database and how?

  • And what is the question then? - teo van kot
  • I'm trying to understand - that the card is stored in memory, and not in the database - this is a flaw in the sample code, or I do not understand at what point the saving is in the database. - Alksar

1 answer 1

Hashtable is an obsolete class. It is similar to a Dictionary, but there is a nuance - Hashtable does not throw an exception if there is no value, but returns null. Therefore, it simply takes the item and returns it without checking if it was there. And in the version with base checks for null are placed.

But in general, you did not ask a question ...

I'm trying to understand - that the card is stored in memory, and not in the database - this is a flaw in the sample code, or I do not understand at what point the saving is in the database.

So, I did not guess the question ...

There somewhere further there is an option with a base. If I am not mistaken, he clearly said that he wants to postpone the method of saving to the very end of the development, as an implementation detail.

  • He postponed the method of storing data using the PayrollDatabase interface, which contains the "API" for working with the database - such methods as "AddEmployee, LoadEmployee ...", and uses the InMemoryDatabase replacement class: PayrollDatabase, which also uses Hashtable for storage. But I wrote transactions in the final form, in the code I quoted, it jerks the class method, not the API - Alksar
  • Uses for testing? Tests should not change the database. - Qwertiy
  • How then will this piece of code look like with the addition of a card? Those. it is also tedious to add a card to the Hashtable in the HorlyClassification class, because using the cards contained there, the class calculates the salary, but somewhere you need to save the card straight to the database. - Alksar
  • one
    I don’t have a book right now, I’m speaking from memory ... It seems that the storage in memory and in the database have the same interface, and somewhere you can switch from one to the other. - Qwertiy pm
  • Yes, you remember very well. But the question is this: if I understood correctly, then the whole point of such an architecture is that only one class knows about the database, the one that implements the interface with the API "PayrollDatabase". The methods of this interface are called only from the "Transaction" entities. Right? So, and the card, in the subject code, adds the line "hc.AddTimeCard (new TimeCard (date, hours));", where hc is part of the Employee class. Now the question arises - since the information on the card should be reflected in two places at once (1-in DB, 2- in the class Employee, since it counts the salary on them) - Alksar