Search code examples
c#class-design

How to design my class


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));
        }
    }
}

Solution

  • The other responses have answered the mechanics of you question, but there's also a design issue that I would be remiss if I didn't point out.

    If your first example is the one that better fits what the data means, you would be better off using a simple object that knows nothing about how it's stored, and an ORM to map it to and from the database. Presumably, you are going to have other code that uses this object. If you write that code expecting this class, querying the database in a constructor and so on, it will be hard to test and hard to reuse.

    If the second is the better match, you should look into dependency injection, so that you can inject a different datasource for testing (or for when you change from a SQL backend to a local database, etc).