I'm trying to learn patterns and I've got a job that is screaming for a pattern, I just know it but I can't figure it out. I know the filter type is something that can be abstracted and possibly bridged. I'M NOT LOOKING FOR A CODE REWRITE JUST SUGGESTIONS. I'm not looking for someone to do my job. I would like to know how patterns could be applied to this example.
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;
}
}
This code is also "screaming" to be broken down into smaller more specialized objects.
Your CopyJob object seems to be more of a manager of a list of jobs. I would maybe change the name of this to CopyJobManager or something. You could then have CopyJob be the base class for the different filter types. The common code, Execute() for example, would be defined in the base class, and the custom behavior, Filtering for example, would be handled in the derived classes. You would have a TextFilterCopyJob, a RegExFilterCopyJob, and a NoFilterCopyJob.
Where the Factory pattern could come into play is when you're building a list of CopyJobs. You could have a CopyJobFactory object that takes in a row from your dataset and returns the proper child version of CopyJob. CopyJobManager would then do its operations on a list of CopyJobs instead of a list of dataset rows.
Whenever I see Swithcs or bricks of Ifs, I jump to the conclusion that atleast a strategy pattern could be created.
a clean and easy way to set one up, is use a dictionary<>
in your case your going to want a key value based on the filterName your cases relate to, and the value will be a new object of the filters.
now you can merely give the string to the dictionarys TryGetValue method and have it retrieve the correct filter object for you, boom!
Now you can encapsulate the mapping of the filters <--> Strings, and keep the logic and use of the filters from having to see the logic of retrieving the correct object!
There's nothing wrong with using a switch statement like you have. It's not screaming for any design pattern other than that you can put it in a function so that you don't have the same switch twice.
The switch will be faster than using reflection, and the problem you're trying to solve doesn't really require the Factory pattern.
Here's some of what I did to implement a Factory pattern
First, I created an interface for the filter:
interface IFileFilter
{
string GetFilterName();
string GetFilterReadableName();
FileInfo[] GetFilteredFiles(string path, string filter);
}
then I created sub-filter classes for this interface:
class RegExFileFilter : IFileFilter
{
#region IFileFilter Members
public string GetFilterName()
{
return "RegExFilter";
}
public string GetFilterReadableName()
{
return "RegEx Filter";
}
public FileInfo[] GetFilteredFiles(string path, string filter)
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] fullFileList = d.GetFiles();
List<FileInfo> filteredList = new List<FileInfo>();
Regex rgx = new Regex(filter);
foreach (FileInfo fi in fullFileList)
{
if (rgx.IsMatch(fi.Name))
{
filteredList.Add(fi);
}
}
return filteredList.ToArray();
}
#endregion
}
class TextFileFilter : IFileFilter
{
#region IFileFilter Members
public string GetFilterName()
{
return "TextFilter";
}
public string GetFilterReadableName()
{
return "Text Filter";
}
public FileInfo[] GetFilteredFiles(string path, string filter)
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] fi = d.GetFiles(filter);
return fi;
}
#endregion
}
class NoFileFilter : IFileFilter
{
#region IFileFilter Members
public string GetFilterName()
{
return "TextFilter";
}
public string GetFilterReadableName()
{
return "Text Filter";
}
public FileInfo[] GetFilteredFiles(string path, string filter)
{
DirectoryInfo d = new DirectoryInfo(path);
FileInfo[] fi = d.GetFiles(filter);
return fi;
}
#endregion
}
Then I created a Factory:
public static IFileFilter FileFilter(string filterName)
{
switch (filterName)
{
case "Text Filter":
return new TextFileFilter();
case "RegEx Filter":
return new RegExFileFilter();
default:
return new NoFileFilter();
}
}
I would suggest the following:
Refactor the switch statements (as #Jordan mentioned)
Add an extension method to convert the FilterType enum into an int and save that to the database rather than a string. E.g.
public static class FilterTypeExtensions
{
public static int AsNumeric(this FilterType filterType)
{
return (int)filterType;
}
}
As a minor point, the single line braces are horrible, either drop the braces or use proper spacing/indentation. :)
Related
I have Java service what creates Menu tree with all children. It works perfectly for Menu.
I want to rewrite with .Net5 but it's difficult for me.
// Java
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Stream;
public class AdminNode implements Iterable<Resource>
{
private Resource resource;
private List<AdminNode> children;
public List<AdminNode> getChildren() {
return children;
}
public AdminNode(Resource resource)
{
this.resource = resource;
this.children = new ArrayList<>();
}
public void addResource(Resource resource)
{
String[] pathArr = resource.getPath().split("-");
List<Integer> intArray = new ArrayList<Integer>();
for(int i = 0; i < pathArr.length; i++) {
intArray.add(Integer.parseInt(pathArr[i]));
}
addResource(resource, intArray);
}
private void addResource(Resource resource, List<Integer> path)
{
if (path.size() > 1)
{
Integer nextParent = path.get(0);
path.remove(0);
for (AdminNode child : children)
{
if (child.getResource().getId().equals(nextParent))
{
child.addResource(resource, path);
}
}
}
else
{
children.add(new AdminNode(resource));
}
}
public Resource getResource() { return resource; }
#Override
public Iterator<Resource> iterator()
{
return stream().iterator();
}
public Stream<Resource> stream()
{
return goDown(this).skip(1).map(AdminNode::getResource);
}
private static Stream<AdminNode> goDown(AdminNode node)
{
Stream<AdminNode> result = Stream.concat(Stream.of(node), node.children.stream().flatMap(AdminNode::goDown));
return result;
}
#Override
public String toString() {
return "AdminNode [resource=" + resource + ", children=" + children + "]";
}
}
I think class can to be:
// C#
public class AdminNode : IEnumerable<Resource> {
private Resource _resource;
private List<AdminNode> _children;
public List<AdminNode> getChildren() {
return _children;
}
public AdminNode(Resource resource)
{
_resource = resource;
_children = new List<>();
}
public void addResource(Resource resource)
{
var pathArr = _resource.Path.Split("-");
var intArray = new List<int>();
for(int i = 0; i < pathArr.Length; i++) {
intArray.Add(Convert.ToInt32(pathArr[i]));
}
addResource(resource, intArray);
}
private void AddResource(Resource resource, List<int> path)
{
if (path.Count > 1)
{
var nextParent = path[0];
path.Remove(0);
foreach (void child in _children)
{
if (child.GetResource().Id == nextParent)
{
child.AddResource(resource, path);
}
}
}
else
{
_children.Add(new AdminNode(resource));
}
}
public Resource GetResource() { return _resource; }
public IEnumerator<Resource> GetEnumerator()
{
//?
}
IEnumerator IEnumerable.GetEnumerator()
{
// ?
}
// and there I stopped.
}
.Net5 does not has Stream, flatMap. Maybe FlatMap I can change with Linq SelectMany.
I don't understand three methods:
stream()
goDown().
How to convert these methods to .Net5?
From a brief glance at Stream in Java, it looks like that is basically their implementation of IEnumerable<T> and LINQ in C#. Essentially it is a toolset to do lazy evaluations over collections. Below is my best guess of what you are needing in C#
public class AdminNode : IEnumerable<Resource>
{
private List<AdminNode> _children = new List<AdminNode>();
public AdminNode(Resource resource)
{
Resource = resource;
}
public Resource Resource { get; }
public IEnumerable<AdminNode> GoDown()
{
var stack = new Stack<AdminNode>();
//this could be done recursively, but explicitly implementing with a stack
//reduces the risk of a stack overflow exception with large trees
stack.Push(this);
while(stack.Any())
{
var next = stack.Pop();
yield return next;
foreach(var child in next._children)
{
stack.Push(child);
}
}
}
public IEnumerator<Resource> GetEnumerator() { return GoDown().Select(x => x.Resource).GetEnumerator(); }
IEnumerator IEnumerable.GetEnumerator() { return GoDown().Select(x => x.Resource).GetEnumerator(); }
}
In this implementation, GoDown() should return an IEnumerable<AdminNode> of all the AdminNodes starting at the current AdminNode and all nodes below it.
If you enumerate an AdminNode it will enumerate over all resources starting at the current node, and then all resources of children below that node.
I'm creating a program which will execute a command after user input.
Some commands I want to implement are: creating, reading a file, getting current working directory etc.
I created a dictionary which will store user input and corresponding command:
public static Dictionary<string, Action<string[]>> Commands { get; set; } = new Dictionary<string, Action<string[]>>()
{
{"pwd", PrintWorkingDirectory },
{"create", CreateFile },
{"print", ReadFile },
};
Unfortunately I have issues with triggering the method:
public void Run()
{
Console.WriteLine("Welcome, type in command.");
string input = null;
do
{
Console.Write("> ");
input = Console.ReadLine();
Execute(input);
} while (input != "exit");
}
public int Execute(string input)
{
if(Commands.Keys.Contains(input))
{
var action = Commands.Values.FirstOrDefault(); //doesn't work, gives '{command} not found'
}
Console.WriteLine($"{input} not found");
return 1;
}
Also I noticed that this solution would not work with method which is not void, but returns something, as for example CreateFile.
public static string CreateFile(string path)
{
Console.WriteLine("Create a file");
string userInput = Console.ReadLine();
try
{
string[] file = userInput.Split(new char[] { ' ' }).Skip(1).ToArray();
string newPath = Path.GetFullPath(Path.Combine(file));
using (FileStream stream = new FileStream(newPath, FileMode.Create, FileAccess.ReadWrite))
{
stream.Close();
}
using (StreamWriter sw = new StreamWriter(newPath))
{
Console.WriteLine("Please type the content.Press Enter to save.");
sw.WriteLine(Console.ReadLine());
sw.Close();
Console.WriteLine("File {0} has been created", newPath);
}
}
catch (Exception)
{
throw;
}
return path;
}
public static void ReadFile(string[] args)
{
Console.WriteLine("Reading file");
string userInput = Console.ReadLine();
string[] file = userInput.Split(new char[] { ' ' }).Skip(1).ToArray();
string newPath = Path.GetFullPath(Path.Combine(file));
string[] lines = File.ReadAllLines(newPath);
foreach (string line in lines)
Console.WriteLine(line);
}
public static void PrintWorkingDirectory(string[] args)
{
var currentDirectory = Directory.GetCurrentDirectory();
Console.WriteLine(currentDirectory);
}
Could somebody advise me how to deal with these issues?
Is it that this dictionary I created does not make much sense at all?
First problem: You're always fetching the first element of the dictionary and are not using the index operator to retrieve the correct value. Therefore change:
if(Commands.Keys.Contains(input))
{
var action = Commands.Values.FirstOrDefault(); //doesn't work, gives '{command} not found'
}
to:
public int Execute(string input)
{
if (Commands.Keys.Contains(input))
{
var action = Commands[input]; //doesn't work, gives '{command} not found'
action?.Invoke(new string[] { });
}
else
{
Console.WriteLine($"{input} not found");
}
return 1;
}
Regarding to your second question about dictionary usage. I think it is ok to use a dictionary to map different commands based on a given key. The alternative would be switch or if constructs, which can be prevented in Object Oriented Programming.
Regarding to your question about string CreateFile(string path). Since C# is strongly typed language your dictionary can only contain objects of type Action<string[]>, so you can't use methods with another signature than that. One solution is to add another dictionary in the form of Dictionary<string,Func<string[], string>. As a result you'll get more and more dictionaries depending on your method signatures. From here on you should think to build to encapsulate your commands in an e.g. CommandInterpreter class, that could offer an API like that:
void Request(string cmdName, string[] cmdParameters);
string GetLastResult();
int GetLastCode();
Update:
Below code shows a possible object oriented solution (I've left out interfaces to make the code more compact):
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApp1
{
public class Command<T>
{
public string Name { get; }
public T TheCommand { get; }
public Command(string name, T theCommand)
{
Name = name;
TheCommand = theCommand;
}
}
public interface ICommandResult
{
void Ok(Action<ICommandResult> yes, Action<ICommandResult> no);
int Code { get; }
string Description { get; }
}
public abstract class CommandResult : ICommandResult
{
public int Code { get; }
public string Description { get; }
protected CommandResult(int code, string description)
{
Code = code;
Description = description;
}
public abstract void Ok(Action<ICommandResult> yes, Action<ICommandResult> no);
}
public class NullCommandResult : CommandResult
{
public NullCommandResult() : base(-1, "null")
{
}
public override void Ok(Action<ICommandResult> yes, Action<ICommandResult> no) => no?.Invoke(this);
}
public class SuccessCommandResult : CommandResult
{
public SuccessCommandResult(string description) : base(0, description)
{
}
public override void Ok(Action<ICommandResult> yes, Action<ICommandResult> no) => yes?.Invoke(this);
}
public class CommandInterpreter
{
private Dictionary<string, Func<IEnumerable<string>, ICommandResult>> Commands = new Dictionary<string, Func<IEnumerable<string>, ICommandResult>>();
public void RegisterCommand(Command<Func<IEnumerable<string>, ICommandResult>> cmd)
=> Commands.Add(cmd.Name, cmd.TheCommand);
public ICommandResult RunCommand(string name, IEnumerable<string> parameters)
=> Commands.Where(kvp => kvp.Key.Equals(name))
.Select(kvp => kvp.Value)
.DefaultIfEmpty(strArr => new NullCommandResult())
.Single()
.Invoke(parameters);
}
class Program
{
private CommandInterpreter _cmdInterpreter;
private Program()
{
_cmdInterpreter = new CommandInterpreter();
_cmdInterpreter.RegisterCommand(new Command<Func<IEnumerable<string>, ICommandResult>>("pwd", PrintWorkingDirectory));
_cmdInterpreter.RegisterCommand(new Command<Func<IEnumerable<string>, ICommandResult>>("create", CreateFile));
_cmdInterpreter.RegisterCommand(new Command<Func<IEnumerable<string>, ICommandResult>>("print", ReadFile));
}
private static CommandResult ReadFile(IEnumerable<string> arg) => new SuccessCommandResult("File read");
private static CommandResult CreateFile(IEnumerable<string> arg) => new SuccessCommandResult("File xyz created");
private static CommandResult PrintWorkingDirectory(IEnumerable<string> arg) => new SuccessCommandResult("Printed something");
static void Main() => new Program().Run();
private void Run()
{
Console.WriteLine("Welcome, type in command.");
string input;
do
{
Console.Write("> ");
input = Console.ReadLine();
var cmdResult = _cmdInterpreter.RunCommand(input, Enumerable.Empty<string>());
cmdResult.Ok(
r => Console.WriteLine($"Success: {cmdResult.Code}, {cmdResult.Description}"),
r => Console.WriteLine($"FAILED: {cmdResult.Code}, {cmdResult.Description}"));
} while (input != "exit");
}
}
}
Output:
Welcome, type in command.
> pwd
Success: 0, Printed something
> create
Success: 0, File xyz created
> abc
FAILED: -1, null
>
You can just copy the code and play around with it.
How do I separate my code into their own classes and still have it function the same? This is currently what my code looks like.
using System;
using System.Collections.Generic;
using System.Xml;
using XCENT.JobServer.JobPlugIn;
using System.IO;
using HPD.API.Utility.DataAccess;
namespace DataPurge
{
public class Purge : IJob, IJobControl {
public IJobControl JobControl { get { return ( this ); } }
public int MaxInstanceCount { get { return 1; } }
public string Name { get { return "DataPurge"; } }
public Purge() { }
public void Run( string XmlFragment ) {
XmlNode xmlNode = null;
try
{
xmlNode = Common.ConstructXmlNodeFromString(XmlFragment, "Params");
var list = DataList();
foreach (var item in list)
{
var factory = new PurgerFactory(item);
IPurger purge = factory.Purger;
purge.Purge();
purge = null;
factory = null;
}
}
catch (Exception ex)
{
throw;
}
}
public interface IPurger
{
void Purge();
}
public enum PurgeType
{
File,
Database,
}
public class FilePurger : IPurger
{
private Parameters parameter;
public FilePurger(Parameters parameter)
{
this.parameter = parameter;
}
public void Purge()
{
var files = new DirectoryInfo(parameter.FilePath).GetFiles();
foreach (var file in files)
{
if (DateTime.Now - file.CreationTime > TimeSpan.FromDays(7))
{
File.Delete(file.FullName);
}
}
}
}
public class DbPurger : IPurger
{
private Parameters parameter;
public DbPurger(Parameters parameter)
{
this.parameter = parameter;
}
public void Purge()
{
var access = new SqlDataAccess();
var sqlParams = new Dictionary<string, object>();
sqlParams.Add("#OlderThanDays", parameter.OlderThanDays);
access.ExecuteNonQuery(parameter.CString, parameter.SPName, sqlParams, 30, false);
}
}
private List<Parameters> DataList()
{
var sqlParams = new SqlDataAccess();
var list = sqlParams.GetDataTableAsList<Parameters>("Data Source = MYSERVER; Initial Catalog = MYDATABASE; User ID = UID; Password = PASSWORD;", "purge.spoDataTable", null);
return list;
}
public class PurgerFactory
{
public IPurger Purger { get; set; }
public PurgerFactory(Parameters parameter)
{
PurgeType type = (PurgeType)Enum.Parse(typeof(PurgeType), parameter.PurgeType);
switch (type)
{
case PurgeType.File:
Purger = new FilePurger(parameter);
break;
case PurgeType.Database:
Purger = new DbPurger(parameter);
break;
default:
throw new NotImplementedException();
}
}
}
/// <summary>
/// Used to submit a job via the job monitor
/// </summary>
public XmlNode JobXMLNode => Common.ConstructXmlNodeFromString("" +
"<JobParams>" +
" <Param Name=\"InfrastructureAPI\" DataType=\"String\">" +
" <Description>Infrastructure API URL.</Description>" +
" </Param>" +
" <Param Name=\"EnvironmentName\" DataType=\"String\">" +
" <Description>The current environment.</Description>" +
" </Param>" +
"</JobParams>",
"JobParams");
}
}
Currently all parts of the program are stuffed into this one single class. I want to separate them out into their own separate classes to make the code much cleaner but still have it function the same. I'm still a beginner coder and don't know the first place to start. Any help would be much appreciated!
You should create a file IPurger.cs for the interface IPurger, then a file FilePurger.cs for the class FilePurger, the file DbPurger.cs for the class DbPurger and lastly PurgerFactory.cs for the class PurgerFactory.
That should clean up your code quite well.
If that enum is used from multiple places, you may want to place it in its own class too, perhaps a generic Enums.cs.
I Have a class Variable and another class Variables.
Variables class is used to maintain and generate the list of objects of the type Variable.
Everything is working ok and i am happy about it. But there is one thing i dont know how to get it done.
i want to use the Variables in foreach loop like this..
Variables oVariables;
foreach(Variable element in oVariables)
But i am not able to do this by just using the object of the class Variables in this example that is oVariable. i have to write some function that will return the Collection.
So is there any way i can avoid writing an extra function and get Collection from that.
Any Help is appreciated. Thanks
Here is the code of Variables Class.
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualBasic;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace APVariable
{
public class Variables
{
private Collection mCol;
public Variable Add(string Name, object Value, string skey = "")
{
Variable objNewMember = new Variable();
objNewMember.Name = Name;
objNewMember.Value = Value;
if (skey.Length == 0)
{
mCol.Add(objNewMember);
}
else
{
try
{
Information.Err().Clear();
mCol.Add(objNewMember, skey);
if (Information.Err().Number != 0)
{
Information.Err().Clear();
mCol.Add(objNewMember);
}
}
catch { Information.Err(); }
{
}
}
return objNewMember;
objNewMember = null;
}
public int count
{
get
{
return mCol.Count;
}
}
//public void Remove(int vntIndexKey)
//{
// //this can be the int or the string.
// //passes the index or the key of the collection to be removed.
// mCol.Remove(vntIndexKey);
//}
public void Remove(dynamic vntIndexKey)
{
//this can be the int or the string.
//passes the index or the key of the collection to be removed.
mCol.Remove(vntIndexKey);
}
public dynamic newEnum
{
get
{
return mCol.GetEnumerator();
}
}
public Variable this[object vIndex]
{
get
{
Variable result = null;
try
{
result = (Variable)mCol[vIndex];
}
catch
{
}
return result;
}
}
//public IEnumerator GetEnumerator()
//{
// //this property allows you to enumerate
// //this collection with the For...Each syntax
// return mCol.GetEnumerator();
//}
public Variables()
{
mCol = new Collection();
}
~Variables()
{
mCol = null;
}
}
}
You'll need to implement the IEnumerable interface. This will allow to be able to use the foreach on your class. You should also use Colection.
It looks like you've started, you're almost there.
namespace APVariable
{
public class Variables : IEnumerable, IEnumerator
{
private Collection<Variable> mCol;
public Variable Add(string Name, object Value, string skey = "")
{
Variable objNewMember = new Variable();
objNewMember.Name = Name;
objNewMember.Value = Value;
if (skey.Length == 0)
{
mCol.Add(objNewMember);
}
else
{
try
{
Information.Err().Clear();
mCol.Add(objNewMember, skey);
if (Information.Err().Number != 0)
{
Information.Err().Clear();
mCol.Add(objNewMember);
}
}
catch { Information.Err(); }
{
}
}
return objNewMember;
objNewMember = null;
}
public int count
{
get
{
return mCol.Count;
}
}
//public void Remove(int vntIndexKey)
//{
// //this can be the int or the string.
// //passes the index or the key of the collection to be removed.
// mCol.Remove(vntIndexKey);
//}
public void Remove(dynamic vntIndexKey)
{
//this can be the int or the string.
//passes the index or the key of the collection to be removed.
mCol.Remove(vntIndexKey);
}
public dynamic newEnum
{
get
{
return mCol.GetEnumerator();
}
}
public Variable this[object vIndex]
{
get
{
Variable result = null;
try
{
result = (Variable)mCol[vIndex];
}
catch
{
}
return result;
}
}
public IEnumerator GetEnumerator()
{
//this property allows you to enumerate
//this collection with the For...Each syntax
return mCol.GetEnumerator();
}
public Variables()
{
mCol = new Collection<Variable>();
}
~Variables()
{
mCol = null;
}
}
}
</code>
</pre>
I'd recommend subclassing Collection or List or implementing IList or ICollection
Looking for design guidelines for the following problem.
I'm receiving two string values - action and message and have to call appropriate method which processes string message (processM1MessageVer1, processM1MessageVer2, processM2MessageVer1...). The method I have to call depends on the given string action. There are 2 versions (but in future there might be more) of each processing method. The version of method I have to call is determined by global variable version. Every method returns object of different type (ResultObject1, ResultObject2...). The result has to be serialized, converted to base64 and returned back.
Is there more elegant way of writing this (eliminate duplicate code, make possible future changes easier, reduce code...):
string usingVersion = "ver1";
public string processRequest(string action, string message)
if (usingVersion == "ver1"){
processRequestVer1(action, message);
}
else{
processRequestVer2(action, message);
}
}
//version 1
public string processRequestVer1(string action, string message){
string result = "";
switch (action){
case "m1":
ResultObject1 ro = processM1MessageVer1(message);
result = serialize(ro);
result = convertToB64(result);
case "m2":
ResultObject2 ro = processM2MessageVer1(message);
result = serialize(ro);
result = convertToB64(result);
case "m3":
ResultObject3 ro = processM3MessageVer1(message);
result = serialize(ro);
result = convertToB64(result);
}
return result;
}
//version 2
public string processRequestVer2(string action, string message){
string result = "";
switch (action){
case "m1":
ResultObject1 ro = processM1MessageVer2(message);
result = serialize(ro);
result = convertToB64(result);
case "m2":
ResultObject2 ro = processM2MessageVer2(message);
result = serialize(ro);
result = convertToB64(result);
case "m3":
ResultObject3 ro = processM3MessageVer2(message);
result = serialize(ro);
result = convertToB64(result);
}
return result;
}
It would be simplier if messages that have to be processed are of different object types instead of strings so that appropriate method could be called polymorphically. The fact that every process method returns different object type also complicates things even more. But these don't depend on me and I cannot change it.
My approach (make it more object oriented, and you should justify whether it's appropriate to create class structure depending on how complex your processing logic is. If your processing logic is only little then maybe this is over-engineering):
For serialize and convert to base 64, I assume you have some logic to do those tasks in a generic way. If not, move those to sub class also
public interface IRequestProcessorFactory
{
IRequestProcessor GetProcessor(string action);
}
public class FactoryVersion1 : IRequestProcessorFactory
{
public IRequestProcessor GetProcessor(string action)
{
switch(action)
{
case "m1":
return new M1Ver1RequestProcessor();
case "m2":
return new M2Ver1RequestProcessor();
case "m3":
return new M3Ver1RequestProcessor();
default:
throw new NotSupportedException();
}
}
}
public class FactoryVersion2 : IRequestProcessorFactory
{
public IRequestProcessor GetProcessor(string action)
{
switch(action)
{
case "m1":
return new M1Ver2RequestProcessor();
case "m2":
return new M2Ver2RequestProcessor();
case "m3":
return new M3Ver2RequestProcessor();
default:
throw new NotSupportedException();
}
}
}
public interface IRequestProcessor
{
string ProcessRequest(string message);
}
public class RequestProcessorBase<T>
{
public string ProcessRequest(string message)
{
T result = Process(message);
string serializedResult = Serialize(result);
return ConvertToB64(serializedResult);
}
protected abstract T Process(string message);
private string Serialize(T result)
{
//Serialize
}
private string ConvertToB64(string serializedResult)
{
//Convert
}
}
public class M1Ver1RequestProcessor : RequestProcessorBase<ResultObject1>
{
protected ResultObject1 Process(string message)
{
//processing
}
}
public class M2Ver1RequestProcessor : RequestProcessorBase<ResultObject2>
{
protected ResultObject2 Process(string message)
{
//processing
}
}
public class M3Ver1RequestProcessor : RequestProcessorBase<ResultObject3>
{
protected ResultObject3 Process(string message)
{
//processing
}
}
public class M1Ver2RequestProcessor : RequestProcessorBase<ResultObject1>
{
protected ResultObject1 Process(string message)
{
//processing
}
}
public class M2Ver2RequestProcessor : RequestProcessorBase<ResultObject2>
{
protected ResultObject2 Process(string message)
{
//processing
}
}
public class M3Ver2RequestProcessor : RequestProcessorBase<ResultObject3>
{
protected ResultObject3 Process(string message)
{
//processing
}
}
Usage:
string action = "...";
string message = "...";
IRequestProcessorFactory factory = new FactoryVersion1();
IRequestProcessor processor = factory.GetProcessor(action);
string result = processor.ProcessRequest(message);
The switch is still there in factory class, but it only returns processor and doesn't do actual work so it's fine for me
First - define interface that suit you best, like this
public interface IProcessMessage
{
string ActionVersion { get; }
string AlgorithmVersion { get; }
string ProcessMessage(string message);
}
Then create as many implementation as you need
public class processorM1Ver1 : IProcessMessage
{
public string ProcessMessage(string message)
{
ResultObject1 ro1 = processM1MessageVer1(message);
var result = serialize(ro1);
result = convertToB64(result);
return result;
}
public string ActionVersion {get { return "m1"; }}
public string AlgorithmVersion {get { return "ver1"; }}
}
public class processorM2Ver1 : IProcessMessage
{
public string ActionVersion {get { return "m2"; }}
public string AlgorithmVersion {get { return "ver1"; }}
public string ProcessMessage(string message)
{
ResultObject1 ro1 = processM2MessageVer1(message);
var result = serialize(ro1);
result = convertToB64(result);
return result;
}
}
public class processorM1Ver2 : IProcessMessage
{
public string ActionVersion {get { return "m1"; }}
public string AlgorithmVersion {get { return "ver2"; }}
public string ProcessMessage(string message)
{
ResultObject1 ro1 = processM1MessageVer2(message);
var result = serialize(ro1);
result = convertToB64(result);
return result;
}
}
Now you need something that know which implementation is best in current context
public class MessageProcessorFactory
{
private MessageProcessorFactory() { }
private static readonly MessageProcessorFactory _instance = new MessageProcessorFactory();
public static MessageProcessorFactory Instance { get { return _instance; }}
private IEnumerable<IProcessMessage> _processorCollection;
IEnumerable<IProcessMessage> ProcessorCollection
{
get
{
if (_processorCollection == null)
{
//use reflection to find all imlementation of IProcessMessage
//or initialize it manualy
_processorCollection = new List<IProcessMessage>()
{
new processorM1Ver1(),
new processorM2Ver1(),
new processorM1Ver2()
};
}
return _processorCollection;
}
}
internal IProcessMessage GetProcessor(string action)
{
var algorithVersion = ReadAlgorithVersion();
var processor = ProcessorCollection.FirstOrDefault(x => x.AlgorithmVersion == algorithVersion && x.ActionVersion == action);
return processor;
}
private string ReadAlgorithVersion()
{
//read from config file
//or from database
//or where this info it is kept
return "ver1";
}
}
It can be use in such way
public class Client
{
public string ProcessRequest(string action, string message)
{
IProcessMessage processor = MessageProcessorFactory.Instance.GetProcessor(action);
return processor.ProcessMessage(message);
}
}