CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

Objects to feed an objectdatasource

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.

 


Published Apr 06 2005, 06:13 AM by pvanooijen
Filed under: ,

Comments

John Papa said:

ObjectDataSource is awesome!

But even more impressive is that you got the hardware hooked up and running in 10 minutes flat! At some of the conferences you get 10 to 15 minutes to get up to the front, turn eveything on, hook it up, get video running, test it all out and get hooked to the mic. Occasionally I have run into a problem (usually with video) ... so I know how you feel.
# April 6, 2005 7:57 AM

pvanooijen said:

Thanks Jon.
I owe a lot to the Zyxel. It has a DHCP server onboard. Ipconfig/release. IpConfig/ renew and your (virtual) pc is on the network.
# April 7, 2005 12:43 PM

pvanooijen said:

John that is. :@
# April 7, 2005 12:44 PM

Peter's Gekko said:

In my apps I organize all interaction with my databases in components. On my component is a connection...
# May 18, 2005 7:01 AM

Peter's Gekko said:

In the North we were the first to have the MSDN/TechNet briefings. A couple of times a year MS organizes...
# October 12, 2005 7:25 AM

Peter's Gekko said:

The term RAD is more or less a hot topic on Codebetter. It started with a "RAD kills" post by Jeffrey...
# April 25, 2006 4:04 AM

Tagroom??? Blog said:

# April 26, 2006 2:37 AM

Bizuneh said:

Objectdatasource is owesome. but it has its own limitation. you have an Object Customer having 10 public properties. The Objectdatasource Update method takes Customer as a parameter. You are updating the first five properties of Customer object on the first page and the remaining on the next page. You want your update method to be executed only after collecting all the data from both page. So how do you maintain the state of the Object from One page to the other?
# July 13, 2006 2:11 PM

pvanooijen said:

The sole responsabillity of an ODS, or any other datasource, is data persistence. To preserve data in the webapp over roundtrips should be the responsabillity of another object. Which stores it in the viewstate, session or anywhere else.
# July 14, 2006 11:16 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!

Our Sponsors

Free Tech Publications

This Blog

Syndication

News