The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Objects to feed an objectdatasource

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Peter van Ooijen

Posts: 284
Nickname: petergekko
Registered: Sep, 2003

Peter van Ooijen is a .NET devloper/architect for Gekko Software
Objects to feed an objectdatasource Posted: Apr 13, 2005 9:03 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Peter van Ooijen.
Original Post: Objects to feed an objectdatasource
Feed Title: Peter's Gekko
Feed URL: /error.htm?aspxerrorpath=/blogs/peter.van.ooijen/rss.aspx
Feed Description: My weblog cotains tips tricks and opinions on ASP.NET, tablet PC's and tech in general.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Peter van Ooijen
Latest Posts From Peter's Gekko

Advertisement

I had to miss the codeworse april fools joke. Spent the day doing my presentation on objectdatasources with VS 2005. On beforehand my main concern was setting up the hardware. Had to run VS 2005 from an external hard disk in 300Mb of memory. The sql server was running on another notebook. All was tied together with my old, but o so reliable Zyxel hub-accesspoint. Within 10 minutes I had to connect all cables, wake up the machines and get the network connected. Which went without a glitch. At the end of the session we had a VS 2005 solution containing a classlibrary, a web service and a web site up and running with debugging. All with the 300 megs of virtual PC RAM. Good job VS 2005 !

In a previous post I had introduced custom objects for an object datasource. To recapitulate: an object datasource expects an object which has methods to read, update, insert or delete data. The consumer of the datasource will invoke these methods. A typical consumer would be a gridview, by setting some properties on the grid you can browse and update (database) data without the need to write plumbing code. A TableAdapter in a dataset (2.0 style) fullfills this by default. In my example the object  fed to the datasource invoked a webservice to interchange data. The webserservice wrapped up the tableadapter and gives the opportunity to split my app’s layers into tiers.

[System.ComponentModel.DataObject]
public class BOwrapper
{

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)]
    public virtual int UpdateInvoice(DateTime InvoiceDate, int InvoiceNumber, int idCustomer, string Description, int Original_idInvoice)
    {
        DataHost.Service ws = new DataHost.Service();
        ws.UpdateInvoice(InvoiceDate, InvoiceNumber, idCustomer, Description, Original_idInvoice);
        return 1;
    }

// And so on…

During last friday’s presentation we had great fun feeding the datasource all kinds of objects. In the previous post I mentioned the  System.ComponentModel.DataObject attribute. This is used by VS but is not required at all, uncheking the checkbox will display all (referenced) classes found in the project. For instance you can feed the proxy of a webservice directly to an objectdatasource.

When a method on the datasource’s object is invoked it is sent a load of parameters. Reflection will query the object for a method with the specific name and a list of parameters with the specific names. When no method whose parameter names match is found an exception is thrown. Both method name and parameterlist can be set in the property window of the objectdatasource

Which parameters have to be sent to the object will vary. In a grid you will normally only edit a few columns. From a detailsview, which can also work with an objectdatsource, the number of updated fields and thus the number of parameters to the method will be larger. Both object-datasources can use the same class for their objects as long as the object has an overload for every number of parameters required. The webservice will do the actual update but you cannot overload the webmethods. A more flexible alternative is to give the webservice one update method which takes a datatable as parameter. For the table to be serializable VS will wrap it up in a dataset.

    [WebMethod]
    public int UpdateInvoice(DataSetInvoices.InvoicesDataTable invoice)
    {
        InvoicesTableAdapter ta = new InvoicesTableAdapter();
        return ta.Update(invoice);
    }

    [WebMethod]
    public DataSetInvoices.InvoicesDataTable Invoice(int id)
    {
        InvoicesTableAdapter ta = new InvoicesTableAdapter();
        return ta.GetDataById(id);
    }
 

The overloaded update methods of the bussinessobject all use this webmethod. As they have to return a full row of data they first have to read in the original row, update the columns passed in and return the full row.

   [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)]
    public virtual int UpdateInvoice(DateTime invoiceDate, int invoiceNumber, int idCustomer, string description, int original_idInvoice)
    {
        Service ws = new Service();
        DataSetInvoices.InvoicesDataTable it = ws.Invoice(original_idInvoice);
        it[0].InvoiceDate = invoiceDate;
        it[0].InvoiceNumber = invoiceNumber;
        it[0].idCustomer = idCustomer;
        it[0].Description = description;
        return ws.UpdateInvoice(it);

    }

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update)]
    public virtual int UpdateInvoice(int InvoiceNumber, int Original_idInvoice)
    {
        Service ws = new Service();
        DataSetInvoices.InvoicesDataTable it = ws.Invoice(Original_idInvoice);
        it[0].InvoiceNumber = InvoiceNumber;
        return ws.UpdateInvoice(it);
    }

This does increase overhead. Instead of one invocation of a webmethod passing just a couple of values we now have two invocations which send a complete dataset to and from the consumer. .NET 2.0 has two options to reduce the traffic. The beta-1 includes the option for a dataset to serialize itselef in a binary format, which is not that usefull on a webservice but quite usefull when you use remoting as RPC protocol. For the next beta it is announced that there is the option to leave out the dataset’s schema from the dataset when serializing it. Another optimization would be to delete the return type from the update web-methods. With WSE or Indigo the method invocation can than be one way, the update is sent to the service, response not needed.

This took me a lot of guessing, trial and error. The goal was to to build a webapp without having to write the plumbing code and without getting stuck with an app without layers or whose layers could not be split into tiers. All the online documentation mainly tlaks about SOA and team system but does not come with that much examples of real code. Beta 2 will have a lot of new things, I’m ready to give that a new look.

 

Read: Objects to feed an objectdatasource

Topic: Scrum and development methods Previous Topic   Next Topic Topic: I'll be at TechEd2005...

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use