Should I use a DuplicateKeyException in my own non-LINQ code?

I am writing an audit service for some critical operations. The service runs using the IoC pattern:

public interface IAuditWriter
{
    void WriteAction(int key, string value);
}

Because of this, I need him to make exceptions that are not implementation related.

Part of the information in the audit process includes a key that must be unique. This is a current service requirement that provides key uniqueness verification as part of the audit process. Duplicate keys violate process requirements.

Currently, the service should be implemented as a record on the SQL server. Although it is unlikely, perhaps the key may be a duplicate, in which case a SqlExceptionwill complain about a violation of the primary key constraint. I would rather wrap this exception in the more general "duplicate key" exception, which you can catch and then allow the process to generate a new key.

I usually hate creating a new exception class; almost always there is a suitable type that can be used to convey the same information. I have caught System.Data.Linq.DuplicateKeyExceptionin the past that looked like a good candidate to drop out here, except that it comes from the LINQ-related namespace and my interface has nothing to do with LINQ.

My closest options are as follows:

  • System.Data.Linq.DuplicateKeyException , .
  • Throw System.InvalidOperationException , .
  • DuplicateKeyException.
  • , .

?

+3
1

, .NET, , . LINQ , .

InvalidOperationException , , . , .

DuplicateKeyException . , , - . , , .

"IsUnique" , "IsUnique" WriteAction , , - ? , , ( ). , WriteAction , , "IsUnique" .

+5

Source: https://habr.com/ru/post/1716186/


All Articles