Here's an example of using an `IErrorReporter` in a real-world application, focusing on a document processing system similar to the one described in the DotNetCurry article[1].
Overview of the Application
The application is designed to process documents by extracting words and storing them in a database. It includes features like retrying failed operations and logging errors using an `IErrorReporter`.
Components of the Application
1. Document Processing Service: This service is responsible for extracting words from documents and storing them in a database. It uses an `IDocumentWithExtractedWordsStore` interface to abstract the storage mechanism.
2. Error Handling Mechanism: To handle errors during document processing, an `IErrorReporter` interface is used. This allows for flexible error reporting mechanisms, such as logging to an event log or sending notifications.
3. Retry Mechanism: A retry mechanism is implemented using a decorator pattern. If a document storage operation fails, it is retried a specified number of times before reporting the error.
Implementation Details
IErrorReporter Interface
csharp
public interface IErrorReporter
{
void ReportError(string message, Exception exception);
}
ErrorAwareDocumentWithExtractedWordsStore Class
This class implements the `IDocumentWithExtractedWordsStore` interface and uses an `IErrorReporter` to report errors.
csharp
public class ErrorAwareDocumentWithExtractedWordsStore : IDocumentWithExtractedWordsStore
{
private readonly IDocumentWithExtractedWordsStore _store;
private readonly IErrorReporter _errorReporter;
public ErrorAwareDocumentWithExtractedWordsStore(IDocumentWithExtractedWordsStore store, IErrorReporter errorReporter)
{
_store = store;
_errorReporter = errorReporter;
}
public void StoreDocument(Document document)
{
try
{
_store.StoreDocument(document);
}
catch (Exception ex)
{
_errorReporter.ReportError("Failed to store document", ex);
throw; // Re-throw to allow further handling if needed
}
}
}
EventLogErrorReporter Class
This class implements the `IErrorReporter` interface by logging errors to an event log.
csharp
public class EventLogErrorReporter : IErrorReporter
{
public void ReportError(string message, Exception exception)
{
// Create an event log source if it doesn't exist
if (!EventLog.SourceExists("DocumentIndexer"))
{
EventLog.CreateEventSource("DocumentIndexer", "Application");
}
// Log the error
EventLog.WriteEntry("DocumentIndexer", $"{message}: {exception.Message}", EventLogEntryType.Error);
}
}
Usage Example
To use these components in a real-world application, you would compose them as follows:
csharp
// Create an instance of the document store
var documentStore = new DatabaseDocumentStore();
// Create an error reporter
var errorReporter = new EventLogErrorReporter();
// Create an error-aware document store
var errorAwareStore = new ErrorAwareDocumentWithExtractedWordsStore(documentStore, errorReporter);
// Use the error-aware store to process documents
var documentProcessor = new DocumentProcessor(errorAwareStore);
documentProcessor.ProcessDocuments();
In this example, any errors that occur during document processing will be logged to the event log using the `EventLogErrorReporter`. This demonstrates how the `IErrorReporter` interface can be used to decouple error reporting from the core logic of the application, allowing for different reporting mechanisms to be easily swapped in or out.
Citations:[1] https://www.dotnetcurry.com/csharp/1295/object-composition-with-solid
[2] https://www.hendrik-erz.de/post/understanding-files-and-folders-a-primer
[3] https://evilmartians.com/chronicles/spa-hexagon-robust-app-architecture-for-mobile-and-web
[4] https://literature.rockwellautomation.com/idc/groups/literature/documents/gr/9306-gr000_-en-p.pdf
[5] https://www.linkedin.com/pulse/exception-handling-real-life-applications-jalal-kiswani
[6] https://downloads.gonitro.com/pro11/en/nitro-pro-11-user-guide-en.pdf
[7] https://github.com/Bikeman868/Prius
[8] https://commons.hostos.cuny.edu/edtech/basic-computer-skills/working-with-files-and-folders/