3
I have a method that generates some information. As I was in a hurry to deliver, I did and sent. Well, it turns out, seeing the code more carefully, I realized how inelegant it is. It was made to meet a specification and as it was changed I had to change the code. The current rule is as follows:
I have a method that handles files from Pharmacist. The same goes for the Drugstore. It happens, that now I need to create before the folder Pharmacist, two new folders. Homologation and Production. In each folder(Homologation and Production) there should be two folders: Farminterna and Farmexterna. I will talk only about Farminterna, which will also apply to External. The same files as Gero in the path: ...Homolagacao/FarmInterna/web/...
are the same to: ...Producao/FarmInterna/web/...
. This has made my code extremely ugly and inelegant. How do I not duplicate the code as is? Is there a Pattern for this? Below my code. Where the letter exists H refers to Homolagation and P the production.
private void CriaPastaFarmInterna()
{
string novo_path_H = caminho_original + @"\Destino\Temp\Homologacao\FarmInterna\web";
string novo_path_P = caminho_original + @"\Destino\Temp\Producao\FarmInterna\web";
string path_files = caminho_original + @"\Destino\Temp";
DirectoryInfo dirInfoH = new DirectoryInfo(novo_path_H);
DirectoryInfo dirInfoP = new DirectoryInfo(novo_path_P);
int indice = 1;
if (dirInfoH.Exists == false)
Directory.CreateDirectory(novo_path_H);
if (dirInfoP.Exists == false)
Directory.CreateDirectory(novo_path_P);
List<String> myFiles = Directory.GetFiles(caminho_original + @"\Destino\Temp\web", "*.*", SearchOption.AllDirectories).ToList();
List<String> myDirectoriesH = Directory.GetDirectories(caminho_original + @"\Destino\Temp\web").ToList();
List<String> myDirectoriesP = Directory.GetDirectories(caminho_original + @"\Destino\Temp\web").ToList();
var diretorios_H = myDirectoriesH.Where(d => !d.Contains("FarmInterna"));
var diretorios_P = myDirectoriesP.Where(d => !d.Contains("FarmInterna"));
try
{
foreach (string file in myFiles)
{
FileInfo mFile = new FileInfo(file);
string newFileH = novo_path_H + (file.Replace(caminho_original + @"\Destino\Temp\web", ""));
string newFileP = novo_path_P + (file.Replace(caminho_original + @"\Destino\Temp\web", ""));
if (!Directory.Exists(newFileH))
Directory.CreateDirectory(Path.GetDirectoryName(newFileH));
if (!Directory.Exists(newFileP))
Directory.CreateDirectory(Path.GetDirectoryName(newFileP));
if (new FileInfo(newFileH).Exists == false)
mFile.CopyTo(newFileH);
if (new FileInfo(newFileP).Exists == false)
mFile.CopyTo(newFileP);
}
RenomearWebConfig(novo_path_H, "H");
RenomearWebConfig(novo_path_P, "P");
}
catch(Exception ex)
{}
}
My initial idea would be to take out the duplicate lines in this code, bearing in mind that they create new directories and the file being repeated in the two directories created. You would change mainly the performance and the number of lines decreasing accordingly. As the directory is new and the files are new it does a check only in one of the folders. You can do the following, run this routine to a folder and at the end copy the files to the other folder is also an idea. You can use Adapter, but, it does not need to my view
– user46523