Saving hierarchical group of classes over a web service

Posts   
 
    
dniasoff
User
Posts: 5
Joined: 14-Jun-2009
# Posted on: 14-Jun-2009 19:35:45   

Hi,

I am new to web services so this is probably a stupid question.

I can see in the documentation how to read or save a single entity over a web service.

I need to receive and save a complex entity that spans over around 15 tables but are all related. I could create 15 different methods and save all the entities seperately but that would be messy. Ideally I would like it as a single transaction.

Is this possible. Try not to get too technical with me and an example would be wonderful

Thanks

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Jun-2009 20:43:01   

You could pass an entity with the related objects attached. Or you can pass a list of entitis. Before the example, please post how woud you do right now (I want to see how your entities are, and how are they related).

David Elizondo | LLBLGen Support Team
dniasoff
User
Posts: 5
Joined: 14-Jun-2009
# Posted on: 15-Jun-2009 04:56:56   

Let say I have these two tables

A list of accounts

CREATE TABLE [dbo].[Accounts]( [Id] [uniqueidentifier] NOT NULL, [CreatedDateTime] [datetime] NOT NULL, [Active] [bit] NOT NULL, [CompanyName] nvarchar NOT NULL, [BranchName] nvarchar NULL, [OfficeAddressId] [uniqueidentifier] NULL, [CorrespondenceAddressId] [uniqueidentifier] NULL, [InvoiceAddressId] [uniqueidentifier] NULL, [SalesContactId] [uniqueidentifier] NULL, [InvoiceContactId] [uniqueidentifier] NULL )

And a list of addresses

CREATE TABLE [dbo].[Addresses]( [Id] [uniqueidentifier] NOT NULL, [CreatedDateTime] [datetime] NOT NULL, [Active] [bit] NOT NULL, [BuildingName] nvarchar NULL, [HouseNumber] nvarchar NULL, [Housename] nvarchar NULL, [Street] nvarchar NOT NULL, [District] nvarchar NULL, [Town] nvarchar NULL, [CountyId] [uniqueidentifier] NULL, [CountryId] [uniqueidentifier] NULL }

I want to create a new account with 3 different addresses (OfficeAddressId,CorrespondenceAddressId, InvoiceAddressId) in one call to the web service.

One way I could do it is to do the following

[WebMethod] public Guid SaveAddress(AddressEntity toSave) { using(DataAccessAdapter adapter = new DataAccessAdapter()) {

            Guid addressguid = System.Guid.NewGuid();

            toSave.Id = addressguid;

            adapter.SaveEntity(toSave);

            return addressguid;

        }
    }
}

I would create the 3 different addresses seperately and then populate the main Account entity with thier ID's

But it sounds much to to pass the account entity with all related objects attached. How can I do that?

Thanks very much

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Jun-2009 10:48:07   

[WebMethod] public Guid SaveAddress(AddressEntity toSave) { using(DataAccessAdapter adapter = new DataAccessAdapter()) {

            Guid addressguid = System.Guid.NewGuid();

            toSave.Id = addressguid;

            adapter.SaveEntity(toSave);

            return addressguid;

        }
    }
}

I would create the 3 different addresses seperately and then populate the main Account entity with thier ID's

But it sounds much to to pass the account entity with all related objects attached. How can I do that?

All you have to do is to create the Account object and associate the 3 addresses to its AdrressCollection, and then pass it over to a webMethodd with should accept an AccountEntity, and there all you have to do is to save it recursively.

        [WebMethod]
        public void SaveAccount(AccountEntity toSave)
        {
            using(DataAccessAdapter adapter = new DataAccessAdapter())
            {
                adapter.SaveEntity(toSave, false, true);
            }
        }
dniasoff
User
Posts: 5
Joined: 14-Jun-2009
# Posted on: 15-Jun-2009 11:25:06   

Sorry,

But how do I do that?

I am assuming that if one of the adderesses is null than I would be able to catch that?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Jun-2009 11:42:07   

I'm sorry I don't understand your question.

dniasoff
User
Posts: 5
Joined: 14-Jun-2009
# Posted on: 15-Jun-2009 11:44:55   

The account entity has the option to attach 3 addresses OfficeAddressId, CorrespondenceAddressId and InvoiceAddressId.

I might receive an account entity where only one of the related address entity is used. The CorrespondenceAddressId and InvoiceAddressId might not be sent across and will be null.

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 15-Jun-2009 14:44:32   

I see no problem in this scenario what so ever.

Calling adapter.Save() and passing true as the 3rd parameter (for recursive save) the framework will check for related entities in the same graphofthe passed entity and will manage to save them as well.

Entities not found won't be saved.