UnitOfWork (Adapter) could use some help

Posts   
 
    
Ren
User
Posts: 42
Joined: 01-Jul-2005
# Posted on: 27-Jul-2006 01:46:11   

Tables:

Request - 1:n with RequestComments Request - 1:n with Item Item - 1:n with ItemComments

ItemComments has foreign keys to RequestId and ItemId. Item has a foreign key to Request RequestComments has a foreign key to Request

Since I am not using SelfService I could use some help in how to go about this.

I have a form that a user enters in all of this and would like to have it all inserted to the database in one go.

Thanks, Ren

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 27-Jul-2006 03:46:53   

Well you wouldn't need to use the UnitOfWork really unless you are moving between forms to capture all of this data. If you are capturing all of this on one form then you could just use the RequestEntity and recursively save it.

request.Name = txtName.Text; .... ... requestComment1.Comment = txtComment1.Text; requestComment2.Comment = txtComment2.Text; .... request.RequestComment.Add(requestComment1); request.RequestComment.Add(requestComment1); .... Same with items ..... Then the same with ItemComments for the item.

Once you have set all of these values you can call

request.Save(true);

This should save all of your entites in one go.

If this doesn't work you may post a snippet of the code you are currently trying.

Ren
User
Posts: 42
Joined: 01-Jul-2005
# Posted on: 27-Jul-2006 05:54:40   

How would that work? I am not using SelfServicing. I am using Adapter template?confused

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 27-Jul-2006 06:38:54   

The same concept, either use a UnitOfWork, or just use an Entity and fill all its fields and all of its related entities fields as shown before then use DataAccessAdapter torecursivly save the entire entity graph as follows:

adapter.SaveEntity(request, false, true);

Following is the signature of the SaveEntity method used above:


public bool SaveEntity(
   IEntity2 entityToSave,
   bool refetchAfterSave,
   bool recurse
);

Ren
User
Posts: 42
Joined: 01-Jul-2005
# Posted on: 27-Jul-2006 17:39:20   

Thanks Walaa,

I will try the adapter.SaveEntity, but you mention that I could also use a UnitOfWork. How would I do that? Does using adapter.SaveEntity place the queries that are run in a transaction?

Thanks, Ren

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 27-Jul-2006 19:21:53   

Please consult the manual on UnitOfWork objects. SaveEntity() uses a transaction if the entity saved is saved recursively, or if the entity is a subtype of targetperentity, as that results in multiple table access (potentially). If you don't start a transaction yourself, one is thus created for you, otherwise the existing transaction is used.

Frans Bouma | Lead developer LLBLGen Pro
Ren
User
Posts: 42
Joined: 01-Jul-2005
# Posted on: 27-Jul-2006 20:32:43   

Cool, thanks for the info. I have changed some keys from natural to identity, (legacy application) and will try again. I'll let you know if that works.

-Edit-

Yup that works!

Thanks everyone for your help.