StrataFrame Forum

Factory methods

http://forum.strataframe.net/Topic14980.aspx

By Ertan Deniz - 3/17/2008

In Strataframe documentation, I couldn't find any related information about this topic ?

Could you give us some information ?

By StrataFrame Team - 3/18/2008

There isn't really any information about factory methods in the documentation.  But, creating one would be really simple.  You would want to make the method shared/static, create a new instance of the business object, populate it with the required data, and return it.

public static MyBO SomeFactoryMethod(string param1)
{
    MyBO bo = new MyBO();

    //-- fill the bo

    return bo;
}

By Ertan Deniz - 3/18/2008

From your Web Site ;

"Factory Methods -– Constructors are provided by the BusinessLayer base class that allows the developer to implement custom factories or shared factory methods directly on the business object class."

What is the advantage of Factory Methods ? Could you give me a little bit further information ?

By Greg McGuffey - 3/18/2008

I obviously can't speak to what is on the SF web site, but I can let you know how I use factory methods.



Typically I use them in one of two situations:



- There are bunch of ways that an object can be created and constructor overloads are either confusing or don't work (because the arguments wouldn't be unique). In this case it is much easier to create shared (static) factory methods that have clear names and precise arguments. I use it a lot with Forms. I.e. I have a form that can be opened with a single record, or in data entry mode, or with all records that match some criterion and the current record being specified. I could have constructors something like:

public MyForm() {}

public MyForm(int id) {...}

public MyForm(bool dataEntry) {...}

public MyForm(int parentID, int id) {...}




But this is confusing and I have trouble remembering all the permutations. So, instead I would create a set of factory methods:

public static MyForm CreateSingleEdit(int id) {...}

public static MyForm CreateDataEntry() {...}

public static MyForm CreateParentEdit(int parentID, int idToNav) {...}





Now I have no limitation on how complex the constructor is and I have help remembering how I have setup a class.



Hope that provides a bit of clarity. I'm sure others have more/better examples, but I thought I'd get the ball rolling! BigGrin
By Ertan Deniz - 3/18/2008

Thanks