I am creating a very small application to demonstrate solid principles and also a brief implementation of a builder pattern, does anyone have any feedback as to how this could be improved or how it breaks the solid principles? The app is a small app that just converts units, e.g. yards to meters, inches to cms.
Interface
public interface IConverter
{
double ConversionRate { get; set; }
string[] ConvertedUnits { get; set; }
string DisplayText { get; set; }
string[] Convert(string input);
}
Class implementing interface
public class Converter : IConverter
{
public double ConversionRate { get; set; }
public string[] ConvertedUnits { get; set; }
public string DisplayText { get; set; }
public Converter(double conversionRate, string[] convertedUnits, string displayText)
{
ConversionRate = conversionRate;
ConvertedUnits = convertedUnits;
DisplayText = displayText;
}
public string[] Convert(string input)
{
if (!input.Contains('\n'))
{
double d = double.Parse(input, CultureInfo.InvariantCulture) * ConversionRate;
string[] array = new string[1];
array[0] = d.ToString();
return array;
}
else
{
double[] doubles = new double[input.Split('\n').Count()];
for (int i = 0; i < input.Split('\n').Count(); i++)
{
double value = double.Parse(input.Split('\n')[i]);
doubles[i] = value;
string.Format("{0}", value * ConversionRate);
}
string[] strings = new string[doubles.Length];
for (int i = 0; i < input.Split('\n').Length; i++)
{
strings[i] = string.Format("{0}", doubles[i] * ConversionRate);
}
return strings;
}
}
}
Builder (abstract class)
public abstract class ConverterBuilder
{
protected double _conversionRate;
protected string[] _convertedUnits;
protected string _displayText;
public ConverterBuilder AddConversionRate(double conversionRate)
{
_conversionRate = conversionRate;
return this;
}
public ConverterBuilder AddConversionRate(string[] convertedUnits)
{
_convertedUnits = convertedUnits;
return this;
}
public ConverterBuilder AddDisplayText(string displayText)
{
_displayText = displayText;
return this;
}
public Converter Build()
{
return new Converter(_conversionRate, _convertedUnits, _displayText);
}
}
Builder implementing abstract builder class
public class UnitConverterBuilder : ConverterBuilder
{
public UnitConverterBuilder()
{
}
}
Controller
public IActionResult Convert(string text, double conversionRate)
{
Converter converter = new UnitConverterBuilder()
.AddConversionRate(conversionRate)
.Build();
string output = "";
for (int i = 0; i < converter.Convert(text).Count(); i++)
{
output += converter.Convert(text)[i] + Environment.NewLine;
}
return Content(output);
}
View
Enter values to convert
<form asp-controller="Home" asp-action="Convert" method="post">
<textarea id="text" name="text" rows="10" cols="40"></textarea>
<select name="conversionRate">
<option value="">Please Select</option>
<option value="0.9144">Yards to Meters</option>
<option value="2.54">Inches To Centimeters</option>
</select>
<button>Convert</button>
</form>
The app is built using .net core, any feedback greatly appreciated :)
These are not necessarily related to SOLID principle but here are a few things I would mention if I was reviewing this code at work
'/n'
. If you wanted to change from '/n'
to '/r/n'
you would have to change it 5 times in Converter.Convert
. A better way to handle this would be to store this in a variable or perhaps allow it to be set via the constructor.var
instead of explicitly stating the variable type e.g var d = double.Parse(input, CultureInfo.InvariantCulture) * ConversionRate;
I hope this helped :)