CodeSmith Community
Your Code. Your Way. Faster!

Strategy for use transactions with a Smart Client

Latest post 11-05-2006 12:31 PM by hugozap. 4 replies.
  • 10-07-2006 1:13 PM

    • hugozap
    • Not Ranked
    • Joined on 10-07-2006
    • Posts 7
    • Points 125

    Strategy for use transactions with a Smart Client

    Hi All

    im in a situation where i need to use transactions for update some records in the database, however it is a Smart Client, and im using the WebService provider for the DAL, i know that there is work in progress to make the Service Layer avalaible as a Web Service too, and that transactions arent supported directly for the web service provider, however i would like to know what are my possibilities right now. I think that i can´t use the transactionmanager from the client, so i think i need to write some method in the data access layer to encapsulate the operations i need to include in the transaction, and then make it avalaible for the WebService provider, however im a little confused and dont know which is the right way to do it. Other option could be to write the custom store procedure with the transaction logic (BEGIN TRAN.....)  however i dont like it too much because i think this situation will appear in the future in my development so i will end up writing a lot of SQL. tnx for your help .

     Hugo

    • Post Points: 35
  • 10-08-2006 11:07 PM In reply to

    Re: Strategy for use transactions with a Smart Client

    Hi Hugo,

    Yes, to do this you would have to write something that would wrap the webservice calls.  There are a few ways to do this, and you would also have to write a few lines of custom logic in the webservice template because it currently passes null. 

    We already include an HTTPModule which does just allows you to create the transactions for every request.  You would just have to configure this in your web.config. 

     

       36     <httpModules>

       37       <add name="EntityTransactionModule" type="NameSpace.Web.Data.EntityTransactionModule"/>

       38     </httpModules>


    WebService.cst
    Then in your template, on every request into the DataRepository, you would  add the ability to get the transaction.  So it would be like:

    FROM:
        [WebMethod(Description="Inserts a row in the table <%=SourceTable.Name%>.")]
        public <%=className%> <%=providerName%>_<%= MethodNames.Insert %>(<%=className%> entity )
        {
            <%=DALNameSpace%>.DataRepository.<%=providerName%>.<%= MethodNames.Insert %>(entity);
            return entity;        
        }

     TO:

        [WebMethod(Description="Inserts a row in the table <%=SourceTable.Name%>.")]
        public <%=className%> <%=providerName%>_<%= MethodNames.Insert %>(<%=className%> entity )
        {
          TransactionManager tm;

            try
            {
                using (tm = EntityTransactionModule.TransactionManager)
                {

                if(!entity.IsValid)
                {
                    entity.Tag = enity.Error;
                    return entity;
                }

                <%=DALNameSpace%>.DataRepository.<%=providerName%>.<%= MethodNames.Insert %>(tm, entity);
                 tm.Commit();
              }
            }
           catch(Exception exc)
            {
              //you can use the Tag property to send messages, or the Error property will be filled of errors if the entity is not valid.
              entity.Tag = exc.Message;
            }

            return entity;        
        }
     //The end request of the http module will handle any cleanup and commits or rollbacks or you can set them explicity. The way i did it above, the dispose method will rollback if the tran is still open and it never reached the Commit();  If an exception is thrown, then mark the error and return.


    Robert Hinojosa
    -------------------------------------
    Member of the Codesmith Tools, .netTiers, teams
    http://www.nettiers.com
    -------------------------------------
    • Post Points: 35
  • 10-10-2006 7:09 PM In reply to

    • hugozap
    • Not Ranked
    • Joined on 10-07-2006
    • Posts 7
    • Points 125

    Re: Strategy for use transactions with a Smart Client

    Thanks for your answer Robert, i will try your solution
    • Post Points: 35
  • 11-02-2006 4:33 AM In reply to

    • kenmcc
    • Not Ranked
    • Joined on 03-23-2005
    • Posts 3
    • Points 45

    Re: Strategy for use transactions with a Smart Client

    Hi Hugo

    Just wondering how you got on with this?  I think the service idea is great... but it all needs to be underpinned with transactions....

    Regards

    Ken

    • Post Points: 35
  • 11-05-2006 12:31 PM In reply to

    • hugozap
    • Not Ranked
    • Joined on 10-07-2006
    • Posts 7
    • Points 125

    Re: Strategy for use transactions with a Smart Client

    Hi Ken

    Well, i decided to use the webservice that wraps the Service layer, so the service layer resides on the server and i can use transactions by overriding methods of the service or adding my own.  The problem with this approach is that there is not WebServiceClient project like the one for the web service for the DAL, and that project is required because if you look at the code of the webserviceclient you will see that Conversion between NEttiers entities and the autogenerated entities of the proxy generated by .NET is required . So i modified the nettiers template , to make it generate the WebServiceClient for the Service Layer Web service (which i name WebServiceClientBLL), and reference WebServiceClient.dll from my Windows forms project.  If i need to use transactions i have to follow the following steps:

    1) Add the method to the xxxService.cs file in the Service project, that will use a transaction.

    2) Add the method to the webService that calls the method created in step 1.

    3) Add the method to the WebServiceClientBLL class that wraps the webservice method i added in step 2.

    Its not very cool to do this, but is not difficult. besides this way i eliminate all the transaction related code from my client.

    Good luck

    Hugo Fernando Zapata.

    • Post Points: 5
Page 1 of 1 (5 items) | RSS
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems