Calculated Properties and Adapter

Posts   
 
    
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 17-Nov-2004 01:19:10   

Hey, all you Adapter gurus...I'm just starting to delve into Adapter as part of a new module I'm working using a distributed architecture. Here's the question:

I'm working on an Inventory module. I have defined an "InventoryItem" object that represents a known part in inventory. I have also defined a "StockTransaction" object that represents the event in which Inventory Items are either pulled from stock, or put into stock. The current stock level of the Inventory Item is represented by the sum of transactions against stock:


CurrentStockLevel = 
SELECT SUM(AmountApplied) 
FROM StockTransaction 
WHERE InventoryItemID = @inventoryItemID

I want to add the CurrentStockLevel as a property of the "InventoryItem" entity. Not a problem in Self-Servicing, but as I am using Adapter against a distributed architecture, what would be the best way to implement this?

There's an example in the documentation, "Extending the Framework Through Inheritance", that seemingly speaks to this, but it looks more like an example for Self-Servicing than Adapter (the example iterates through the OrderDetails collection without first loading it).

I need to have the value for the "CurrentStockLevel" property populated when the entity is fetched from the database, not when the GUI calls into it, otherwise I'll get chatty entities, and no centralized data access (defeating the purpose of Adapter). Any thoughts?

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2004 10:31:35   

To ease your life, use the extended entity templates for adapter, from the 3rd party section. With these, you generate derived entities for each entity. In these derived entities, you add the extra property and the member variable that holds the value.

At the moment, it is not possible to add an expression column to an entity from the designer. So you have to fetch these values manually, when you fetch the entity, as you need an adapter to read the data using a scalar query.

To do that efficiently for a group of entities, you could fabricate a dynamic list with a groupby clause and a fieldcompareset predicate which reads for all entities you're interested in the sum of the stock. Then you merge the data in a small loop with your entities.

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 17-Nov-2004 10:57:20   

Otis wrote:

To ease your life, use the extended entity templates for adapter, from the 3rd party section. With these, you generate derived entities for each entity. In these derived entities, you add the extra property and the member variable that holds the value.

Thanks simple_smile

At the moment, it is not possible to add an expression column to an entity from the designer.

Not a big deal for me that I can't do it in the designer.

So you have to fetch these values manually, when you fetch the entity, as you need an adapter to read the data using a scalar query.

If I extended the InventoryItem entity to include a CurrentStockLevel property, how would I populate that property manually at fetch-time? Via a custom EntityFactory?

Jeff...

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2004 11:21:28   

jeffreygg wrote:

So you have to fetch these values manually, when you fetch the entity, as you need an adapter to read the data using a scalar query.

If I extended the InventoryItem entity to include a CurrentStockLevel property, how would I populate that property manually at fetch-time? Via a custom EntityFactory?

No, in the routine where you fetch the InventoryItem entities. So you create a routine which fetches these entities and also the currentstocklevel. For the time being that's the best option I think.

Frans Bouma | Lead developer LLBLGen Pro
jeffreygg
User
Posts: 805
Joined: 26-Oct-2003
# Posted on: 17-Nov-2004 22:20:44   

Otis wrote:

jeffreygg wrote:

So you have to fetch these values manually, when you fetch the entity, as you need an adapter to read the data using a scalar query.

If I extended the InventoryItem entity to include a CurrentStockLevel property, how would I populate that property manually at fetch-time? Via a custom EntityFactory?

No, in the routine where you fetch the InventoryItem entities. So you create a routine which fetches these entities and also the currentstocklevel. For the time being that's the best option I think.

Ok, thanks Frans. simple_smile

Jeff...