ObjectFactoryAttribute

ObjectFactoryAttribute

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


jmcd posted on Thursday, September 11, 2008

I have been trying to get an implementation of the ObjectFactoryAttribute working (based on Rocky's blog post http://www.lhotka.net/weblog/CSLANETObjectFactoryAttribute.aspx).

Unfortunately, I'm having trouble with the following:
1. How does the Create method create a new instance of a BusinessObject without calling a public constructor or calling the static Create method (which would recall the object factory resulting in an infinite loop)?
2. In the Fetch method, how do you populate a business object with values obtained from the DataLayer (as these fields are private)?

sergeyb replied on Thursday, September 11, 2008

I guess it all depends why you are following factory pattern.
1. Activator class should create an instance of your object in .NET even if it only has a private constructor.
2. I think you would need to create an internal method in your class that would accept field values and populate them, or you can simply set the public properties, which is easier. When you are done, just mark object as old - you have a helper method for that in base factory class as in MarkOld(target) where target is an instance of your class.

Sergey Barskiy
Principal Consultant
office: 678.405.0687 | mobile: 404.388.1899
Magenic ®
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation
-----Original Message-----
From: jmcd [mailto:cslanet@lhotka.net]
Sent: Thursday, September 11, 2008 8:16 AM
To: Sergey Barskiy
Subject: [CSLA .NET] ObjectFactoryAttribute

I have been trying to get an implementation of the ObjectFactoryAttribute working (based on Rocky's blog post http://www.lhotka.net/weblog/CSLANETObjectFactoryAttribute.aspx).

Unfortunately, I'm having trouble with the following:
1. How does the Create method create a new instance of a BusinessObject without calling a public constructor or calling the static Create method (which would recall the object factory resulting in an infinite loop)?
2. In the Fetch method, how do you populate a business object with values obtained from the DataLayer (as these fields are private)?

SonOfPirate replied on Friday, September 12, 2008

The simplest solution to both questions is an internal constructor on your business object that accepts a data reader or whatever you are using with data access.  You could optionally have the constructor accept a dictionary so that it isn't tied to a specific data access technology.  I've extended DataMapper to include methods to map a data reader to a dictionary for just such a purpose.

The approach Sergey describes is certainly valid as well.  Here you could have a private constructor (or protected if you see the next for extensibility) that is used by the Activator.  Then have an internal  or protected internal (or even public) Initialize method that accepts a dictionary or NameValueCollection of property/value pairs to populate the object.

Be careful of the scope you choose depending on whether the factory is in the same assembly or not.

HTH

 

Copyright (c) Marimer LLC