J'essaie d'apprendre des motifs et j'ai un travail qui crie pour un motif, je le sais juste mais je ne peux pas le comprendre. Je sais que le type de filtre est quelque chose qui peut être abstrait et éventuellement comblé. Je ne suis pas à la recherche d'un CODE REWRITE JUSTE SUGGESTIONS. Je ne cherche pas quelqu'un pour faire mon travail. Je voudrais savoir comment les modèles pourraient être appliqués à cet exemple.Bridge ou Factory et comment
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Xml;
using System.Text.RegularExpressions;
namespace CopyTool
{
class CopyJob
{
public enum FilterType
{ TextFilter, RegExFilter, NoFilter }
public FilterType JobFilterType { get; set; }
private string _jobName;
public string JobName { get { return _jobName; } set { _jobName = value; } }
private int currentIndex;
public int CurrentIndex { get { return currentIndex; } }
private DataSet ds;
public int MaxJobs { get { return ds.Tables["Job"].Rows.Count; } }
private string _filter;
public string Filter { get { return _filter; } set { _filter = value; } }
private string _fromFolder;
public string FromFolder
{
get { return _fromFolder; }
set
{
if (Directory.Exists(value))
{ _fromFolder = value; }
else
{ throw new DirectoryNotFoundException(String.Format("Folder not found: {0}", value)); }
}
}
private List<string> _toFolders;
public List<string> ToFolders { get { return _toFolders; } }
public CopyJob()
{
Initialize();
}
private void Initialize()
{
if (ds == null)
{ ds = new DataSet(); }
ds.ReadXml(Properties.Settings.Default.ConfigLocation);
LoadValues(0);
}
public void Execute()
{
ExecuteJob(FromFolder, _toFolders, Filter, JobFilterType);
}
public void ExecuteAll()
{
string OrigPath;
List<string> DestPaths;
string FilterText;
FilterType FilterWay;
foreach (DataRow rw in ds.Tables["Job"].Rows)
{
OrigPath = rw["FromFolder"].ToString();
FilterText = rw["FilterText"].ToString();
switch (rw["FilterType"].ToString())
{
case "TextFilter":
FilterWay = FilterType.TextFilter;
break;
case "RegExFilter":
FilterWay = FilterType.RegExFilter;
break;
default:
FilterWay = FilterType.NoFilter;
break;
}
DestPaths = new List<string>();
foreach (DataRow crw in rw.GetChildRows("Job_ToFolder"))
{
DestPaths.Add(crw["FolderPath"].ToString());
}
ExecuteJob(OrigPath, DestPaths, FilterText, FilterWay);
}
}
private void ExecuteJob(string OrigPath, List<string> DestPaths, string FilterText, FilterType FilterWay)
{
FileInfo[] files;
switch (FilterWay)
{
case FilterType.RegExFilter:
files = GetFilesByRegEx(new Regex(FilterText), OrigPath);
break;
case FilterType.TextFilter:
files = GetFilesByFilter(FilterText, OrigPath);
break;
default:
files = new DirectoryInfo(OrigPath).GetFiles();
break;
}
foreach (string fld in DestPaths)
{
CopyFiles(files, fld);
}
}
public void MoveToJob(int RecordNumber)
{
Save();
LoadValues(RecordNumber - 1);
}
public void AddToFolder(string folderPath)
{
if (Directory.Exists(folderPath))
{ _toFolders.Add(folderPath); }
else
{ throw new DirectoryNotFoundException(String.Format("Folder not found: {0}", folderPath)); }
}
public void DeleteToFolder(int index)
{
_toFolders.RemoveAt(index);
}
public void Save()
{
DataRow rw = ds.Tables["Job"].Rows[currentIndex];
rw["JobName"] = _jobName;
rw["FromFolder"] = _fromFolder;
rw["FilterText"] = _filter;
switch (JobFilterType)
{
case FilterType.RegExFilter:
rw["FilterType"] = "RegExFilter";
break;
case FilterType.TextFilter:
rw["FilterType"] = "TextFilter";
break;
default:
rw["FilterType"] = "NoFilter";
break;
}
DataRow[] ToFolderRows = ds.Tables["Job"].Rows[currentIndex].GetChildRows("Job_ToFolder");
for (int i = 0; i <= ToFolderRows.GetUpperBound(0); i++)
{
ToFolderRows[i].Delete();
}
foreach (string fld in _toFolders)
{
DataRow ToFolderRow = ds.Tables["ToFolder"].NewRow();
ToFolderRow["JobId"] = ds.Tables["Job"].Rows[currentIndex]["JobId"];
ToFolderRow["Job_Id"] = ds.Tables["Job"].Rows[currentIndex]["Job_Id"];
ToFolderRow["FolderPath"] = fld;
ds.Tables["ToFolder"].Rows.Add(ToFolderRow);
}
}
public void Delete()
{
ds.Tables["Job"].Rows.RemoveAt(currentIndex);
LoadValues(currentIndex++);
}
public void MoveNext()
{
Save();
currentIndex++;
LoadValues(currentIndex);
}
public void MovePrevious()
{
Save();
currentIndex--;
LoadValues(currentIndex);
}
public void MoveFirst()
{
Save();
LoadValues(0);
}
public void MoveLast()
{
Save();
LoadValues(ds.Tables["Job"].Rows.Count - 1);
}
public void CreateNew()
{
Save();
int MaxJobId = 0;
Int32.TryParse(ds.Tables["Job"].Compute("Max(JobId)", "").ToString(), out MaxJobId);
DataRow rw = ds.Tables["Job"].NewRow();
rw["JobId"] = MaxJobId + 1;
ds.Tables["Job"].Rows.Add(rw);
LoadValues(ds.Tables["Job"].Rows.IndexOf(rw));
}
public void Commit()
{
Save();
ds.WriteXml(Properties.Settings.Default.ConfigLocation);
}
private void LoadValues(int index)
{
if (index > ds.Tables["Job"].Rows.Count - 1)
{ currentIndex = ds.Tables["Job"].Rows.Count - 1; }
else if (index < 0)
{ currentIndex = 0; }
else
{ currentIndex = index; }
DataRow rw = ds.Tables["Job"].Rows[currentIndex];
_jobName = rw["JobName"].ToString();
_fromFolder = rw["FromFolder"].ToString();
_filter = rw["FilterText"].ToString();
switch (rw["FilterType"].ToString())
{
case "TextFilter":
JobFilterType = FilterType.TextFilter;
break;
case "RegExFilter":
JobFilterType = FilterType.RegExFilter;
break;
default:
JobFilterType = FilterType.NoFilter;
break;
}
if (_toFolders == null)
_toFolders = new List<string>();
_toFolders.Clear();
foreach (DataRow crw in rw.GetChildRows("Job_ToFolder"))
{
AddToFolder(crw["FolderPath"].ToString());
}
}
private static FileInfo[] GetFilesByRegEx(Regex rgx, string locPath)
{
DirectoryInfo d = new DirectoryInfo(locPath);
FileInfo[] fullFileList = d.GetFiles();
List<FileInfo> filteredList = new List<FileInfo>();
foreach (FileInfo fi in fullFileList)
{
if (rgx.IsMatch(fi.Name))
{
filteredList.Add(fi);
}
}
return filteredList.ToArray();
}
private static FileInfo[] GetFilesByFilter(string filter, string locPath)
{
DirectoryInfo d = new DirectoryInfo(locPath);
FileInfo[] fi = d.GetFiles(filter);
return fi;
}
private void CopyFiles(FileInfo[] files, string destPath)
{
foreach (FileInfo fi in files)
{
bool success = false;
int i = 0;
string copyToName = fi.Name;
string copyToExt = fi.Extension;
string copyToNameWithoutExt = Path.GetFileNameWithoutExtension(fi.FullName);
while (!success && i < 100)
{
i++;
try
{
if (File.Exists(Path.Combine(destPath, copyToName)))
throw new CopyFileExistsException();
File.Copy(fi.FullName, Path.Combine(destPath, copyToName));
success = true;
}
catch (CopyFileExistsException ex)
{
copyToName = String.Format("{0} ({1}){2}", copyToNameWithoutExt, i, copyToExt);
}
}
}
}
}
public class CopyFileExistsException : Exception
{
public string Message;
}
}
Pourriez-vous expliquer la nature du problème et l'intention du code? – SWeko
Quel est le problème que vous essayez de résoudre? Les patterns sont là pour résoudre des problèmes, ne pas être giflés sur un ancien code juste parce que vous voulez utiliser 'patterns '. – Oded
Chris ... vous pourriez avoir une meilleure réponse si vous choisissez une pièce que vous aimeriez voir refactorisée. C'est le tout "Comment manges-tu un éléphant?" problème. Personne ne veut réécrire tout votre bruit juste pour donner un aperçu de ce qui pourrait être fait différent. –