Suppose I have a method
public Patient(int id)
{
----
}
that returns Patient object given an id.. I could define contract in 2 ways
Which contract should I use? Any other suggestions?
Update: Please comment on this case too... If it is not an database assigned Id and it is something a user enter in UI.. like SSN .. then which one is better..
Comment about Null pattern from Steve that I think is valid: probably not a good idea here, as it would be really useful to know immediately when an ID did not exist.
And I also think Null pattern here would be somewhat heavy weight
Comment from Rob Wells on throwing exception because its bad Id: i don't think a typo in a patient's name is an exceptional circumstance" IMHO
Fixed Price Contracts. This is the best contract type when someone knows exactly what the scope of work is. Also known as a lump sum contract, this contract is the best way to keep costs low when you can predict the scope.
A time and materials contract is great for buyers who don't necessarily know what they want when they begin their project. Sellers use time and materials contracts when it's difficult to determine the amount of time they need to spend on the project and the types of materials required to complete the project.
DbC assists engineers clarify whether a caller or a callee should take charge of fixing a defect. DbC declares what should be met by a client (caller) in pre-conditions, what should be met by a supplier (callee) in post-conditions and what is forever true in all conditions.
Keep in mind that going "over the wire" to another tier (whether a database or an application server) is one of the most expensive activities you can do - typically a network call will take several orders of magnitude longer than in-memory calls.
It's therefore worth while structuring your API to avoid redundant calls.
Consider, if your API is like this:
// Check to see if a given patient exists
public bool PatientExists(int id);
// Load the specified patient; throws exception if not found
public Patient GetPatient(int id);
Then you are likely to hit the database twice - or to be reliant on good caching to avoid this.
Another consideration is this: In some places your code may have a "known-good" id, in other places not. Each location requires a different policy on whether an exception should be thrown.
Here's a pattern that I've used to good effect in the past - have two methods:
// Load the specified patient; throws exception if not found
public Patient GetExistingPatient(int id);
// Search for the specified patient; returns null if not found
public Patient FindPatient(int id);
Clearly, GetExistingPatient() can be built by calling FindPatient().
This allows your calling code to get the appropriate behaviour, throwing an exception if something has gone wrong, and avoiding exception handling in cases where it is not needed.
Another option would be the Null Object pattern.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With