Thanks both for that confirmation.
I've started having a go at this and had to jump through hoops trying to get the schema in a generic way.
Heres my (test) m:m relationship
Asset <-> AssetToOwner <-> LegalBody
Taking one side of this, Asset, I have two collections I'm interested in:
.AssetOwnerships (which is the 1:m with AssetToOwner)
.Owners (which is the m:m and is a collection of LegalBody)
Ultimately I want to just provide the m:m collection to my new class.
new ManyToManyUpdater(asset.Owners);
(but for now I'm also passing in asset.Ownerships 'cos I can't work out if/how they are related)
The theory is simple:
In the ctor:
1) Get the entity that hold the m:n collection.
2) Get the collection to the intermediate entities (.AssetOwnerships in this case)
3) Read all the intermediate entities in .AssetOwnerships and find the LegalBodyEntity (ie the 'other' side' - copy this into .Owners (remembering to set IsReadOnly = false temporarily.
At this point, .Owners can be bound to a Grid and will show the list of LegalBody entities. The DefaultView will be fine or I could use the TypedEntityView I mentioned in another thread if I wish to filter by entities derived form LegalBody.
4) I then listen to changes on .AssetOwnerships and make the same changes in .Owners: If an AssetToOwner entity gets added then I add AssetToOwner.LegalBody to Owners. If an AssetToOwner entity gets removed then I remove the corresponding entity from owners (the EntityRemoved event has the InvolvedEntity - nice one!). I don't need to worry about changes to the LegalBody entity itself - those changes will be pick up by property bindings.
I also don't need to worry about persistence because the collection I am modifying is ignored - hurrah! So that it is, hopefully.
What you could help me with is the best way of accessing the metadata:
For Step 1: I have partial class for EntityCollection<TEntity> and I added a GetContainingEntity() method which just calls the protected IEntity2 ContainingEntity property. This works but why is this protected though??
For Step 2: This is the biggest problem and I cheat for now by just passing it in. Does the metadata reflect that for a m:m collection, the 1:m collection it is indirectly 'related' with?
For Step 3: Hmm. My method works but there must be a better way. Here is what I came up with (don't laugh) :-
IEntity2 GetOtherEntityFromIntermediateEntity(IEntity2 intermediateEntity)
{
var data = intermediateEntity.GetRelatedData();
return (IEntity2) data.Values.Single(o => o != owningEntity);
}
I thought that for a given intermediate entity, it would store the two entities on either side. Well I just want the one that isn't the one I started with. (it is assumed and a requirement that the entities are present and not just their IDs for this to work).
Is there a better way than this?