I'm writing an auditing service for some business-critical operations. The service is being implemented using the IoC pattern:
public interface IAuditWriter
{
void WriteAction(int key, string value);
}
Because of this, I need it to raise exceptions which are not specific to the implementation.
Part of the information in the audit process includes a key which is intended to be unique. It is a current requirement of the service that it provides a check for key uniqueness as part of its auditing process. Duplicate keys are a violation of a process requirement.
Currently, the service is to be implemented as a write to a SQL-Server. Although unlikely, it's possible the key might be a duplicate, in which case a SqlException
will be thrown complaining about a primary key constraint violation. I would rather wrap this exception in a more generic "duplicate key" exception which can be caught and then allow the process to generate a new key.
Normally, I hate to create a new exception class; there's almost always a suitable type available that can be used to convey the same information. I have caught the System.Data.Linq.DuplicateKeyException
in the past, which looked like a good candidate to throw here, except that it comes from a LINQ-related namespace, and my interface has nothing to do with LINQ.
My immediate options seem to be:
- Throw
System.Data.Linq.DuplicateKeyException
anyway and hope nobody reads into the namespace too much. - Throw
System.InvalidOperationException
and cross my fingers I never need an implementation that can throw this exception for other reasons. - Throw my own custom
DuplicateKeyException
. - Create a separate method in the interface to check for key uniqueness and call this before I write the key and value.
What are your opinions on this?