Factory Pattern for Business Objects

Factory Pattern for Business Objects

Old forum URL: forums.lhotka.net/forums/t/1047.aspx


paresh posted on Monday, August 28, 2006

Hi,

Has anyone implemented factory pattern to instantiate busines objects using CSLA? Just to eloborate: Let's say we have a Project business object. I have another business object called MyProject class that inherits from Projetc business class. When I create a new instance of Project, using "NewProject"static method of Project class, I need to look up in a configuration file to see if if I should instantiate Project class or MyProject class. MyProject class will override some of the business logic in the Project class. So, in short I need a polymorphic behavior in the business objects.

I can do this by using DataPortal's private static object Create(Type objectType, object criteria) method in Project's class NewProject method instead of using generic create method. But this will require changing the CSLA code to make Create(Type objectType, object criteria) public from private in the DataPortal class. My goal is not to make any changes to the CSLA framework code if possible.

I would appreciate if anyone can suggest better alternatives.

Thanks,

Paresh

Fabio replied on Monday, August 28, 2006

Hi.
Don't gaste your time...
Factory is the base funcionality of http://www.springframework.net/
Bye.
Fabio.

RockfordLhotka replied on Monday, August 28, 2006

I think the simplest solution is to use a seperate Factory object - based on either ReadOnlyBase (most efficient) or CommandBase (less efficient, but perhaps more obvious).

To get a project, you'd do this:

x = ProjectFactory.GetProject(mycriteria)

The ProjectFactory class is based on ROB, so it just goes to the app server and runs DataPortal_Fetch. DataPortal_Fetch does whatever work needed to determine which type of project to create, and it creates/loads that object. NOTE that you now have two objects on the server: the ProjectFactory and the specific Project. The new Project object is stored in a private field within ProjectFactory, so it is returned to the client along with the ProjectFactory object.

The GetProject method can then return the value from that private field as a result.

This is not unlike the Exists() command object discussed in the book in Chapter 8 - except that this returns an object rather than a Boolean result.

Copyright (c) Marimer LLC