有没有闲着的 来个人帮忙重构下

SomethingJack 2012-09-24 05:42:54


public string file;//显示生成完成的swf文件
protected void Button1_Click(object sender, EventArgs e)
{
string UploadFilePath = Server.MapPath(this.HiddenField1.Value);//保存上传的PDF或者其他可以打印的文件(DOC,DOCX等) /UploadFile/系统分析师通过的经验.doc
string NewUploadFilePath = string.Empty;//转换WPS文件路径
string ext = Path.GetExtension(UploadFilePath);
if ((".wps,.et,.pps,.dps,.pps").Contains(ext))
{
switch (ext)
{
case ".wps"://wps文件
NewUploadFilePath = Path.ChangeExtension(UploadFilePath, ".doc");
File.Move(UploadFilePath, NewUploadFilePath);
break;

case ".et"://wps的表格文件
NewUploadFilePath = Path.ChangeExtension(UploadFilePath, ".xls");
File.Move(UploadFilePath, NewUploadFilePath);
break;

case ".pps":
case ".dps":
NewUploadFilePath = Path.ChangeExtension(UploadFilePath, ".ppt");
File.Move(UploadFilePath, NewUploadFilePath);
break;
}
try
{
if (UploadFilePath != null)
{
string SwfFile = strType(UploadFilePath);//得到文件类型
if (SwfFile != null)
{
string file = NewUploadFilePath.Replace(SwfFile, ".swf");
if (!File.Exists(file))
{
bool isconvert = ConvertPdfToSwf(NewUploadFilePath, file.Replace("UploadFile", "SwfFolder"));//执行转换
if (isconvert == true)
{
file = Path.GetFileName(file);
string FlashView = "http://199.99.99.111:8011/FlashPrinter/Interface/FlashView.aspx";
Response.Write(" <script> window.location.href= '" + FlashView + "?dFile=" + HttpUtility.UrlEncode(file) + "'; </script> ");
}
}
}
}
}
catch (Exception)
{
throw;
}

}
else
{
try
{
if (UploadFilePath != null)
{
string SwfFile = strType(UploadFilePath);//转换后的文件后缀名
if (SwfFile != null)
{
string file = UploadFilePath.Replace(SwfFile, ".swf");
if (!File.Exists(file))
{
bool isconvert = ConvertPdfToSwf(UploadFilePath, file.Replace("UploadFile", "SwfFolder"));//执行转换
if (isconvert == true)
{
file = Path.GetFileName(file);
string FlashView = "http://199.99.99.111:8011/FlashPrinter/Interface/FlashView.aspx";
Response.Write(" <script> window.location.href= '" + FlashView + "?dFile=" + HttpUtility.UrlEncode(file) + "'; </script> ");
}
}
}
}
}
catch (Exception)
{
throw;
}

}
}

/// <summary>
/// 文档转swf
/// </summary>
/// <param name="inFilename">转换前的文件</param>
/// <param name="swfFilename">转换后的文件</param>
public bool ConvertPdfToSwf(string inFilename, string swfFilename)
{
bool isStart;
Process process = new Process();

string flashPrinter = Server.MapPath("~/FlashPrinter/FlashPrinter.exe");
FileInfo fileinfo = new FileInfo(inFilename);
ProcessStartInfo startInfo = new ProcessStartInfo(flashPrinter);
int time = ((int)(fileinfo.Length / (1024 * 1204))) * 20 + 20;
startInfo.Arguments = string.Concat(inFilename, " -o ", swfFilename);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.CreateNoWindow = true; // 是否创建显示窗口。
startInfo.RedirectStandardError = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
try
{
isStart = process.Start();
process.WaitForExit(time * 1000);
process.Close();
process.Dispose();

while (!File.Exists(swfFilename))
{
Thread.Sleep(1);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (process != null)
{
process.Close();
}
}
return isStart;
}

/// <summary>
/// 检查上传文件类型
/// </summary>
/// <param name="type">文件类型</param>
/// <returns></returns>
public string strType(string type)
{
type = type.ToLower();
type = type.Substring(type.LastIndexOf("."));
if ((".swf,.flv,.doc,.docx,.jpg,.xls,.xlsx,.txt,.ppt,.pptx,.pub").Contains(type))
{
return type;
}
else
{
switch (type)
{
//case ".txt"://txt文本文件
// type = ".rtf";
// break;
case ".wps"://wps文件
type = ".doc";
break;

case ".et"://wps的表格文件
type = ".xls";
break;

case ".pps":
case ".dps":
type = ".ppt";
break;
}
}
return type;
}
}
...全文
163 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
习惯就好 2012-10-11
  • 打赏
  • 举报
回复



无意间看关注了下小宝,发现很爱学习,而且很谦虚。 up
SomethingJack 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 的回复:]

涉及到简单架构上的重构,首先要求设计师懂得按照类型(或者说接口)来抽象。如果你滥用string等类型来作为控制机制,而不是使用面向领域实体类型的数据结构进行接口设计,那么再怎么“闲着”没事时搞“重构”,所写出的程序也跟原来是一个层次的啊。

因此这需要有点抽象、封装、扩展等概念,再说。
[/Quote]
谢谢!
SomethingJack 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

Insus.NET再重构了第一个方法,改成这样子:
C# code

public string file;//显示生成完成的swf文件
protected void Button1_Click(object sender, EventArgs e)
{
string UploadFilePath = Server.MapPath(this.HiddenFi……
[/Quote]
谢谢前辈指点
SomethingJack 2012-09-25
  • 打赏
  • 举报
回复
虚心接受以上所有前辈的指教 哈哈 。学习到了 谢谢各位!
因为关于这个代码 我自己可能思维局限了点 重构来重构去 还是觉得没重构到最简化
  • 打赏
  • 举报
回复
涉及到简单架构上的重构,首先要求设计师懂得按照类型(或者说接口)来抽象。如果你滥用string等类型来作为控制机制,而不是使用面向领域实体类型的数据结构进行接口设计,那么再怎么“闲着”没事时搞“重构”,所写出的程序也跟原来是一个层次的啊。

因此这需要有点抽象、封装、扩展等概念,再说。
SocketUpEx 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

以软件开发标准。一个函数不超过5行代码。估计是要重构的。
[/Quote]

老大,是50行吧
一个函数不超过5行
那得定义多少函数啊

SocketUpEx 2012-09-24
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

Insus.NET只重构了最后一个方法,不知是否有可参考的地方:
http://www.cnblogs.com/insus/archive/2012/09/24/2700710.html
[/Quote]

手贱了
点开了
点评了

1. strType方法名是很奇葩的
你重构后的方法依然很奇葩

2.你Dictionary代替了switch
Dictionary和switch本来就是两种不同功用的语法
想不明白你为什么会使用Dictionary

当然,注释是很重要的

//如果以后还在有类型需要判断,请加在这里。

完了,如果有1000个条件,你只能继续往Dictionary里塞数据了

而且,还把塞数据的操作放在函数里,明白我想说什么了吗?

最后,给你一点小建议:
可以使用TryGetValue代替ContainsKey
虽然小数据量看不出效果




SocketUpEx 2012-09-24
  • 打赏
  • 举报
回复
发现有switch语句,估计会有人建议套用一个设计模式
insus 2012-09-24
  • 打赏
  • 举报
回复
Insus.NET再重构了第一个方法,改成这样子:

public string file;//显示生成完成的swf文件
protected void Button1_Click(object sender, EventArgs e)
{
string UploadFilePath = Server.MapPath(this.HiddenField1.Value);//保存上传的PDF或者其他可以打印的文件(DOC,DOCX等) /UploadFile/系统分析师通过的经验.doc

string ext = Path.GetExtension(UploadFilePath);

if (ExtensionType().ContainsKey(ext))
{
MoveFile(ext, UploadFilePath);
}
else
{
ConvertFile(UploadFilePath);
}
}


详细参考:
http://www.cnblogs.com/insus/archive/2012/09/24/2700820.html
cykb518 2012-09-24
  • 打赏
  • 举报
回复
主要是改了下第一个方法,还有就是变量命名。
cykb518 2012-09-24
  • 打赏
  • 举报
回复

protected void Button1_Click(object sender, EventArgs e)
{
var uploadFilePath = Server.MapPath(this.HiddenField1.Value);//保存上传的PDF或者其他可以打印的文件(DOC,DOCX等) /UploadFile/系统分析师通过的经验.doc
var newuploadFilePath = string.Empty;//转换WPS文件路径
var ext = Path.GetExtension(newuploadFilePath);
if ((".wps,.et,.pps,.dps,.pps").Contains(ext))
{
newuploadFilePath = Path.ChangeExtension(uploadFilePath, StrType(ext));
File.Move(uploadFilePath, newuploadFilePath);
}
try
{
if (uploadFilePath != null)
{
var swfFile = StrType(uploadFilePath);//转换后的文件后缀名
if (swfFile != null)
{
var file = uploadFilePath.Replace(swfFile, ".swf");// seems have some issues
if (!File.Exists(file))
{
bool isconvert = ConvertPdfToSwf(uploadFilePath, file.Replace("UploadFile", "SwfFolder"));//执行转换
if (isconvert)
{
file = Path.GetFileName(file);
var flashView = "http://199.99.99.111:8011/FlashPrinter/Interface/FlashView.aspx";
Response.Write(" <script> window.location.href= '" + flashView + "?dFile=" + HttpUtility.UrlEncode(file) + "'; </script> ");
}
}
}
}
}
catch (Exception)
{
throw;
}
}

/// <summary>
/// 文档转swf
/// </summary>
/// <param name="inFilename">转换前的文件</param>
/// <param name="swfFilename">转换后的文件</param>
public bool ConvertPdfToSwf(string inFilename, string swfFilename)
{
bool isStart;
var process = new Process();

var flashPrinter = Server.MapPath("~/FlashPrinter/FlashPrinter.exe");
var fileinfo = new FileInfo(inFilename);
var startInfo = new ProcessStartInfo(flashPrinter);
var time = ((int)(fileinfo.Length / (1024 * 1204))) * 20 + 20;
startInfo.Arguments = string.Concat(inFilename, " -o ", swfFilename);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = false;
startInfo.RedirectStandardOutput = false;
startInfo.RedirectStandardError = false;
startInfo.CreateNoWindow = true; // 是否创建显示窗口。
startInfo.RedirectStandardError = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
process.StartInfo = startInfo;
try
{
isStart = process.Start();
process.WaitForExit(time * 1000);
while (!File.Exists(swfFilename))
{
Thread.Sleep(1);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
process.Close();
}
return isStart;
}

/// <summary>
/// 检查上传文件类型
/// </summary>
/// <param name="type">文件类型</param>
/// <returns></returns>
public string StrType(string type)
{
type = type.Substring(type.LastIndexOf(".")).ToLower();
if ((".swf,.flv,.doc,.docx,.jpg,.xls,.xlsx,.txt,.ppt,.pptx,.pub").Contains(type))
{
return type;
}
else
{
switch (type)
{
//case ".txt"://txt文本文件
// type = ".rtf";
// break;
case ".wps"://wps文件
type = ".doc";
break;

case ".et"://wps的表格文件
type = ".xls";
break;

case ".pps":
case ".dps":
type = ".ppt";
break;
}
}
return type;
}
insus 2012-09-24
  • 打赏
  • 举报
回复
Insus.NET只重构了最后一个方法,不知是否有可参考的地方:
http://www.cnblogs.com/insus/archive/2012/09/24/2700710.html
Banianer 2012-09-24
  • 打赏
  • 举报
回复
比超过5行代码~~ 这么夸张。
足球中国 2012-09-24
  • 打赏
  • 举报
回复
以软件开发标准。一个函数不超过5行代码。估计是要重构的。
Banianer 2012-09-24
  • 打赏
  • 举报
回复
楼主这么多分只给这么点。 这个功能可以收藏下,或许会用到。

61,658

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧