automatic update of related entity?

Posts   
 
    
Posts: 2
Joined: 03-Jan-2008
# Posted on: 07-Jan-2008 14:23:05   

We data-bound Entity field to a text box. How can we achieve automatic (on-the-fly) update of related entity?

details: we have some input boxes where user can enter values. after they enter value, a box next to input box should show some attributes of related entity (PK of which user has entered).

where would be a right place to re-fetch Entity?

we already have derived class with additional property which shows related entity's attribute.

we tried: a) write custom Binding class to Bind Foreign entity directly to text box (OnParse and OnFormat convert from string to entity and back)

b) set related entity in custom IValidator

but both had quirks and I believe there must be better way.

We are using: - LLBGEN 2.5 final. - DataAdapter templates - Sql Server 2005 Compact Edition - .NET Compact Framework v2 SP2

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 08-Jan-2008 03:48:57   

As there is may places you can do that, I would recommend you do it in the GUI as is the natural place to do that kind of things.

You also can wirte handlers for some field and entity events. For that please review the Reference Manual to see what events are available.

David Elizondo | LLBLGen Support Team
Posts: 2
Joined: 03-Jan-2008
# Posted on: 08-Jan-2008 12:46:42   

Before posting the question, we did check reference manual, but there is no exact example for doing that.

As for example:

We have a tables "customer" and "customer_order"

table "customer" has two fields:

Id_customer PK, Name

table "customer_order" has three fields:

Id_order PK, Id_customer, Quantity.

There exist a foreign key customer_order->customer on field Id_customer.

On the form which we are using for adding a new record to the table (OR changing existing record) "customer order" we want to type Id_customer which should be checked on the fly against table "customer" (if exist such a record).

If record doesn't exist, nothing should be shown in below Name field.

If the record exist there should be a text field below entering field which displays on the fly corresponding Name from the "customer" table.

**When you open Id_order object on gui with corresponding prefetch values from "customer" table, it works untill you change the value Id_customer and prefetch is not able to dynamicaly provide corresponding Name from table "customer".

So, the problem lies in opening object Id_order (llblgen object) and changing/checking/editing corresponding objects from "customer" table values on the fly (on GUI) without commiting anything to the database. **

Of course, we know how to do it using GUI controls and C#, but we want to use llblgen events and objects.

Can you, PLEASE, provide a simple example how to do that?

Best regards,

Andrej

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 08-Jan-2008 13:08:34   

Thus has nothing to do with prefetchPaths, PrefetchPaths are used to fetch related entities from the database. But your change of the FK field is just a change in UI that's not yet propagated to the database.

we want to type Id_customer which should be checked on the fly against table "customer" (if exist such a record).

You should handle the textBox textChanged event, and use the DataAccessAdapter to fetch a new entity for the Customer.Order entity.

OrderEntity newOrder = new OrderEntity(newID);
adapter.FetchEntity(newOrder);

customer.Order = newOrder
ak77
User
Posts: 1
Joined: 08-Jan-2008
# Posted on: 08-Jan-2008 14:35:39   

You should handle the textBox textChanged event, and use the DataAccessAdapter to fetch a new entity for the Customer.Order entity

.

so no use of binding? no use of IAudit and/or IValidator?

Data integrity checking shouldn't be GUI's job, it just passes in values and should get a) related entity set or b) error in error field

All we wanted is that when we set IdEntityA to have related EntityA fetched if EntityA with PK IdEntityA exists or to set Error for IdEntityA.

There obviously must be another Fetch somewhere. But I don't want to put it into GUI.

Markiemac
User
Posts: 132
Joined: 25-Apr-2006
# Posted on: 08-Jan-2008 15:01:28   

ak77 wrote:

You should handle the textBox textChanged event, and use the DataAccessAdapter to fetch a new entity for the Customer.Order entity

.

so no use of binding? no use of IAudit and/or IValidator?

Data integrity checking shouldn't be GUI's job, it just passes in values and should get a) related entity set or b) error in error field

All we wanted is that when we set IdEntityA to have related EntityA fetched if EntityA with PK IdEntityA exists or to set Error for IdEntityA.

There obviously must be another Fetch somewhere. But I don't want to put it into GUI.

I don't think Walaa is suggesting that you put this in the Gui. The idea is that you handle events in the Gui to fetch required data via another layer. Validators are also placed elsewhere and will automatically notfy the Gui of errors.

The User Guide + this site contains many, many ideas and examples which will help. Also don't forget the reference manual.simple_smile

tangent
User
Posts: 41
Joined: 30-Apr-2006
# Posted on: 09-Jan-2008 06:03:52   

Have you tried using self servicing templates rather than adapter? This kind of functionality is baked into selfservicing out of the box.

Another thing you can do, which I use in my projects, is look at the templates for both self servicing and adapter and customize the adapter templates to have some (optional) fetch on demand facilities similar to self-servicing.

I actually do this in another layer though which is based off the manager templates and ideas from self-servicing templates, rather than in the entities themselves. This way you get all the flexibility of adapter model and all the transparency of self-servicing, and you can use either approach depending on the scenario, the best of both worlds simple_smile

If you are new to llblgen that may be a bit over your head, but if you are intent on using adapter I would definitely recommend you look into the manager templates which are posted on this forum and practice using the template studio.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Jan-2008 10:41:59   

If you want, you can use OnValidateFieldValue of the IValidator, to validate the value set to the FK, and in there you can use the code I've used earlier. Thus doing the Refrential Integrity in the Validator Class, and fetching the new related entity if exists.

Check the manual's section Using the generated code -> Validation per field or per entity