views:

107

answers:

3

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));
        }
    }
}
+1  A: 

Depends if you want to initialize all the variables up front, or execute it only when needed. I think the best way to do it totally depends on how you expect to use the class. If you will use the properties a lot, and the value wont change, perhaps do the initialization(your first option), or do Lazy Loading.

On the other hand if your properties will not be used very much, maybe once or twice, or the values returned from the methods will vary from one call to the next, the second option will be best. If the value from the function changes a lot, you can't really do initialization anyway and get the expected results.

Lazy Loading is really a good compromise between the two ideas, but can only be used if the values don't change. When you Lazy Load, if nobody uses the properties, you don't have to pay the performance of initializing the variables.

Kekoa
With help of this class I set up GUI of my application on start up, so it will run only once and I need to get all properties at once.
_simon_
A: 

One version of your code uses cached values whereas the other retrieves the values directly from the underlying data source. First has better performance because the roundtrip to the data source is only performed once, the latter has always the most actual data. I think in your case it depends on the usage of the class.

Beside that I wonder why you have such a flat view on your domain model. It seems somehow unusual to me.

Oliver Hanappi
What do you mean with 'flat view on my domain model'? I'm new to design patterns, that's why I ask such questions...
_simon_
Kevin goes a little bit more into detail. You should have two classes: Package and Box. Package has an IList<Box> to store its boxes. An O/R-Mapper like NHibernate/Entity Framework should take care of the persistence of your classes. You would then use in memory methods to determine the lastest box. With flat view I mean that you have only a very specific view on some properties of your entities (packages, boxes) and you will run easily into problems when you need some more information. Another thing: You should abstract your data access layer in order to be able to test your code ;)
Oliver Hanappi
+2  A: 

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).

Kevin Peterson