WCF Update method that saves to several datatables

2 posts, 0 answers
  1. Jörgen
    Jörgen avatar
    10 posts
    Member since:
    Apr 2013

    Posted 22 Apr 2013 Link to this post

    I have a testproject containing an N-Tier solution using WCF Plain Service and OpenAccess ORM Free Edition.

    I have created a Windows Form that uses Four Data tables. My WCF Service returns BindingLists that I have databound my Controls to.

    On the Form I have an Save Button. When I press it I want to call an Update Method in the WCF Service where I pass along my four Bindinglists. When It gets down to the Business Logic Layer I want to create a transaction and save the Changes in the four database tables.

    How do I accomplish this?

    The table SysAnvandare is the parent table and the tables SysAnvInstallning and SysAnvRoller are Child tables and the database has Foreign Key constraints between them.

    I need help to write the code that uses DataAccess ORM to save these tables to the database.

    In My Service Class I have defined this method:

    Public Sub UpdateSysAnvandare(dtoSysAnvandare As SysAnvandareDto, dtoSysAnvInstallning As SysAnvInstallningDto, dtoSysAnvRoller As SysAnvRollerDto



    End Sub

  2. Dimitar Tachev
    Dimitar Tachev avatar
    67 posts

    Posted 25 Apr 2013 Link to this post

    Hi Jörgen,

    You could update these related entities with the WCF Plain Service generated by the OpenAccess ORM Add OpenAccess Service wizard by specifying a custom Assemble methods for populating all of the navigation properties of your parent entity (the SysAnvandare one).

    In this way when you update or get a SysAnvandare object, all of its child objects will be also properly updated in the same transaction without implementing any additional methods.

    In order to achieve that you need to create a partial class of your SysAnvandareAssembler and override the Assemble method for converting a Entity to Dto like below:

    Namespace Assemblers
        Partial Public Class SysAnvandareAssembler
            Public Overrides Function Assemble(ByVal entity As SysAnvandare)
    As SysAnvandareDto

                Dim _SysAnvandareDto As SysAnvandareDto = MyBase.Assemble(entity)
                ' Get all of the navigation properties along with the DTO
                Me.AssembleNavigational(entity, _SysAnvandareDto)
                Return _SysAnvandareDto
            End Function
        End Class
    End Namespace

    Then in order to update all of the entities with the same service call you need also to overload the Assemble method which is converting the DTO to Entity.

    The implementation of this method is depending on your tables relationships (1:1, 1:M or M:M) and for your convenience I prepared a sample application demonstrating this approach based on the Products table of the Northwind database - please find it attached. 

    In the example you could find how to get and update all of the product related entities (its OrderDetails collection, Supplier and Category) with only one service call - please find the applied changes in the ProductAssembler.partial.vb file. Also there is a sample example of the service usage in the ServiceConsumer application.

    You could find more information about handling relationships in the WCF Plain services in this documentation section. Also you could take a look at the article describing the transactions handling by OpenAccess ORM.

    I hope this is applicable for you. Do not hesitate to contact us back if you need any further clarifications.

    Dimitar Tachev
    the Telerik team
    Using Encrypted Connection Strings with Telerik OpenAccess ORM. Read our latest blog article >>
  3. DevCraft banner
Back to Top