IsABitDirty

Posts   
 
    
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 11-Sep-2012 10:09:49   

I have a PersonEntity, an AddressEntity and a PersonAddress join table (with the two FKs plus an AddressType field).

At runtime I want to use databinding to an address of a particular type, say HomeAddress, so I have a property called HomeAddress which looks in the join table for an address of type HomeAddress and returns it if found. If it isn't found, I need to create one on the fly so that databinding will work. This involves a new PersonAddressEntity and a new AddressEntity.

If the user doesn't change anything in the new AddressEntity then its IsDirty flag is false and so won't get saved which is great.

However the intermediate PersonAddressEntity is a problem. If I set its IsDirty flag to false, then it doesn't get saved though its AddressEntity will (if modified). If I leave it as true but the user doesn't make a change to AddressEntity then it bombs because it has no AddressID to use.

Any ideas on what I can do to make PersonAddress.IsDirty 'conditional' on its Address.IsDirty?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-Sep-2012 23:20:59   

Any ideas on what I can do to make PersonAddress.IsDirty 'conditional' on its Address.IsDirty?

Entity X doesn't own an entity which is related to it. So IsDirty of one has nothing to do with the other.

Side note: Why this is a m:n relation, rather than 1:m?

simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 12-Sep-2012 09:13:08   

Entity X doesn't own an entity which is related to it. So IsDirty of one has nothing to do with the other.

That's why I put it in quotes. simple_smile

Side note: Why this is a m:n relation, rather than 1:m?

Because the join table needs to store the type of Address - e.g. Home, Work etc.

What you say is not quite true at runtime/binding time when a new intermediate entity has been created solely to link a new AddressEntity. You either want to save both or discard both hence using Address.IsDirty as the decider. If they are mismatched then saving will either fail or save an orphaned Address.

In the end I opted to just scan the entity graph at Save time to ensure the IsDirty flags match:

            var map = new ReferencedEntityMapAce(Data);

            var newAddressMappings = map.AllEntities.OfType<LegalBodyAddressEntity>().Where(e => e.IsNew && e.Address.IsNew);

            foreach(var newAddressMapping in newAddressMappings)
            {
                newAddressMapping.IsDirty = newAddressMapping.Address.IsDirty;
            }

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 12-Sep-2012 19:41:39   

Glad you have found a workaround.

Btw,

Because the join table needs to store the type of Address - e.g. Home, Work etc.

Type can be set on the m side of the relation, as follows

Person
--------
ID
Name


Address
---------
ID
PersonID
Type (Home/Work/..etc)
Country
City
Street
....
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 13-Sep-2012 09:43:52   

I disagree on that one.

An Address is just that - a building on a street in a town. It has no concept of a Person and no concept of a Type.

If I have a Company, with an employee (a Person) and an Address, then using your method I would end up storing the same address twice - once for the company and once for the Person's Work address.

Cheers Simon

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 13-Sep-2012 12:03:18   

So the intermediate entity has non-PK fields as well? If so, this means it's an 'objectified' relationship: it is an intermediate entity for a m:n relationship, but it's also a separate entity, with its own fields. It's best to work with the 1:n / m:1 and the intermediate entity directly instead of the m:n relationship in that case. (if it's the case as I described above).

Frans Bouma | Lead developer LLBLGen Pro