Convert into another SubType

Posts   
 
    
Posts: 21
Joined: 28-Jul-2008
# Posted on: 20-Mar-2009 06:45:14   

Hi ! I have one logical problem:

TABLES: REALTY REALTY_FLAT REALTY_FLAT_NEW REALTY_HOUSE

all tables has REALTY_ID and connect by 1:1 in LLBLGEN mapper we create syb-types:

REALTY REALTY_FLAT (syb-type of REALTY) REALTY_FLAT_NEW (syb-type of REALTY_FLAT) REALTY_HOUSE (syb-type of REALTY)

We can create entities of any types and fetch they - all work fine. Problem: We can't convert between this types.

example:

RealtyEntity realtyEntity1; adapter.Fetch(realtyEntity1);

how I can convert realtyEntity1 to RealtyFlatEntity type ? If I insert record into REALTY_FLAT table with same REALTY_ID and refetch realtyEntity1 - they will be RealtyFlatEntity, but how I can do this without direct insert into database - from code ?

P.S. Sory for my bad English.

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 20-Mar-2009 09:41:29   

In inheritance hierarchies, you can't directly convert between types. If you want to work around this you will have to INSERT, or DELETE and INSERT if they are sibblings.

Posts: 21
Joined: 28-Jul-2008
# Posted on: 20-Mar-2009 10:05:30   

Walaa wrote:

In inheritance hierarchies, you can't directly convert between types. If you want to work around this you will have to INSERT, or DELETE and INSERT if they are sibblings.

This is mean what I can't do this using LLBLGen (adapter mode) - and need create separate connection to DB and perform manual INSERT/DELETE ? Not exist more effective way without separate connection and manual insert ? Possible create some thing like HELPER for LLBLGen for perform this operations (in current structure of LLBLGen runtime) ?

Walaa avatar
Walaa
Support Team
Posts: 14983
Joined: 21-Aug-2005
# Posted on: 23-Mar-2009 09:25:05   

This is mean what I can't do this using LLBLGen (adapter mode) - and need create separate connection to DB and perform manual INSERT/DELETE ?

You got me wrong. This should be done with LLBLGen. What I ment to say there is no convert type functionality, instead you should use normal LLBLGen methods to INSERT a record in a subType table with the same ID as that one in the SuperType table.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39749
Joined: 17-Aug-2003
# Posted on: 26-Mar-2009 10:33:05   

exception.cpp wrote:

Hi ! I have one logical problem:

TABLES: REALTY REALTY_FLAT REALTY_FLAT_NEW REALTY_HOUSE

all tables has REALTY_ID and connect by 1:1 in LLBLGEN mapper we create syb-types:

REALTY REALTY_FLAT (syb-type of REALTY) REALTY_FLAT_NEW (syb-type of REALTY_FLAT) REALTY_HOUSE (syb-type of REALTY)

In inheritance, you've to look at what makes an entity type an entity type and not a state of an entity. If I look at your type hierarchy, I see 'REALTY_FLAT_NEW'. Being 'new' is a state: after a while the flat isn't new anymore and therefore should change type. But that's not possible: instances can't change type: you can't do:

Object o = new Object();

and then change o into a string, even though string is a subtype of o: o isn't a string, so it can never become a string. Data inside a table are entity instances. A row in REALTY is an entity instance of type REALTY. You can't change that to a subtype. Sure, physically you can by adding data to a table, but that's not correct, semantically, therefore we don't support it. it's tempting to do, but it has consequences in edge cases and it's also not correct semantically, which in the end will lead to bugs which are hard to track down.

So drop the REALTY_FLAT_NEW as a subtype of REALTY_FLAT, and instead make it a related entity with information about REALTY_FLAT entities which are new. Once they're not new anymore, you can delete their REALTY_FLAT_NEW entity and you're ok.

We can create entities of any types and fetch they - all work fine. Problem: We can't convert between this types.

example:

RealtyEntity realtyEntity1; adapter.Fetch(realtyEntity1);

how I can convert realtyEntity1 to RealtyFlatEntity type ? If I insert record into REALTY_FLAT table with same REALTY_ID and refetch realtyEntity1 - they will be RealtyFlatEntity, but how I can do this without direct insert into database - from code ? P.S. Sory for my bad English.

You can't do this, and also shouldn't do this. Something which is a FLAT should be created as such.

Frans Bouma | Lead developer LLBLGen Pro
Posts: 21
Joined: 28-Jul-2008
# Posted on: 26-Mar-2009 11:15:03   

Otis wrote:

In inheritance, you've to look at what makes an entity type an entity type and not a state of an entity. If I look at your type hierarchy, I see 'REALTY_FLAT_NEW'. Being 'new' is a state: after a while the flat isn't new anymore and therefore should change type. But that's not possible: instances can't change type: you can't do:

Sorry for mess - my English still very bad rage

REALTY REALTY_FLAT REALTY_FLAT_NEW REALTY_FLAT_SECONDARY REALTY_FLAT_COMMUNAL

all this is entities have some properties: REALTY contains common properties (like PRICE or SQUARE), REALTY_FLAT have additional properties (FLOOR etc), REALTY_FLAT_NEW contains in additional some specific props (like BUILDING CONSTRUCTION END DATE). They are hierarchical structure. And for example I create entity: var entity = new Realty(); // All is OK adapter.Save(entity, true); .... // now I known what entity is realy not REALTY - but REALTY_FLAT_NEW, try convert: entity = (RealtyFlatNew)entity; entity.ConstructionDateeEnd = xxx; adapter.Save(entity, true);

also possible situation when need convert back from REALTY_FLAT_NEW to REALTY.