发送 HTML 参数和文件路径参数?

发布于 2024-10-17 00:35:09 字数 720 浏览 2 评论 0原文

我正在创建一个打印机类,它需要打印 HTML 字符串和 HTML 文档。所以基本上它可以得到:

Printer.Print("<b>Hello world</b>");

因此,

Printer.Print(@"C:\hello.html");

在设计我的类时,我在以下之间做出决定:

public static void Print(string inputString, string mode){
    if(mode=="htmlString"){//Print the string itself}
    else if(mode=="htmlFile"){//Print the document in the filepath}
}

或者

public static void Print(string inputString){
    if(file.Exists(inputString)){//Print the document in the filepath}
    else{//Print the string itself}
}

一般来说,哪个是更好的实践?第一个选项需要另一个参数,这不是很好,但是如果我们使用第二个选项,如果我们打算实际打印一个文件但使用不正确的文件名,它将打印错误的内容。

I'm creating a printer class that will need to print both HTML strings and HTML documents. So basically it can get:

Printer.Print("<b>Hello world</b>");

And

Printer.Print(@"C:\hello.html");

So in designing my class the Print method definition I'm deciding between the following:

public static void Print(string inputString, string mode){
    if(mode=="htmlString"){//Print the string itself}
    else if(mode=="htmlFile"){//Print the document in the filepath}
}

Or

public static void Print(string inputString){
    if(file.Exists(inputString)){//Print the document in the filepath}
    else{//Print the string itself}
}

In general, which is the better practice? The first option requires another argument which is not great, but then if we use the second option, if we intend to actually print a file but use an incorrect file name, it will print the wrong thing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身狗的梦 2024-10-24 00:35:09

很多时候,意外事件的空间太大,特别是在这种情况下,您必须根据输入确定如何采取行动,然后进一步进行验证处理(即File.Exists),这是为误报而大声疾呼。在我看来,做这样的事情:

public static void PrintString(string input)
{
    //print the string, knowing precisely this is the intent,
    //and if not, it's what you're going to do anyway!
}

public static void PrintFile(string fileName)
{
    //no qualms here, you're going to print a file
}

A lot of times there is just too much room for contingencies, specifically in this case where you have to determine how to act based on the input, then further do validation processing (i.e. File.Exists), it's crying out for false positives. In my opinion do something like this instead:

public static void PrintString(string input)
{
    //print the string, knowing precisely this is the intent,
    //and if not, it's what you're going to do anyway!
}

public static void PrintFile(string fileName)
{
    //no qualms here, you're going to print a file
}
≈。彩虹 2024-10-24 00:35:09

我建议您采用失望先生建议的设计。

然而,如果出于某种原因你想保留原来的想法,我会做一些小小的改变。不是将模式作为字符串传递,而是将其作为枚举传递。事实上,你也可以将失望先生的建议转入其中。例如,

public enum PrintMode
{
  File,
  Raw
}

public static void Print(string printData, PrintMode mode)
{
  if(mode == PrintMode.Raw)
  {
    //Print the string itself
  }
  else if (mode == PrintMode.File)
  {
    //Print the document in the filepath
  }
  else
  {
    throw new ArgumentException("Invalid print mode specified");
  }
}

public static void PrintString(string input)
{
  Print(input, PrintMode.Raw);
}

public static void PrintFile(string input)
{
  Print(input, PrintMode.File);
}

您的第二个想法是一个坏主意,因为每当用户打印原始字符串时,您都会执行不必​​要的文件系统检查。更重要的是,它可能会抛出异常,因为在打印原始字符串时,这不是有效的文件路径。因此,Exists 检查可能会失败。

I would suggest that you go with the design that Mr. Disappointment suggested.

However, if for whatever reason you want to keep the original idea I would make a slight alteration. Rather than pass mode as a string pass it as an enum instead. In fact you could wire the suggestion from Mr. Disappointment into this aswell. For example

public enum PrintMode
{
  File,
  Raw
}

public static void Print(string printData, PrintMode mode)
{
  if(mode == PrintMode.Raw)
  {
    //Print the string itself
  }
  else if (mode == PrintMode.File)
  {
    //Print the document in the filepath
  }
  else
  {
    throw new ArgumentException("Invalid print mode specified");
  }
}

public static void PrintString(string input)
{
  Print(input, PrintMode.Raw);
}

public static void PrintFile(string input)
{
  Print(input, PrintMode.File);
}

Your second idea is a bad idea as you would be performing unnecessary filesystem checks whenever a user is printing a raw string. More importantly it would probably throw an exception as, when printing a raw string, this will not be a valid file path. So the Exists check will probably blow up.

薔薇婲 2024-10-24 00:35:09

我同意使用两种方法是最好的方法。但是,.Net 约定将具有以下方法名称:

public static void Print(string path) { ... }
public static void PrintHtml(string html) { ... }

I agree that using two methods is the best approach. However, the .Net conventions would have the following method names:

public static void Print(string path) { ... }
public static void PrintHtml(string html) { ... }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文