Extended EntityFactory

Posts   
 
    
TommyKeane
User
Posts: 8
Joined: 07-May-2006
# Posted on: 12-Jun-2006 18:30:29   

Hi,

I have been reading Frans' post at : http://weblogs.asp.net/fbouma/archive/2006/06/09/LLBLGen-Pro-v2.0-with-ASP.NET-2.0.aspx.

If I create an extended entity factory that overrides CreateFields(), how do I instruct LLBLGEN to use it instead of the base class (ie. to use ExtendedCustomerEntityFactory instead of the generated CustomerEntityFactory).

I will not be using the new LLBLGenProDataSource2 control, I'll be calling the DataAdapter directly.

Thanks, Tommy

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 12-Jun-2006 22:39:21   

Set the EntityCollection's property EntityFactoryToUse to the factory class with your extended code simple_smile (or pass it to the entitycollection's constructor)

Frans Bouma | Lead developer LLBLGen Pro
TommyKeane
User
Posts: 8
Joined: 07-May-2006
# Posted on: 12-Jun-2006 23:01:31   

Otis wrote:

Set the EntityCollection's property EntityFactoryToUse to the factory class with your extended code simple_smile (or pass it to the entitycollection's constructor)

Thanks for the reply. I am only fetching one entity from the database:

The CountyEntity has been extended with a Count property:

public partial class CountyEntity {
        public Nullable<int> DealerCount {
            get {
                object value = this.Fields[this.Fields.Count - 1].CurrentValue;
                if (value != null) {
                    return Convert.ToInt32(value);
                } else {
                    return null;
                }
            }
        }
}

and the EntityFactory has been extended to add the new count field:

public class ExtendedCountyEntityFactory : CountyEntityFactory {
        public override IEntityFields2 CreateFields() {
            IEntityFields2 toReturn = base.CreateFields();
            toReturn.Expand(1);
            IEntityField2 scalarField = new EntityField2("DealerCount",
                    new ScalarQueryExpression(CountyFields.CountyId.SetAggregateFunction(
                        AggregateFunction.Count),
                    (SellerFields.CountyId == CountyFields.CountyId)));
            toReturn.DefineField(scalarField, toReturn.Count - 1);
            return toReturn;
        }
    }

Where in the following code can I set the EntityFactory to use?

DataAccessAdapter adapter = new DataAccessAdapter();
CountyEntity county = new CountyEntity(2);
adapter.FetchEntity(county);
TommyKeane
User
Posts: 8
Joined: 07-May-2006
# Posted on: 12-Jun-2006 23:26:27   

Just to add to the above, I can pass the extended factory to the contructor of the EntityCollection:

 EntityCollection<CountyEntity> counties = new EntityCollection<CountyEntity>(new ExtendedCountyEntityFactory());

That works great, and the entities in the collection now have the custom count property.

How do set the EntityFactory for a single entity? The entity does not accept an EntityFactory in its constructor.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39928
Joined: 17-Aug-2003
# Posted on: 13-Jun-2006 00:07:00   

That's trickier. You then either should fetch it inside an entitycollection, or simply set the Fields object to the adjusted Fields object, inside the override of OnInitialized. Though first expand the current Fields object to make them have the same # of fields simple_smile

Frans Bouma | Lead developer LLBLGen Pro