Adding Entity to m:n collection

Posts   
 
    
Maciek
User
Posts: 23
Joined: 28-Aug-2008
# Posted on: 05-Sep-2008 10:18:55   

Hi,

I'd like to add entity to collection of entities owned by other entity (multi to many relation).


DocumentEntity doc = new DocumentEntity
            {
                Id = 0,
            };

doc.Save();

DataEntity meta = new MetadataEntity
{
                Id = 0
};

doc.DataCollectionViaDataInDocument.Add(meta);
doc.DataCollectionViaDataInDocument.SaveMulti();
//doc.save(true); This dosn't work either

This code saves both Data and the Document but dosn't connect them by adding row in DataInDocument table.

What's the best approach on this? Do i have to manualy create DataInDocumentEntity?

Thanks in advance simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 05-Sep-2008 10:25:28   

What's the best approach on this? Do i have to manualy create DataInDocumentEntity?

Yes you have to. Please check this thread

Maciek
User
Posts: 23
Joined: 28-Aug-2008
# Posted on: 08-Sep-2008 07:37:09   

Is there a reason for this? Why do we have to create connection manualy? Does it have any undesirable effects?

I'd like to override OnSaveComplete() and put connection code in there. Any reasons why i shouldn't?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 08-Sep-2008 11:21:08   

Is there a reason for this? Why do we have to create connection manualy? Does it have any undesirable effects?

The following thread explains it: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=10404

I'd like to override OnSaveComplete() and put connection code in there. Any reasons why i shouldn't?

I think that's do-able.

Maciek
User
Posts: 23
Joined: 28-Aug-2008
# Posted on: 08-Sep-2008 11:35:46   

Is there any way i can check weather OnSaveComplete() was invoked due to Save(false) or Save(true)?

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 08-Sep-2008 11:41:23   

I don't think it's possible.

If you are gonna put some code in there to create the middle entity and save it. Why don't you wrap this into a BL method? Which should call each entity save routine and then create and save the middle entity.

Maciek
User
Posts: 23
Joined: 28-Aug-2008
# Posted on: 08-Sep-2008 12:15:32   

What's a BL method? simple_smile

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 08-Sep-2008 16:15:02   

A business layer method, what I mean from this: a routine method that's out of the Data access layer (i.e. rather than overwriting the OnSaveComplete() )

Something to wrap the code shown in the previously mentioned thread. So you can call it anywhere from your code.

Maciek
User
Posts: 23
Joined: 28-Aug-2008
# Posted on: 08-Sep-2008 17:53:03   

Walaa wrote:

A business layer method

Oh, then that's what i did originally. I made a CompleteSave method that connects this two entities and executes some custom logic. The downside of this approach is that module's user can still invoke Save method ... which may and will crash this system. I think the best way to do it is to override Save, but it's not virtual disappointed

Then i thought about OnSaveComplete but since i can't check which Save was called it won't work either.

I guess i'll stay with BL method as it's the least evil (yet evil) approach wink