I have a class, which returns three properties. First property is dependent on some parameter, second one is dependent on the first property and third one is dependend on the second property.
What is the best way, to implement this type of class, is there a appropriate design pattern for this one? Below I pasted two versions of my code, which both work and I wonder which of them is better (or am I complicating things too much)?
First one:
class Initializer
{
private string lastCode;
private int lastPackage;
private int lastBox;
public Initializer(int machineNumber)
{
lastCode = GetLastCodeFromDatabase(machineNumber);
lastPackage = GetLastPackageByLastCode(lastCode);
lastBox = GetLastBoxByLastPackage(lastPackage);
}
public string LastCode
{
get { return lastCode; }
}
public int LastPackage
{
get { return lastPackage; }
}
public int LastBox
{
get { return lastBox; }
}
private string GetLastCodeFromDatabase(int machineNumber)
{
using (InitializerTableAdapter adapterGetLastCode = new InitializerTableAdapter())
{
return Convert.ToString(adapterGetLastCode.GetLastCodeByMachineNumber(machineNumber));
}
}
private int GetLastPackageByLastCode(string lastCode)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastPackageByLastCode(lastCode));
}
}
private int GetLastBoxByLastPackage(int lastPackage)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastBoxByPackageNumber(lastPackage));
}
}
}
Second:
class Initializer
{
public static string LastCode(int machineNumber)
{
return GetLastCodeFromDatabase(machineNumber);
}
public static int LastPackage(string lastCode)
{
return GetLastPackageByLastCode(lastCode);
}
public static int LastBox(int lastPackage)
{
return GetLastBoxByLastPackage(lastPackage);
}
private static string GetLastCodeFromDatabase(int machineNumber)
{
using (InitializerTableAdapter adapterGetLastCode = new InitializerTableAdapter())
{
return Convert.ToString(adapterGetLastCode.GetLastCodeByMachineNumber(machineNumber));
}
}
private static int GetLastPackageByLastCode(string lastCode)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastPackageByLastCode(lastCode));
}
}
private static int GetLastBoxByLastPackage(int lastPackage)
{
using (InitializerTableAdapter adapterGetLastPackage = new InitializerTableAdapter())
{
return Convert.ToInt32(adapterGetLastPackage.GetLastBoxByPackageNumber(lastPackage));
}
}
}