1.类的结构更加松散了,
2.类本身也变得更小,名称利于理解.
3.最重要的是获得了一种新的视角,可以从外到内的驱动程序的开发.不知不觉间有了一种很明确的目标感,知道自己在干什么.而且精力就集中在这个点上这个感觉实在是好.这个类似于上帝的视角.
4.目前我用接口将所有的外部依赖解耦和掉.这样做就像自己本身获得了自由一样.代码可以到处的移植.更换皮肤,更换数据库soeasy.
5.更好的可维护性.发现断点调试用的越来越少了.
6.目前的开发方式转换为先用测试数据和Monk搭建一个程序骨架,然后再填充里面的细节.
7.试试由外到内将一个巨类分解为可以测试的小类.瞄定输入输出就可以开干了.试试效果.
一开始是这个样子的记录一下.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using webdownLoad;
using System.Diagnostics;
using Microsoft.Win32;
using myClassNameSpace;
using System.Windows.Forms;
using System.IO;
using System.ComponentModel;
using System.Threading;
namespace zcx
{
public partial class DII
{
public class CZuPei
{
int _CiShu = 0;
public int CiShu { get { return _CiShu; } }
int _Count = 0;
const int _BuChang = 30;
bool _ZuoPeiOk;
public bool ZUoPeiOK { get { return _ZuoPeiOk; } }
public CZuPei(int count)
{
_CiShu = 0;
_Count = count;
_ZuoPeiOk = false;
}
public void BeginZuPei()
{
int JiShu = 0;
bool beChecked = false;
for (int i = _CiShu * _BuChang; i < _Count + _CiShu; i++)
{
//form.zupeiCheckTrue(i);form中选择第i个.
JiShu++;
if (JiShu == _BuChang)
{
_CiShu++;
beChecked = true;
//form.zupeiClickOK();//开始组配.
break;
}
}
if (!beChecked)
{
//此处所有的就会结束.
_CiShu++;
//form.zupeiClickOK()
_ZuoPeiOk = true;
}
}
}
public class SendTS
{
const string JianSuoWrong = "点击检索失败";
const string addTsToBoxWrong = "输入Ts失败";
const string OKMsg = "传送数据完毕";
const string TSEmpty = "TS数量为零";
int _TSIdx;
bool _sendOK;
//在complete循环中判断是否传送ts完毕.
public bool SendOk { get { return _sendOK; } }
public int TSIdx { get { return _TSIdx; } }
List<string> _TSList;
WrongTs _wrongTs;
IZCXForm _form;
public List<string> TSList { get { return _TSList; } }
public SendTS(IZCXForm form)
{
_form = form;
_TSList = new List<string>();
_TSIdx = 0;
_wrongTs = new WrongTs();
_sendOK = false;
}
//鼠标点击时和complete中调用.
public void AddTsListToWeb()
{
if (_TSList.Count == 0)
{
_form.addMsg(TSEmpty);
return;
}
if (_TSIdx == 0)
{
_wrongTs.clear();
}
else
{
//得到在complete 中的错误数据.并进行添加.
string wrong = "";
string ts = _TSList[_TSIdx];
//_form1.getWrong
wrong = _form.GetTsWrong();
if (wrong != "")
{
_wrongTs.Add(wrong, _TSList[_TSIdx]);
}
else
{
_form.addMsg(_TSList[_TSIdx] + "提交发生错误.unimpossbel??");
}
}
if (_TSIdx < _TSList.Count)
{
string ats = _TSList[_TSIdx];
if (_form.AddTsToBox(ats))
{
if (_form.ClickJianSuoButton())
{
_TSIdx++;
}
else
{
_form.addMsg(JianSuoWrong);
}
}
else
{
_form.addMsg(addTsToBoxWrong);
}
}
else
{
_sendOK = true;
_form.addMsg(OKMsg);
}
}
public void ReadTSTxt(string[] FromFile)
{
_TSList.Clear();
if (FromFile == null)
{
return;
}
foreach (string item in FromFile)
{
_TSList.Add(item);
}
}
}
private void LogInComplete(object sender, RunWorkerCompletedEventArgs e)
{
//输入用户名和密码.
//<input name="svpn_name_show" class="inptxt" id="svpn_name_show" type="text" maxlength="49" autocomplete="off">
//<input name="svpn_name" class="inptxt inphid" id="svpn_name" type="hidden" maxlength="49" autocomplete="off">
//<input name="svpn_password" class="inptxt" id="svpn_password" type="password" maxlength="48" autocomplete="off">
//<button class="btn" id="logButton" οnmοuseοver="this.className='btnred'" οnmοuseοut="this.className='btn'" οnfοcus="closeSoftKb()" type="submit">登 录</button>
_form.addMsg("此处延时了5秒,须找到webbrowser加载完毕的代码才能将此延时去掉.");
HtmlDocument HD = _form.Web_Doc;
HtmlElement aele = HD.GetElementById("svpn_name_show");
if (aele != null)
{
aele.InnerText = "las_zhangcx";
}
aele = HD.GetElementById("svpn_name");
if (aele != null)
{
//aele.InnerText = "las_zhangcx";
}
aele = HD.GetElementById("svpn_password");
if (aele != null)
{
aele.InnerText = "las111111";
}
aele = HD.GetElementById("logButton");
if (aele != null)
{
aele.InvokeMember("click");
//Referer: https://q.csdl.ac.cn/por/login_psw.csp
//Content-Type: application/x-www-form-urlencoded
//TWFID=68600c9ceedcb5d6; collection=%7Bauto_login_count%3A0%7D; g_LoginPage=login_psw; VisitTimes=0; haveLogin=0
_LiuCheng = LiuCheng.LList;
//转到complete.
}
}
private void waitForLogIn(object sender, DoWorkEventArgs e)
{
Thread.Sleep(5000);
}
private void waitList(object sender, DoWorkEventArgs e)
{
Thread.Sleep(10000);
}
private void ListComplete(object sender, RunWorkerCompletedEventArgs e)
{
_form.addMsg("此处延时了10秒,认证好费时啊....");
//DERWENT INNOVATIONS
_form.addMsg("进入Listcomplete.");
HtmlDocument HD = _form.Web_Doc;
HtmlElementCollection all_a = HD.GetElementsByTagName("a");
bool findItem=false;
foreach(HtmlElement item in all_a)
{
if (item.InnerText=="DERWENT INNOVATIONS")
{
_form.addMsg("Good,找到DERWENT INNOVATIONS链接,点击之.");
//会在新窗口打开连接.
item.InvokeMember("click");
findItem = true;
break;
}
}
if ( findItem==false)
{
_form.addMsg("坏事,没有找到dii库链接,系统无法前进:-(");
}
}
}
}
还有一部分这是个part类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using webdownLoad; using System.Diagnostics; using Microsoft.Win32; using myClassNameSpace; using System.Windows.Forms; using System.IO; using System.ComponentModel; using System.Threading; namespace zcx { ///html/body/table[4]/tbody/tr/td[3]/table[3]/tbody/tr/td/table[16]/tbody/tr[2]/td[2]/strong/a public partial class DII { //在complete中使用的控制变量. public enum LiuCheng : int { //las.ac.cn LLas, //随易通 LSYT, //到各个库的跳转页面. LList, //Dii库. LDii, //发送ts数据. LTS, //错误流程. LNewHuiHua } LiuCheng _LiuCheng; public LiuCheng Liu_Cheng { get { return _LiuCheng; } } IZCXForm _form; bool _beginSendTS; public bool BeginSendTs { get { return _beginSendTS; } } //开始打开随意通. string _SYTUri; //BackColor = Color.FromArgb(38,38,38); //组配. bool _beginZuPei; int _ZuPeiIdx; WrongTs wrongTS { get { return _wrongTs; } } public DII(IZCXForm form) { _form = form; _TSIdx = 0; _beginSendTS = false; _beginZuPei = false; _ZuPeiIdx = 0; _TSList = new List<string>(); _wrongTs = new WrongTs(); } public void OpenWebOfKnow(string uri) { _form.Web_Navigate(uri); } public void OpenSuiYiTong() { _LiuCheng = LiuCheng.LLas; _form.Web_Navigate("www.las.ac.cn"); } int _TSIdx; List<string> _TSList; WrongTs _wrongTs; public void JianSuo() { //开始检索数据库. _wrongTs.clear(); _TSList.Clear(); _TSIdx = 0; removeControls(); string fileName; _form.OpenDiaLog_Filter = "文本文件(*.txt)|*.txt"; if (_form.OpenDiaLog_ShowDialog() == DialogResult.OK) { fileName = _form.OpenDialog_FileName; } else { return; } readTs(fileName); //进度条. _form.ProgressBar_Value = 0; _form.ProgressBar_Maximum = _TSList.Count; _form.ProgressBar_Minimum = 0; if (_TSList.Count > _TSIdx) { if (_form.AddTsToBox(_TSList[_TSIdx])) { if (_form.ClickJianSuoButton()) { _beginSendTS = true; } } } } private bool setGaoJiJianSuo() { HtmlDocument doc = _form.Web_Doc; HtmlElement jiBen = doc.GetElementById("searchTypes"); if (jiBen.InnerText == "基本检索") { HtmlElementCollection elements = doc.GetElementsByTagName("a"); // <a target="" href="DIIDW_AdvancedSearch_input.do?SID=P2rztUa4YLVgxlgYtG3&product=DIIDW&search_mode=AdvancedSearch" title="高级检索" οncοntextmenu="javascript:return IsAllowedRightClick(this);" hasautosubmit="true">高级检索</a> foreach (HtmlElement ele in elements) { if (ele.GetAttribute("title").Trim() == "高级检索") { ele.InvokeMember("click"); return true; } } } return false; } private void getCuoWu() { // <div id="client_error_input_message" class="errorText">检索错误: 检索式无效。请检查语法。</div> bool isCuowu = false; HtmlDocument doc = _form.Web_Doc; //HtmlElement erroInput = doc.GetElementById("client_error_input_message"); //if (erroInput != null) //{ // if (erroInput.InnerText.IndexOf("检索式无效")!=-1) // { // _wrongTs.Add("检索式无效", _TSList[_TSIdx]); // isCuowu = true; // } //} //<div class="newErrorHead">检索后没有发现记录。</div> HtmlElementCollection els = doc.GetElementsByTagName("div"); foreach (HtmlElement aEl in els) { string temp = aEl.InnerText; if (temp != null) { if (temp.IndexOf("检索后没有发现记录") != -1) { _wrongTs.Add("记录为零", _TSList[_TSIdx]); isCuowu = true; break; } else if (temp.IndexOf("检索错误") != -1) { _wrongTs.Add("检索错误", _TSList[_TSIdx]); isCuowu = true; break; } } } if (!isCuowu) { _wrongTs.Add("正确", _TSList[_TSIdx]); } } private void readTs(string fileName) { if (File.Exists(fileName)) { string[] atempList = File.ReadAllLines(fileName); foreach (var str in atempList) { _TSList.Add(str); } } _TSIdx = 0; } private void removeControls() { _form.RemoveControls(); } private bool setStartYear() { string startYear = "1984"; HtmlDocument doc = _form.Web_Doc; //选中年范围. HtmlElement yearRadio = doc.GetElementById("periodRange"); yearRadio.InvokeMember("click"); HtmlElementCollection elments = doc.GetElementsByTagName("span"); foreach (HtmlElement ele in elments) { if (ele.GetAttribute("id") != null) { // Text_Info.AppendText(ele.GetAttribute("id") + "\r\n"); } if (ele.GetAttribute("id").Trim() == "select2-chosen-4") { if (ele.InnerText != startYear) { ele.InnerText = startYear; return true; } } } return false; } public void ZuPei() { //第一行. //<tr id="set_1_row" //<input id="combineSets1" type="checkbox" οnclick="swapCheckboxLabel(this, 'combineLabel1', '选择检索式进行组配。', '此检索式已被选定组配到一个检索式组配中。');" value="1" name="combineSets"> //第二行 //<tr id="set_2_row" class=""> //<input id="combineSets2" type="checkbox" οnclick="swapCheckboxLabel(this, 'combineLabel2', '选择检索式进行组配。', '此检索式已被选定组配到一个检索式组配中。');" value="2" name="combineSets"> for (int idx = 1; idx <= _TSIdx; idx++) { //string tr = "set_" + idx.ToString() + "_row"; string checkIuput = "combineSets" + idx.ToString(); HtmlDocument doc = _form.Web_Doc; HtmlElement el = doc.GetElementById(checkIuput); if (el != null) { el.InvokeMember("click"); } } //选中OR选项. //<input type="radio" ""="" value="OR" name="combineOp"> OR HtmlElementCollection els = _form.Web_Doc.GetElementsByTagName("input"); foreach (HtmlElement el in els) { if (el.GetAttribute("type") == "radio" & el.GetAttribute("value") == "OR") { el.InvokeMember("click"); } } _beginZuPei = false; } public void NewWindow() { if (_LiuCheng == LiuCheng.LLas) { _form.Web_Navigate(_SYTUri); _LiuCheng = LiuCheng.LSYT; _form.addMsg("在NewWindow中转换到随易通登陆状态."); //去除警告. //System.Diagnostics.Process.Start("webWorning"); return; } if (_LiuCheng == LiuCheng.LList) { //取自网页中的js数据. string diiUri = "http://www.webofknowledge.com/diidw"; _LiuCheng = LiuCheng.LDii; _form.Web_Navigate(diiUri); _form.addMsg("在newwindow中将状态转换为Dii状态"); return; } { } } public void WebBroserComplete() { _form.Edit_Text = _form.Web_Doc.Url.ToString(); if (_beginSendTS) { getCuoWu(); _TSIdx = _TSIdx + 1; _form.ProgressBar_Value++; if (_TSIdx >= _TSList.Count) { _beginSendTS = false; _beginZuPei = true; ZuPei(); _form.showCuoWu(_wrongTs); return; } HtmlDocument doc = _form.Web_Doc; HtmlElement el = doc.GetElementById("value(input1)"); HtmlElement elButton = doc.Forms["DIIDW_AdvancedSearch_input_form"]; if (el != null) { el.InnerText = _TSList[_TSIdx]; } if (elButton != null) { elButton.InvokeMember("submit"); _beginSendTS = true; return; } } if (_LiuCheng == LiuCheng.LLas) { //<a target="_blank" href="https://q.csdl.ac.cn/">随易通</a> HtmlDocument HD = _form.Web_Doc; HtmlElementCollection els = HD.Links; //点击随易通. foreach (HtmlElement ele in els) { if (ele.InnerText != null) { if (ele.InnerText.Trim() == "随易通") { //转到newwindow _SYTUri = ele.GetAttribute("href"); ele.InvokeMember("click"); return; } } } } if (_LiuCheng == LiuCheng.LSYT) { //formLogin HtmlDocument HD = _form.Web_Doc; HtmlElement aform = HD.Forms["formLogin"]; if (aform != null) { //此处需要等待一段时间来让网页加载.所以开个线程等待时间. using (BackgroundWorker bw = new BackgroundWorker()) { bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(LogInComplete); bw.DoWork += new DoWorkEventHandler(waitForLogIn); bw.RunWorkerAsync("Tank"); } return; } } if (_LiuCheng == LiuCheng.LList) { //DERWENT INNOVATIONS //此处需要等待一段时间来让网页加载.所以开个线程等待时间. using (BackgroundWorker bw = new BackgroundWorker()) { bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(ListComplete); bw.DoWork += new DoWorkEventHandler(waitList); bw.RunWorkerAsync("Tank"); } return; } if (_LiuCheng == LiuCheng.LDii) { //准备数据开始下载. //设置开始时间. setStartYear(); //设置高级检索. // <a target="" href="DIIDW_AdvancedSearch_input.do?SID=T1J3XQFcPqzXlaTB1ak&product=DIIDW&search_mode=AdvancedSearch" title="高级检索" οncοntextmenu="javascript:return IsAllowedRightClick(this);" hasautosubmit="true">高级检索</a> //<a id="searchTypes" href="javascript:;" title="基本检索" οncοntextmenu="javascript:return IsAllowedRightClick(this);" hasautosubmit="true"> //如果判断 searchTypes 不是advanced那么点击高级检索,设置为高级检索. HtmlElement item = _form.Web_Doc.GetElementById("searchTypes"); if (item != null) { if (item.GetAttribute("title") == "基本检索") { HtmlElementCollection all_a = _form.Web_Doc.Links; foreach (HtmlElement a_a in all_a) { if (a_a.GetAttribute("title") == "高级检索") { _LiuCheng = LiuCheng.LTS; a_a.InvokeMember("click"); return; } } } } else { _form.addMsg("在liucheng 为 LDii 时,searchTypes must be find ,impossble wrong!"); } } if (_LiuCheng == LiuCheng.LTS) { } }//complete end } }