English 中文(简体)
能够处理错误的部件的例外情况或返回情况
原标题:Exception or Return Status for Component that can Process with Errors

Im目前正在设计一个处理不同类型档案的处理系统。 我确定了以下接口:

public interface IFileProcessor
{
    bool ProcessFile(string fileContents)
}

目的是建立若干具体执行机制,处理不同类型的档案。 管制人员班负责:

  • Watching a folder for a newly added file
  • Reading the file contents
  • Obtaining a collection of these IFileProcessor concrete implementations
  • One by one call the ProcessFile() on each component, passing the file contents
  • If a component cannot process the file it returns false, otherwise it will process the contents and return true
  • If no IFileProcessor implementation can process the file it is moved by the controller to a UnProcessed folder
  • If a component successfully processes the file it is moved to the Processed folder
  • If an exception is thrown by a component it is moved to a Failed folder

我正在创立一个化粪池,它将首先检查它是否能够根据类型(即csv)处理档案,然后进行一些最高一级的验证(即核对档案负责人)。 如果这些检查没有例外,就会把整个档案视为无效。

然而,一旦最高一级验证成功,该构成部分将处理档案中的每一行。 从此以后,单个项目无法进行处理(即验证)是可以接受的,其余工作将继续进行。

This is where the problem lies, I am wondering if its best to log that validation errors have occurred and then throw an exception at the end of the process, or to change the ProcessFile() signature to return an enum (one of Processed, UnProcessed, ProcessedWithErrors)?

From what I have read it seems that exceptions are the preferred route over status codes, however in this particular circumstance where a process can continue it seems wrong to use an artificial exception at the end to state the process did not complete 100%.

我真正关心各国人民对此的想法。

问题回答

一项建议:

它不归还ool或 en,而是归还一个叫随器可以检查的物体。 也许把它称为“档案”。 在标的中,你可以储存各种信息,如整体成功或失败、验证状况、处理状况等。

In cases of severe failure, I would return an exception back, so correct action could take place by the caller. You could derive an exception class so that the try catch is clean.

例:

try
{
    FileProcessorResult fpr = ProcessFile(Contents);
    //Do something with fpr

}
catch (FileProcessorException fpe)
{
   //Something unexpected occured during file processing, handle it
}

我可能无法给出绝对正确的答案,因此,请将此视为建议。

我不认为这是一个绝对问题,因为我看不到这一点。

不过,我同意你的意见,但似乎不愿意在职能结束时提出一个例外,以表示部分处理,我也赞同返回法,并在职能完全失败时提出例外(即没有从档案中读过)。

其中一个很好的理由是,这种例外情况可能无法在电话职能中处理,如果档案得到部分处理,你可能不想执行?

无论你选择哪一条道路,你都需要详细记录所投掷的返回代码/例外,其他人将感谢你这样做。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签